ircproxy  The Ultimate Cyborg

ServerConnection Class Reference

#include <ServerConnection.h>

Inheritance diagram for ServerConnection:

IdentityReference

List of all members.


Detailed Description

Object representing the server-side connection.

This object is owned by an Identity (and therefore has the same life time). The actual server connection is a ServerSession object that is created and managed by this object. Administrative information about the specific server itself (name, network, ports etc) is stored in a Server object.

Definition at line 54 of file ServerConnection.h.


Public Member Functions

 ServerConnection (Identity *identity)
 Constructor.
 ServerConnection (Identity *identity, boost::shared_ptr< Server > server)
 Constructor for secondary connection.
 ~ServerConnection ()
 Destructor.
state_nt state (void) const
 Return the state of the connection.
bool secondary_connection (void) const
 Return true if this is a secondary connection.
void set_my_serverside_nick_name (std::string const &my_serverside_nick_name)
 Set a new serverside nick name for myself. May only be called for a secondary connection.
std::string const & my_serverside_nick_name (void) const
 Get my serverside nick name. Only valid for secondary connection.
void change_state (state_nt, state_nt to)
 Change the state of the connection from from to to.
bool has_server (void) const
 Return true if we have a server object.
boost::shared_ptr< Server const > server (void) const
 Returns the server that we are connected with or connecting to.
boost::shared_ptr< Serverserver (void)
 Returns the server that we are connected with or connecting to.
bool has_server_session (void) const
 Return true if we have a server session object.
ServerSession const & server_session (void) const
 Return the ServerSession.
ServerSessionserver_session (void)
 Return the ServerSession.
Network const & network (void) const
 Return the network object of the current connection.
Networknetwork (void)
 Return the network object of the current connection.
void connect (bool auto_reconnect)
 Connect to the server and stay connected.
void disconnect (void)
 Disconnect from the server.
void session_lost (void)
 This function is called when the ServerSession object is destroyed.
void delayed_connect (timer_event_type_ct const &expired_at)
 Called when the timer for a delayed connect expired.
void received_server_port_answer (UserAnswerEventType const &event_type)
 This function is called when an answer to the server/port question is received.
void error_message_received (ServerMessageIn const &msg)
 This function is called if an ERROR message is received.
void successful_connection (void)
 Called when the first message is received from the server.
void decode_ping (ServerMessageIn const &pong_msg)
 This function is called when a PING message is received.
void send_pong_and_mode (ExpressionEventType const &event_type)
 This function is called when it is safe to call it.
void received_first_umode (MatcherEventType const &event_type)
 Called for the first MODE <mynick> received from the server.
bool is_connected (void) const
 Return true if we are connected with a server.
bool reconnect (void) const
 Return true if we try to (stay) connect(ed).
timeval connect_delay (void) const
 Accessor for M_connect_delay.
bool timed_out (void) const
 Accessor for M_timed_out.
int timed_out_count (void) const
 Accessor for M_timed_out_count.
void invisible_event (ExpressionEventType const &)
 Called when we first turn invisible.
void make_primary (void)
void make_secondary (void)

Private Member Functions

void do_connect (void)
 Attempt an immediate connect to the server.

Private Attributes

boost::shared_ptr< ServerM_server
 The server that we are connected to or trying to connect to, or NULL when the server is still unknown.
ServerSessionM_server_session
 A pointer to the current server session, or NULL when there is none.
bool M_reconnect
 True if we should try to (re)connect.
bool M_connected
state_nt M_state
 The state of the connection.
timeval M_connect_delay
 The next connect delay.
bool M_timed_out
 Set if the last connection attempted timed out.
int M_timed_out_count
 The number of connect attempts left that will time out, until we stop trying.
bool M_secondary_connection
 True if this connection is a secondary connection, while the primary still exists.
std::string M_my_serverside_nick_name
 This is my serverside nick name for as long as this is a secondary connection.

Static Private Attributes

static timeval S_connect_delay_init = { 1, 0 }
 The initial connect delay.

Friends

std::ostream & operator<< (std::ostream &os, ServerConnection const &server_connection)
 For debugging purposes. Write a ServerConnection to os.

Constructor & Destructor Documentation

ServerConnection::ServerConnection ( Identity identity  )  [inline]

Constructor.

Definition at line 90 of file ServerConnection.h.

References debug::channels::dc::objects.

00090                                        :
00091       event_client_ct(), IdentityReference(identity), M_server_session(NULL), M_reconnect(false),
00092       M_connected(false), M_state(disconnected),
00093       M_connect_delay(S_connect_delay_init), M_timed_out(false), M_timed_out_count(0),
00094       M_secondary_connection(false)
00095       {
00096         Dout(dc::objects, "Constructing ServerConnection(" << (void*)identity << ")");
00097       }

ServerConnection::ServerConnection ( Identity identity,
boost::shared_ptr< Server server 
) [inline]

Constructor for secondary connection.

Definition at line 100 of file ServerConnection.h.

References debug::channels::dc::objects.

00100                                                                        :
00101       event_client_ct(), IdentityReference(identity),
00102       M_server(server), M_server_session(NULL),
00103       M_reconnect(false), M_connected(false), M_state(disconnected),
00104       M_connect_delay(S_connect_delay_init), M_timed_out(false), M_timed_out_count(0),
00105       M_secondary_connection(true), M_my_serverside_nick_name(random_nick())
00106       {
00107         Dout(dc::objects, "Constructing secondary ServerConnection(" << (void*)identity << ")");
00108       }

ServerConnection::~ServerConnection (  )  [inline]

Destructor.

Definition at line 111 of file ServerConnection.h.

References disconnect(), M_server_session, and debug::channels::dc::objects.

00112       {
00113         Dout(dc::objects, "Destructing ServerConnection" << *this);
00114         cancel_all_requests();
00115         if (M_server_session)
00116           disconnect();
00117       }


Member Function Documentation

state_nt ServerConnection::state ( void   )  const [inline]

Return the state of the connection.

Definition at line 120 of file ServerConnection.h.

References M_state.

Referenced by Queue::flush(), ServerSession::new_message_received(), Identity::new_server_message_received(), operator<<(), and Identity::pass_as_is_to_server().

00120 { return M_state; }

void ServerConnection::set_my_serverside_nick_name ( std::string const &  my_serverside_nick_name  )  [inline]

Set a new serverside nick name for myself. May only be called for a secondary connection.

Definition at line 126 of file ServerConnection.h.

References ASSERT, M_my_serverside_nick_name, and M_secondary_connection.

std::string const& ServerConnection::my_serverside_nick_name ( void   )  const [inline]

Get my serverside nick name. Only valid for secondary connection.

Definition at line 130 of file ServerConnection.h.

References ASSERT, M_my_serverside_nick_name, and M_secondary_connection.

void ServerConnection::change_state ( state_nt  ,
state_nt  to 
) [inline]

Change the state of the connection from from to to.

Definition at line 134 of file ServerConnection.h.

References ASSERT, and M_state.

Referenced by ServerSession::new_message_received().

00135       {
00136         Debug(ASSERT(M_state == from));
00137         M_state = to;
00138       }

bool ServerConnection::has_server ( void   )  const [inline]

Return true if we have a server object.

Definition at line 141 of file ServerConnection.h.

References M_server.

Referenced by connect(), delayed_connect(), received_server_port_answer(), and server().

00141 { return M_server.get(); }

boost::shared_ptr<Server const> ServerConnection::server ( void   )  const [inline]

Returns the server that we are connected with or connecting to.

Definition at line 144 of file ServerConnection.h.

References ASSERT, has_server(), and M_server.

Referenced by ServerSession::detected_network(), ServerSession::found_network_name(), Matcher_noarg::match_impl(), network(), ServerSession::network_connection_terminated(), ServerSession::new_message_received(), and received_server_port_answer().

00144 { ASSERT(has_server()); return boost::const_pointer_cast<Server const>(M_server); }

boost::shared_ptr<Server> ServerConnection::server ( void   )  [inline]

Returns the server that we are connected with or connecting to.

Definition at line 147 of file ServerConnection.h.

References ASSERT, has_server(), and M_server.

00147 { ASSERT(has_server()); return M_server; }

bool ServerConnection::has_server_session ( void   )  const [inline]

Return true if we have a server session object.

Definition at line 150 of file ServerConnection.h.

References M_server_session.

Referenced by operator<<().

00150 { return M_server_session; }

ServerSession& ServerConnection::server_session ( void   )  [inline]

Return the ServerSession.

Definition at line 156 of file ServerConnection.h.

References M_server_session.

00156 { return *M_server_session; }

Network const& ServerConnection::network ( void   )  const [inline]

Return the network object of the current connection.

Definition at line 159 of file ServerConnection.h.

References server().

Referenced by Target::add_channel(), Target::add_joined_nick(), Target::get_joined_nick(), Identity::network(), and Target::serverside_add_channel().

00159 { return server()->network(); }

Network& ServerConnection::network ( void   )  [inline]

Return the network object of the current connection.

Definition at line 162 of file ServerConnection.h.

References server().

00162 { return server()->network(); }

void ServerConnection::connect ( bool  auto_reconnect  ) 

Connect to the server and stay connected.

This function should be called only after the ServerConnection was just created, or after calling ServerConnection::disconnect.

This function will start the procedure of connecting to a server, either the one already specified by ServerConnection::M_server or, if none is known, it will first ask the user for a server name and port.

It will continue to attempt to connect to a server (with appropriate connect delays if necessary) until connected. After a connection is successful then the authentication sequence is started (handled by Authentication).

If a connection is lost, at any time, then a new connection is automatically attempted.

Definition at line 73 of file ServerConnection.cc.

References ASSERT, Identity::client_session(), delayed_connect(), disconnect(), disconnected, do_connect(), DoutEntering, has_server(), IdentityReference::identity(), is_connected(), M_connect_delay, M_reconnect, M_secondary_connection, M_server, M_server_session, M_state, M_timed_out, M_timed_out_count, Identity::network_name(), received_server_port_answer(), server_port_question, and ClientSession::user_answer_event_server.

Referenced by do_connect(), Authentication::received_nick_and_user(), received_server_port_answer(), and session_lost().

00074 {
00075   DoutEntering(dc::debug, "ServerConnection::connect() for " << *this);
00076   ASSERT(!is_connected());
00077   ASSERT(M_state == disconnected);
00078   if (!M_server_session)        // Could already be allocated if has_server() was false
00079                                 // and we get here a second time because the ClientSession
00080                                 // was destroyed after requesting an answer.
00081   {
00082     M_server_session = new ServerSession(identity(), *this);
00083     AllocTag(M_server_session, (M_secondary_connection ? "Secondary Server session of " : "Server session of ") << identity().key());
00084   }
00085   if (auto_reconnect)
00086   {
00087     // See if we can find a network that this Identity belongs to.
00088     for (networks_type::iterator network_iter = Application::instance().networks().begin(); network_iter != Application::instance().networks().end(); ++network_iter)
00089       if (network_iter->name() == identity().network_name())
00090       {
00091         if (has_server())
00092         {
00093           if (!M_timed_out_count)
00094           {
00095             M_timed_out_count = 3 * network_iter->servers().size();
00096             ASSERT(M_timed_out_count != 0);     // We have at least one server because has_server() is true.
00097           }
00098           if (M_timed_out)
00099             --M_timed_out_count;
00100         }
00101         if (network_iter->servers().empty() || (M_timed_out && !M_timed_out_count))
00102           M_server.reset();     // If we don't have any servers, or when all servers timed out three times on a row.
00103         else
00104         {
00105           std::stable_sort(network_iter->servers().begin(), network_iter->servers().end(), ConnectionOrder());
00106           M_server = network_iter->servers().front();
00107         }
00108         break;
00109       }
00110   }
00111   M_reconnect = true;
00112   if (has_server())
00113   {
00114     timeval next_connect;
00115     timeradd(&M_server->last_connection_event(), &M_connect_delay, &next_connect);
00116     timeval now = timerRequest.get_now();
00117     if (timercmp(&now, &next_connect, >))
00118       do_connect();
00119     else
00120       timerRequest(M_connect_delay, *this, &ServerConnection::delayed_connect);
00121   }
00122   else if (identity().has_client_session())
00123   {
00124     ASSERT(!M_secondary_connection);    // The secondary connection should always have a server.
00125     identity().client_session().user_answer_event_server
00126         (UserAnswerRequestData(identity().client_session(), server_port_question,
00127         "What server should I connect to (type: <server> [<port> [<localip>]])?"),
00128         *this, &ServerConnection::received_server_port_answer);
00129   }
00130   else
00131     disconnect();
00132 }

void ServerConnection::disconnect ( void   )  [inline]

Disconnect from the server.

Definition at line 168 of file ServerConnection.h.

References ASSERT, DoutEntering, M_reconnect, and M_server_session.

Referenced by connect(), ServerSession::force_quit(), ServerSession::found_network_name(), ServerSession::new_message_received(), and ~ServerConnection().

00169       {
00170         DoutEntering(dc::debug, "ServerConnection::disconnect() for " << *this);
00171         ASSERT(M_server_session);
00172         M_reconnect = false;            // Before calling del(), otherwise 'session_lost' will
00173                                         // immediately try to reconnect.
00174         M_server_session->del();        // This calls session_lost (possibly with a tiny delay),
00175                                         // messages in the socket queue are first flushed.
00176       }

void ServerConnection::session_lost ( void   ) 

This function is called when the ServerSession object is destroyed.

Never call this function directly.

Definition at line 252 of file ServerConnection.cc.

References Identity::client_session(), connect(), Identity::destroy_secondary_server_connection(), disconnected, IdentityReference::identity(), M_connected, M_reconnect, M_secondary_connection, M_server, M_server_session, M_state, and ClientSession::serversession_lost().

Referenced by ServerSession::~ServerSession().

00253 { 
00254   if (M_state != disconnected)
00255   {
00256     // Inform the server object.
00257     M_server->connection_lost();
00258     // Let the client know that the connection was broken.
00259     if (!M_secondary_connection && identity().has_client_session())
00260       identity().client_session().serversession_lost();
00261   }
00262   M_connected = false;
00263   M_state = disconnected;
00264   M_server_session = NULL;
00265   if (M_secondary_connection)
00266   {
00267     M_reconnect = false;
00268     identity().destroy_secondary_server_connection();
00269   }
00270   if (M_reconnect)
00271     connect(true);
00272 }

void ServerConnection::delayed_connect ( timer_event_type_ct const &  expired_at  ) 

Called when the timer for a delayed connect expired.

Never call this function directly.

Definition at line 138 of file ServerConnection.cc.

References do_connect(), DoutEntering, has_server(), is_connected(), M_reconnect, and M_server_session.

Referenced by connect().

00139 {
00140   DoutEntering(dc::debug, "ServerConnection::delayed_connect(" << expired_at.get_expire_time() << ")");
00141   if (!is_connected() && M_reconnect && M_server_session && has_server())
00142     do_connect();
00143 }

void ServerConnection::received_server_port_answer ( UserAnswerEventType const &  event_type  ) 

This function is called when an answer to the server/port question is received.

Never call this function directly.

Definition at line 201 of file ServerConnection.cc.

References UserAnswer::answer(), connect(), DoutEntering, has_server(), IdentityReference::identity(), M_server, M_timed_out, and server().

Referenced by connect().

00202 {
00203   DoutEntering(dc::debug, "ServerConnection::received_server_port_answer(" << event_type << ")");
00204   std::string answer = event_type.answer();
00205   std::string hostname;
00206   unsigned int port = 6667;
00207   struct in_addr vhost = { INADDR_ANY };
00208 
00209   unsigned int count = 0;
00210   std::stringstream ss(answer);
00211   std::string buf;
00212   while ((ss >> buf))
00213   {
00214     if (count == 0)
00215       hostname = buf;
00216     else if (count == 1)
00217       port = atoi(buf.c_str());
00218     else if (count == 2)
00219       inet_aton(buf.c_str(), &vhost);
00220     ++count;
00221   }
00222 
00223   // See if this server was already known.
00224   bool found = false;
00225   for (networks_type::iterator network_iter = Application::instance().networks().begin(); network_iter != Application::instance().networks().end(); ++network_iter)
00226   {
00227     // Never change network.
00228     if (has_server() && server()->network().name() != network_iter->name())
00229       continue;
00230     for (Network::servers_type::iterator server_iter = network_iter->servers().begin(); server_iter != network_iter->servers().end(); ++server_iter)
00231       if (strcasecmp((*server_iter)->hostname().c_str(), hostname.c_str()) == 0 && memcmp(&(*server_iter)->vhost(), &vhost, sizeof(vhost)) == 0)
00232       {
00233         Dout(dc::notice, "Found existing server " << *server_iter);
00234         found = true;
00235         M_server = *server_iter;
00236         M_server->set_port(port);
00237         break;
00238       }
00239     if (found)
00240       break;
00241   }
00242   if (!found)
00243     M_server.reset(NEW(Server(identity().private_network(), hostname, port, vhost)));
00244   M_timed_out = false;
00245   connect(false);
00246 }

void ServerConnection::error_message_received ( ServerMessageIn const &  msg  ) 

This function is called if an ERROR message is received.

Never call this function directly.

Definition at line 281 of file ServerConnection.cc.

References DoutEntering, M_connect_delay, M_state, MessageIn::param(), server_session(), ServerSession::set_disconnect_reason(), and successful_connect.

Referenced by ServerSession::new_message_received().

00282 {
00283   DoutEntering(dc::notice, "ServerConnection::error_message_received(" << msg << ")");
00284 
00285   server_session().set_disconnect_reason(msg.param(0));
00286 
00287   if (M_state < successful_connect)
00288   {
00289     // FIXME: only do this when the connection was throttled.
00290     // Different actions are needed on different errors.
00291     M_connect_delay.tv_sec *= 2;  
00292     if (M_connect_delay.tv_sec > 600)
00293       M_connect_delay.tv_sec = 600;
00294     Dout(dc::notice, "Incrementing connect delay to " << M_connect_delay);
00295   }
00296 }

void ServerConnection::successful_connection ( void   ) 

Called when the first message is received from the server.

Definition at line 300 of file ServerConnection.cc.

References ASSERT, authentication_started, M_connect_delay, M_state, M_timed_out_count, S_connect_delay_init, and successful_connect.

Referenced by AuthState::auth_notice_looking_up_hostname(), AuthState::auth_notice_looking_up_ident(), and decode_ping().

00301 {
00302   ASSERT(M_state == authentication_started || M_state == successful_connect);
00303   M_state = successful_connect;
00304   M_connect_delay = S_connect_delay_init;       // We didn't get throttled.
00305   M_timed_out_count = 0;
00306 }

void ServerConnection::decode_ping ( ServerMessageIn const &  ping_msg  ) 

This function is called when a PING message is received.

Never call this function directly.

Definition at line 312 of file ServerConnection.cc.

References default_priority(), M_server_session, M_state, MessageIn::param(), pong_sent, ServerSession::queue_msg(), real_nick_sent, server_session(), ServerSession::set_auth_ping_argument(), and successful_connection().

Referenced by ServerSession::new_message_received().

00313 {
00314   if (M_state >= real_nick_sent)
00315   {
00316     MessageOut msgout(default_priority(keys::PONG));
00317     server_session().queue_msg(msgout << keys::PONG << ping_msg.param(0));
00318   }
00319   else if (M_state >= pong_sent)
00320     *M_server_session << "PONG " << ping_msg.param(0) << "\r\n";
00321   else
00322   {
00323     successful_connection();
00324     M_server_session->set_auth_ping_argument(ping_msg.param(0));
00325   }
00326 }

void ServerConnection::send_pong_and_mode ( ExpressionEventType const &  event_type  ) 

This function is called when it is safe to call it.

See AuthState::AuthState. Never call this function directly.

Definition at line 333 of file ServerConnection.cc.

References ASSERT, ServerSession::auth_ping_argument(), DoutEntering, invisible_event(), ServerSession::M_detected_network, M_server, M_server_session, M_state, ServerSession::message_event_server(), MatchRequest::mynick(), ServerSession::nicks(), NickPair::out_nick(), pong_sent, received_first_umode(), successful_connect, and ServerSession::user_mode().

Referenced by AuthState::send_pong_and_mode().

00334 {
00335   DoutEntering(dc::notice, "ServerConnection::send_pong_and_mode(" << event_type << ")");
00336 
00337   ASSERT(M_state == successful_connect);                        // We received at least the PING.
00338   ASSERT(!M_server_session->auth_ping_argument().empty());      // Didn't we?
00339 
00340   *M_server_session << "PONG " << M_server_session->auth_ping_argument() << "\r\n";
00341   M_state = pong_sent;
00342 
00343   // Send the 'invisible' user mode in the same packet as the PONG, in the hope
00344   // that the server will process them at the same time. That way we are not
00345   // (for 2 seconds) visible to the network.
00346   // Thus, we will have sent [PASS], NICK, USER, PONG, MODE +i -- which should
00347   // be processed immediately (we have 5 free messages before the flood protection
00348   // kicks in).
00349   char service_mode = 'x';      // FIXME, this isn't the same on every network.
00350   // If we don't know if the USER did set the user mode, ask for the user mode to see if it did.
00351   // Always sent the +i to assure invisibility at the same time as the PONG is
00352   // processed.
00353   if (!M_server->version().get_user_supports_umode())
00354   {
00355     // Always set invisibility.
00356     *M_server_session << "MODE " << M_server_session->nicks().out_nick() << " +i\r\n";
00357     // Test.
00358     *M_server_session << "MODE " << M_server_session->nicks().out_nick() << "\r\n";
00359     // Finish.
00360     *M_server_session << "MODE " << M_server_session->nicks().out_nick() << " +" << service_mode << "\r\n";
00361   }
00362   else if (!*M_server->version().get_user_supports_umode())
00363     *M_server_session << "MODE " << M_server_session->nicks().out_nick() << " +i" << service_mode << "\r\n";
00364 
00365   ConstExpression<UserModeMask> mask(UserModeMask('i') | service_mode);
00366   ON_TRUE((*M_server_session->user_mode() & mask) == mask && *M_server_session->M_detected_network, *this, &ServerConnection::invisible_event);
00367 
00368   MatchRequest match_request;
00369   match_request(keys::MODE).mynick(0);
00370   M_server_session->message_event_server()(match_request, *this, &ServerConnection::received_first_umode);
00371 }

void ServerConnection::received_first_umode ( MatcherEventType const &  event_type  ) 

Called for the first MODE <mynick> received from the server.

This call-back is used to extract the real ident and real hostname, as the IRC server sees it.

Definition at line 392 of file ServerConnection.cc.

References ASSERT, DoutEntering, IdentityReference::identity(), MatcherEventType::message(), MessageIn::prefix_part(), Identity::set_ident(), and Identity::set_real_hostname().

Referenced by send_pong_and_mode().

00393 {
00394   DoutEntering(dc::debug, "ServerConnection::received_first_umode(" << event_type << ")");
00395   MessageIn::Part const& prefix(event_type.message().prefix_part());
00396   char const* p = strchr(prefix.start(), '!');
00397   ASSERT(p);
00398   char const* ident = p + 1;
00399   p = strchr(p, '@');
00400   ASSERT(p);
00401   size_t ident_len = p - ident;
00402   char const* real_hostname = p + 1;
00403   size_t real_hostname_len = prefix.end() - real_hostname;
00404   identity().set_real_hostname(std::string(real_hostname, real_hostname_len));
00405   identity().set_ident(std::string(ident, ident_len));
00406 }

bool ServerConnection::is_connected ( void   )  const [inline]

Return true if we are connected with a server.

Definition at line 190 of file ServerConnection.h.

References M_connected.

Referenced by connect(), delayed_connect(), and operator<<().

00190 { return M_connected; }

bool ServerConnection::reconnect ( void   )  const [inline]

Return true if we try to (stay) connect(ed).

Definition at line 193 of file ServerConnection.h.

References M_reconnect.

Referenced by operator<<(), and ServerSession::~ServerSession().

00193 { return M_reconnect; }

timeval ServerConnection::connect_delay ( void   )  const [inline]

Accessor for M_connect_delay.

Definition at line 196 of file ServerConnection.h.

References M_connect_delay.

Referenced by operator<<().

00196 { return M_connect_delay; }

bool ServerConnection::timed_out ( void   )  const [inline]

Accessor for M_timed_out.

Definition at line 199 of file ServerConnection.h.

References M_timed_out.

Referenced by operator<<().

00199 { return M_timed_out; }

int ServerConnection::timed_out_count ( void   )  const [inline]

Accessor for M_timed_out_count.

Definition at line 202 of file ServerConnection.h.

References M_timed_out_count.

Referenced by operator<<().

00202 { return M_timed_out_count; }

void ServerConnection::invisible_event ( ExpressionEventType const &   ) 

Called when we first turn invisible.

This function is called when MODE +i is received.

Never call this function directly.

Definition at line 377 of file ServerConnection.cc.

References ASSERT, Identity::authentication(), DoutEntering, IdentityReference::identity(), Identity::key(), login_sent, M_server_session, M_state, pong_sent, and Authentication::service_pass().

Referenced by send_pong_and_mode().

00378 {
00379   DoutEntering(dc::notice, "ServerConnection::invisible_event(" << event_type << ')');
00380   ASSERT(event_type);   // It should be true, of course.
00381   
00382   ASSERT(M_state == pong_sent);
00383   M_state = login_sent;
00384   *M_server_session << "PRIVMSG X@channels.undernet.org :login " <<
00385       identity().key() << ' ' << identity().authentication().service_pass() << "\r\n";
00386 }

void ServerConnection::make_primary ( void   )  [inline]

For internal use only.

Definition at line 208 of file ServerConnection.h.

References M_secondary_connection.

00208 { M_secondary_connection = false; }

void ServerConnection::make_secondary ( void   )  [inline]

For internal use only.

Definition at line 211 of file ServerConnection.h.

References M_secondary_connection.

00211 { M_secondary_connection = true; }

void ServerConnection::do_connect ( void   )  [private]

Attempt an immediate connect to the server.

Never call this function directly. Call ServerConnection::connect() instead.

Definition at line 149 of file ServerConnection.cc.

References ASSERT, authentication_started, Identity::client_session(), connect(), ClientSession::del_private_target(), disconnected, DoutEntering, IdentityReference::identity(), M_connected, M_secondary_connection, M_server, M_server_session, M_state, M_timed_out, question_target_name(), Identity::send_notice(), server_port_question, and ServerSession::start_authentication().

Referenced by connect(), and delayed_connect().

00150 {
00151   DoutEntering(dc::debug, "ServerConnection::do_connect()");
00152   ASSERT(M_state == disconnected);
00153 
00154   if (identity().has_client_session())
00155   {
00156     std::ostringstream buf;
00157     buf << "Trying to connect to \"" << M_server->hostname().c_str() << "\" (port " << M_server->next_port() << ").";
00158     identity().send_notice(buf.str(), server_port_question);
00159   }
00160 
00161   // Reset the timed_out flag.
00162   M_timed_out = false;
00163   // Set new time out timer.
00164   timeval delay = { 4, 0 };
00165   timerRequest(delay, *this, &ServerConnection::connect_timed_out);
00166 
00167   ASSERT(M_server->next_port() != 0);
00168   if (M_server_session->connect(M_server->hostname().c_str(), M_server->next_port(), M_server->vhost()))
00169   {
00170     M_server->attempting_to_connect();
00171     M_connected = true;
00172     M_server_session->start_authentication();
00173     M_state = authentication_started;
00174   }
00175   else
00176   {
00177     ASSERT(!M_secondary_connection);    // FIXME
00178     // If the connect failed, then we have to ask the user for another servername.
00179     M_server.reset();
00180     if (identity().has_client_session())
00181       identity().client_session().del_private_target(question_target_name(server_port_question));
00182     // Try again.
00183     connect(true);
00184   }
00185 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
ServerConnection const &  server_connection 
) [friend]

For debugging purposes. Write a ServerConnection to os.

Definition at line 318 of file debug_ostream_operators.cc.

00319 {
00320   os << "{server:";
00321   if (server_connection.M_server.get())
00322     os << *server_connection.M_server.get();
00323   else
00324     os << "<none>";
00325   os << "; server_session:";
00326   if (server_connection.has_server_session())
00327     os << server_connection.server_session();
00328   else
00329     os << "<NULL>";
00330   os << "; reconnect:";
00331   if (server_connection.reconnect())
00332     os << "true";
00333   else
00334     os << "false";
00335   os << "; connected:";
00336   if (server_connection.is_connected())
00337     os << "true";
00338   else
00339     os << "false";
00340   os << "; state:" << server_connection.state();
00341   os << "; connect_delay:" << server_connection.connect_delay();
00342   os << "; timed_out:";
00343   if (server_connection.timed_out())
00344     os << "true";
00345   else
00346     os << "false";
00347   os << "; timed_out_count:" << server_connection.timed_out_count();
00348   os << "; secondary_connection:" << server_connection.secondary_connection();
00349   return os << '}';
00350 }


Member Data Documentation

boost::shared_ptr<Server> ServerConnection::M_server [private]

The server that we are connected to or trying to connect to, or NULL when the server is still unknown.

Definition at line 57 of file ServerConnection.h.

Referenced by connect(), do_connect(), has_server(), operator<<(), received_server_port_answer(), send_pong_and_mode(), server(), and session_lost().

True if we should try to (re)connect.

Definition at line 63 of file ServerConnection.h.

Referenced by connect(), delayed_connect(), disconnect(), reconnect(), and session_lost().

TCP/IP connect in progress, or connected. True if we actually attempted to connect to the server (after calling ServerConnection::do_connect). Reset when the session was lost (or destructed).

Definition at line 68 of file ServerConnection.h.

Referenced by do_connect(), is_connected(), and session_lost().

The next connect delay.

Definition at line 73 of file ServerConnection.h.

Referenced by connect(), connect_delay(), error_message_received(), and successful_connection().

timeval ServerConnection::S_connect_delay_init = { 1, 0 } [static, private]

The initial connect delay.

Definition at line 74 of file ServerConnection.h.

Referenced by successful_connection().

Set if the last connection attempted timed out.

Definition at line 77 of file ServerConnection.h.

Referenced by connect(), do_connect(), received_server_port_answer(), and timed_out().

The number of connect attempts left that will time out, until we stop trying.

Definition at line 80 of file ServerConnection.h.

Referenced by connect(), successful_connection(), and timed_out_count().

True if this connection is a secondary connection, while the primary still exists.

Definition at line 83 of file ServerConnection.h.

Referenced by connect(), do_connect(), make_primary(), make_secondary(), my_serverside_nick_name(), secondary_connection(), session_lost(), and set_my_serverside_nick_name().

This is my serverside nick name for as long as this is a secondary connection.

Definition at line 86 of file ServerConnection.h.

Referenced by my_serverside_nick_name(), and set_my_serverside_nick_name().


The documentation for this class was generated from the following files:

Copyright © 2005-2007 Carlo Wood.  All rights reserved.