--- a/sources/network/rconsession.cpp Wed Jul 20 18:29:13 2016 +0300 +++ b/sources/network/rconsession.cpp Wed Jul 20 22:56:16 2016 +0300 @@ -36,16 +36,16 @@ // ------------------------------------------------------------------------------------------------- // RCONSession::RCONSession() : - m_state (RCON_DISCONNECTED), - m_lastPing (0), - m_numAdmins (0), - m_interface (nullptr) + m_state(RCON_DISCONNECTED), + m_lastPing(0), + m_adminCount(0), + m_interface(nullptr) { - if (not m_socket.set_blocking (false)) + if (not m_socket.set_blocking(false)) { - fprintf (stderr, "unable to set socket as non-blocking: %s\n", + fprintf(stderr, "unable to set socket as non-blocking: %s\n", m_socket.error_string().chars()); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } } @@ -55,12 +55,12 @@ // ------------------------------------------------------------------------------------------------- // -void RCONSession::connect (IPAddress address) +void RCONSession::connect(IPAddress address) { m_address = address; m_state = RCON_CONNECTING; m_interface->updateStatusBar(); - send_hello(); + sendHello(); } // ------------------------------------------------------------------------------------------------- @@ -71,8 +71,8 @@ { // Say goodbye to remote Bytestream packet; - packet.write_byte (CLRC_DISCONNECT); - this->send (packet); + packet.write_byte(CLRC_DISCONNECT); + this->send(packet); m_interface->disconnected(); } @@ -81,9 +81,9 @@ // ------------------------------------------------------------------------------------------------- // -void RCONSession::send (const Bytestream& packet) +void RCONSession::send(const Bytestream& packet) { - m_socket.send (m_address, packet); + m_socket.send(m_address, packet); } // ------------------------------------------------------------------------------------------------- @@ -94,34 +94,34 @@ return; time_t now; - time (&now); + time(&now); if (m_lastPing < now) { if (m_state == RCON_CONNECTING) { - send_hello(); + sendHello(); } else if (m_state == RCON_AUTHENTICATING) { - send_password(); + sendPassword(); } else if (m_state == RCON_CONNECTED and m_lastPing + 5 < now) { Bytestream packet; - packet.write_byte (CLRC_PONG); - send (packet); - bump_last_ping(); + packet.write_byte(CLRC_PONG); + send(packet); + bumpLastPing(); } } - for (Datagram datagram; m_socket.read (datagram);) - handle_packet (datagram); + for (Datagram datagram; m_socket.read(datagram);) + handlePacket(datagram); } // ------------------------------------------------------------------------------------------------- // -void RCONSession::handle_packet (Datagram& datagram) +void RCONSession::handlePacket(Datagram& datagram) { if (datagram.address != m_address) return; @@ -132,26 +132,26 @@ { int header = datagram.message.read_byte(); - switch (ServerResponse (header)) + switch (ServerResponse(header)) { case SVRC_OLDPROTOCOL: - m_interface->printError ("Your RCON client is using outdated protocol.\n"); + m_interface->printError("Your RCON client is using outdated protocol.\n"); m_state = RCON_DISCONNECTED; break; case SVRC_BANNED: - m_interface->printError ("You have been banned from the server.\n"); + m_interface->printError("You have been banned from the server.\n"); m_state = RCON_DISCONNECTED; break; case SVRC_SALT: m_salt = datagram.message.read_string(); m_state = RCON_AUTHENTICATING; - send_password(); + sendPassword(); break; case SVRC_INVALIDPASSWORD: - m_interface->printError ("Login failed.\n"); + m_interface->printError("Login failed.\n"); m_state = RCON_DISCONNECTED; break; @@ -159,41 +159,41 @@ { String message = datagram.message.read_string(); message.normalize(); - m_interface->printText ("%s\n", message.chars()); + m_interface->printText("%s\n", message.chars()); } break; case SVRC_LOGGEDIN: - m_interface->print ("Login successful!\n"); + m_interface->print("Login successful!\n"); m_serverProtocol = datagram.message.read_byte(); m_hostname = datagram.message.read_string(); - m_interface->setTitle (m_hostname); + m_interface->setTitle(m_hostname); m_state = RCON_CONNECTED; for (int i = datagram.message.read_byte(); i > 0; --i) - process_server_updates (datagram.message); + processServerUpdates(datagram.message); - m_interface->print ("Previous messages:\n"); + m_interface->print("Previous messages:\n"); for (int i = datagram.message.read_byte(); i > 0; --i) { String message = datagram.message.read_string(); message.normalize(); - m_interface->printText ("--- %s\n", message.chars()); + m_interface->printText("--- %s\n", message.chars()); } - m_interface->print ("End of previous messages.\n"); + m_interface->print("End of previous messages.\n"); break; case SVRC_UPDATE: - process_server_updates (datagram.message); + processServerUpdates(datagram.message); break; case SVRC_TOOMANYTABCOMPLETES: { unsigned int numCompletions = datagram.message.read_short(); - m_interface->print ("%d completions for '%s'.\n", - int (numCompletions), m_lastTabComplete.chars()); + m_interface->print("%d completions for '%s'.\n", + int(numCompletions), m_lastTabComplete.chars()); } break; @@ -207,17 +207,17 @@ if (completes.size() == 1) { - m_interface->tabComplete (m_lastTabComplete, completes[0]); + m_interface->tabComplete(m_lastTabComplete, completes[0]); } else if (not completes.is_empty()) { - m_interface->print ("Completions for '%s':\n", m_lastTabComplete.chars()); + m_interface->print("Completions for '%s':\n", m_lastTabComplete.chars()); for (int i : range(0, completes.size(), 8)) { - Range<int> spliceRange (i, min (i + 8, completes.size())); - StringList splice (completes.splice (spliceRange)); - m_interface->print ("- %s\n", splice.join (", ").chars()); + Range<int> spliceRange(i, min(i + 8, completes.size())); + StringList splice(completes.splice(spliceRange)); + m_interface->print("- %s\n", splice.join(", ").chars()); } } } @@ -227,29 +227,29 @@ } catch (std::exception& e) { - m_interface->printWarning ("Couldn't process packet: %s\n", e.what()); + m_interface->printWarning("Couldn't process packet: %s\n", e.what()); } } -void RCONSession::process_server_updates (Bytestream& packet) +void RCONSession::processServerUpdates(Bytestream& packet) { int header = packet.read_byte(); - switch (RCONUpdateType (header)) + switch (RCONUpdateType(header)) { case SVRCU_PLAYERDATA: { StringList players; for (int i = packet.read_byte(); i > 0; --i) - players.append (packet.read_string()); + players.append(packet.read_string()); - m_interface->setPlayerNames (players); + m_interface->setPlayerNames(players); } break; case SVRCU_ADMINCOUNT: - m_numAdmins = packet.read_byte(); + m_adminCount = packet.read_byte(); m_interface->updateStatusBar(); break; @@ -259,85 +259,84 @@ break; default: - m_interface->printWarning ("Unknown server update type: %d\n", header); + m_interface->printWarning("Unknown server update type: %d\n", header); break; } } // ------------------------------------------------------------------------------------------------- // -UDPSocket* RCONSession::socket() +UDPSocket* RCONSession::getSocket() { return &m_socket; } // ------------------------------------------------------------------------------------------------- // -void RCONSession::send_hello() +void RCONSession::sendHello() { - m_interface->print ("Connecting to %s...\n", - m_address.to_string (IPAddress::WITH_PORT).chars()); + m_interface->print("Connecting to %s...\n", m_address.to_string(IPAddress::WITH_PORT).chars()); Bytestream packet; - packet.write_byte (CLRC_BEGINCONNECTION); - packet.write_byte (RCON_PROTOCOL_VERSION); - send (packet); - bump_last_ping(); + packet.write_byte(CLRC_BEGINCONNECTION); + packet.write_byte(RCON_PROTOCOL_VERSION); + send(packet); + bumpLastPing(); } // ------------------------------------------------------------------------------------------------- // -void RCONSession::send_password() +void RCONSession::sendPassword() { - m_interface->print ("Authenticating...\n"); + m_interface->print("Authenticating...\n"); Bytestream packet; - packet.write_byte (CLRC_PASSWORD); - packet.write_string ((m_salt + m_password).md5()); - send (packet); - bump_last_ping(); + packet.write_byte(CLRC_PASSWORD); + packet.write_string((m_salt + m_password).md5()); + send(packet); + bumpLastPing(); } // ------------------------------------------------------------------------------------------------- // -void RCONSession::set_password (const String& password) +void RCONSession::setPassword(const String& password) { m_password = password; } // ------------------------------------------------------------------------------------------------- // -void RCONSession::bump_last_ping() +void RCONSession::bumpLastPing() { time_t now; - time (&now); + time(&now); m_lastPing = now; } // ------------------------------------------------------------------------------------------------- // -bool RCONSession::is_active() const +bool RCONSession::isActive() const { - return state() != RCON_DISCONNECTED; + return getState() != RCON_DISCONNECTED; } // ------------------------------------------------------------------------------------------------- // Returns true if the message was successfully sent. // -bool RCONSession::send_command (const String& message) +bool RCONSession::sendCommand(const String& message) { if (m_state != RCON_CONNECTED or message.isEmpty()) return false; Bytestream packet; - packet.write_byte (CLRC_COMMAND); - packet.write_string (message); - send (packet); - bump_last_ping(); + packet.write_byte(CLRC_COMMAND); + packet.write_string(message); + send(packet); + bumpLastPing(); return true; } // ------------------------------------------------------------------------------------------------- // -RCONSessionState RCONSession::state() const +RCONSessionState RCONSession::getState() const { return m_state; } @@ -351,42 +350,42 @@ // ------------------------------------------------------------------------------------------------- // -int RCONSession::num_admins() const +int RCONSession::getAdminCount() const { - return m_numAdmins; + return m_adminCount; } // ------------------------------------------------------------------------------------------------- // -const String& RCONSession::level() const +const String& RCONSession::getLevel() const { return m_level; } // ------------------------------------------------------------------------------------------------- // -void RCONSession::request_tab_complete (const String& part) +void RCONSession::requestTabCompletion(const String& part) { if (m_serverProtocol >= 4) { Bytestream packet; - packet.write_byte (CLRC_TABCOMPLETE); - packet.write_string (part); - send (packet); - bump_last_ping(); + packet.write_byte(CLRC_TABCOMPLETE); + packet.write_string(part); + send(packet); + bumpLastPing(); m_lastTabComplete = part; } else { - m_interface->print ("This server does not support tab-completion\n", m_serverProtocol); + m_interface->print("This server does not support tab-completion\n", m_serverProtocol); } } // ------------------------------------------------------------------------------------------------- // -void RCONSession::set_interface (Interface* iface) +void RCONSession::setInterface(Interface* interface) { - m_interface = iface; + m_interface = interface; } END_ZFC_NAMESPACE