--- a/sources/network/rconsession.cpp Fri May 15 18:36:22 2015 +0300 +++ b/sources/network/rconsession.cpp Fri May 15 20:03:35 2015 +0300 @@ -3,10 +3,11 @@ // ------------------------------------------------------------------------------------------------- // -RCONSession::RCONSession() : +RCONSession::RCONSession (Interface* iface) : m_state (RCON_DISCONNECTED), m_lastPing (0), - m_numAdmins (0) + m_numAdmins (0), + m_interface (iface) { if (not m_socket.set_blocking (false)) { @@ -22,19 +23,17 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::connect (IPAddress address) -> void +void RCONSession::connect (IPAddress address) { m_address = address; m_state = RCON_CONNECTING; - Interface::update_statusbar(); + m_interface->update_statusbar(); send_hello(); } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::disconnect() -> void +void RCONSession::disconnect() { if (m_state > RCON_CONNECTING) { @@ -42,8 +41,8 @@ Bytestream packet; packet.write_byte (CLRC_DISCONNECT); this->send (packet); - print ("Disconnected from %1\n", m_address.to_string (IP_WITH_PORT)); - Interface::update_statusbar(); + m_interface->print ("Disconnected from %1\n", m_address.to_string (IP_WITH_PORT)); + m_interface->update_statusbar(); } m_state = RCON_DISCONNECTED; @@ -51,16 +50,14 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::send (const Bytestream& packet) -> void +void RCONSession::send (const Bytestream& packet) { m_socket.send (m_address, packet); } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::tick() -> void +void RCONSession::tick() { if (m_state == RCON_DISCONNECTED) return; @@ -93,8 +90,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::handle_packet (Bytestream& packet, const IPAddress& from) -> void +void RCONSession::handle_packet (Bytestream& packet, const IPAddress& from) { if (from != m_address) return; @@ -108,12 +104,12 @@ switch (ServerResponse (header)) { case SVRC_OLDPROTOCOL: - print_error ("Your RCON client is using outdated protocol.\n"); + m_interface->print_error ("Your RCON client is using outdated protocol.\n"); m_state = RCON_DISCONNECTED; break; case SVRC_BANNED: - print_error ("You have been banned from the server.\n"); + m_interface->print_error ("You have been banned from the server.\n"); m_state = RCON_DISCONNECTED; break; @@ -124,7 +120,7 @@ break; case SVRC_INVALIDPASSWORD: - print_error ("Login failed.\n"); + m_interface->print_error ("Login failed.\n"); m_state = RCON_DISCONNECTED; break; @@ -132,30 +128,30 @@ { String message = packet.read_string(); message.normalize(); - print ("%1\n", message); + m_interface->print ("%1\n", message); } break; case SVRC_LOGGEDIN: - print ("Login successful!\n"); + m_interface->print ("Login successful!\n"); m_serverProtocol = packet.read_byte(); m_hostname = packet.read_string(); - Interface::set_title (m_hostname); + m_interface->set_title (m_hostname); m_state = RCON_CONNECTED; for (int i = packet.read_byte(); i > 0; --i) process_server_updates (packet); - print ("Previous messages:\n"); + m_interface->print ("Previous messages:\n"); for (int i = packet.read_byte(); i > 0; --i) { String message = packet.read_string(); message.normalize(); - print ("--- %1\n", message); + m_interface->print ("--- %1\n", message); } - print ("End of previous messages.\n"); + m_interface->print ("End of previous messages.\n"); break; case SVRC_UPDATE: @@ -165,7 +161,7 @@ case SVRC_TOOMANYTABCOMPLETES: { unsigned int numCompletions = packet.read_short(); - print ("%1 completions for '%2'.\n", + m_interface->print ("%1 completions for '%2'.\n", int (numCompletions), m_lastTabComplete); } break; @@ -178,16 +174,18 @@ completes << packet.read_string(); if (completes.size() == 1) - Interface::tab_complete (m_lastTabComplete, completes[0]); + { + m_interface->tab_complete (m_lastTabComplete, completes[0]); + } else if (not completes.is_empty()) { - print ("Completions for '%1':\n", m_lastTabComplete); + m_interface->print ("Completions for '%1':\n", m_lastTabComplete); for (int i = 0; i < completes.size(); i += 8) { Range<int> spliceRange (i, min (i + 8, completes.size() - 1)); StringList splice (completes.splice (spliceRange)); - print ("- %1\n", splice.join (", ")); + m_interface->print ("- %1\n", splice.join (", ")); } } } @@ -197,12 +195,11 @@ } catch (std::exception& e) { - print_warning ("Couldn't process packet: %1\n", e.what()); + m_interface->print_warning ("Couldn't process packet: %1\n", e.what()); } } -METHOD -RCONSession::process_server_updates (Bytestream& packet) -> void +void RCONSession::process_server_updates (Bytestream& packet) { int header = packet.read_byte(); @@ -215,40 +212,38 @@ for (int i = packet.read_byte(); i > 0; --i) players.append (packet.read_string()); - Interface::set_player_names (players); + m_interface->set_player_names (players); } break; case SVRCU_ADMINCOUNT: m_numAdmins = packet.read_byte(); - Interface::update_statusbar(); + m_interface->update_statusbar(); break; case SVRCU_MAP: m_level = packet.read_string(); - Interface::update_statusbar(); + m_interface->update_statusbar(); break; default: - print_warning ("Unknown server update type: %d\n", header); + m_interface->print_warning ("Unknown server update type: %d\n", header); break; } } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::socket() -> UDPSocket* +UDPSocket* RCONSession::socket() { return &m_socket; } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::send_hello() -> void +void RCONSession::send_hello() { - print ("Connecting to %1...\n", m_address.to_string (IP_WITH_PORT)); + m_interface->print ("Connecting to %1...\n", m_address.to_string (IP_WITH_PORT)); Bytestream packet; packet.write_byte (CLRC_BEGINCONNECTION); packet.write_byte (RCON_PROTOCOL_VERSION); @@ -258,10 +253,9 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::send_password() -> void +void RCONSession::send_password() { - print ("Authenticating...\n"); + m_interface->print ("Authenticating...\n"); Bytestream packet; packet.write_byte (CLRC_PASSWORD); packet.write_string ((m_salt + m_password).md5()); @@ -271,16 +265,14 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::set_password (const String& password) -> void +void RCONSession::set_password (const String& password) { m_password = password; } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::bump_last_ping() -> void +void RCONSession::bump_last_ping() { time_t now; time (&now); @@ -289,17 +281,7 @@ // ------------------------------------------------------------------------------------------------- // -STATIC METHOD -RCONSession::get_session() -> RCONSession* -{ - static RCONSession session; - return &session; -} - -// ------------------------------------------------------------------------------------------------- -// -METHOD -RCONSession::is_active() const -> bool +bool RCONSession::is_active() const { return state() != RCON_DISCONNECTED; } @@ -307,8 +289,7 @@ // ------------------------------------------------------------------------------------------------- // Returns true if the message was successfully sent. // -METHOD -RCONSession::send_command (const String& message) -> bool +bool RCONSession::send_command (const String& message) { if (m_state != RCON_CONNECTED or message.is_empty()) return false; @@ -323,40 +304,35 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::state() const -> RCONSessionState +RCONSessionState RCONSession::state() const { return m_state; } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::address() const -> const IPAddress& +const IPAddress& RCONSession::address() const { return m_address; } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::num_admins() const -> int +int RCONSession::num_admins() const { return m_numAdmins; } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::level() const -> const String& +const String& RCONSession::level() const { return m_level; } // ------------------------------------------------------------------------------------------------- // -METHOD -RCONSession::request_tab_complete (const String& part) -> void +void RCONSession::request_tab_complete (const String& part) { if (m_serverProtocol >= 4) { @@ -368,5 +344,5 @@ m_lastTabComplete = part; } else - print ("Server protocol is %1, cannot tab-complete\n", m_serverProtocol); -} + m_interface->print ("Server protocol is %1, cannot tab-complete\n", m_serverProtocol); +} \ No newline at end of file