--- a/sources/network/rconsession.cpp Wed Jul 20 18:31:19 2016 +0300 +++ b/sources/network/rconsession.cpp Fri Jul 22 17:59:55 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(); } // ------------------------------------------------------------------------------------------------- @@ -70,9 +70,7 @@ if (m_state > RCON_CONNECTING) { // Say goodbye to remote - Bytestream packet; - packet.write_byte (CLRC_DISCONNECT); - this->send (packet); + send({CLRC_DISCONNECT}); m_interface->disconnected(); } @@ -81,9 +79,9 @@ // ------------------------------------------------------------------------------------------------- // -void RCONSession::send (const Bytestream& packet) +void RCONSession::send(const ByteArray& packet) { - m_socket.send (m_address, packet); + m_socket.send(m_address, packet); } // ------------------------------------------------------------------------------------------------- @@ -94,38 +92,38 @@ 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(); + send({CLRC_PONG}); + 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; + Bytestream stream(datagram.message); + try { int32_t header = datagram.message.read_long(); @@ -134,59 +132,59 @@ while (datagram.message.bytes_left() > 0) { - int header = datagram.message.read_byte(); + int header = stream.readByte(); - 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_salt = stream.readString(); 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; case SVRC_MESSAGE: { - String message = datagram.message.read_string(); + String message = stream.readString(); 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_serverProtocol = datagram.message.read_byte(); - m_hostname = datagram.message.read_string(); - m_interface->setTitle (m_hostname); + m_interface->print("Login successful!\n"); + m_serverProtocol = stream.readByte(); + m_hostname = stream.readString(); + m_interface->setTitle(m_hostname); m_state = RCON_CONNECTED; - for (int i = datagram.message.read_byte(); i > 0; --i) - process_server_updates (datagram.message); + for (int i = stream.readByte(); i > 0; --i) + processServerUpdates(stream); - m_interface->print ("Previous messages:\n"); + m_interface->print("Previous messages:\n"); - for (int i = datagram.message.read_byte(); i > 0; --i) + for (int i = stream.readByte(); i > 0; --i) { - String message = datagram.message.read_string(); + String message = stream.readString(); 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"); // Watch sv_hostname so that we can update the titlebar when it changes. request_watch("sv_hostname"); @@ -194,38 +192,38 @@ break; case SVRC_UPDATE: - process_server_updates (datagram.message); + processServerUpdates(stream); break; case SVRC_TOOMANYTABCOMPLETES: { - unsigned int numCompletions = datagram.message.read_short(); - m_interface->print ("%d completions for '%s'.\n", - int (numCompletions), m_lastTabComplete.chars()); + unsigned int numCompletions = stream.readShort(); + m_interface->print("%d completions for '%s'.\n", + int(numCompletions), m_lastTabComplete.chars()); } break; case SVRC_TABCOMPLETE: { StringList completes; - completes.resize(datagram.message.read_byte()); + completes.resize(stream.readByte()); for (String& completion : completes) - completion = datagram.message.read_string(); + completion = stream.readString(); 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()); } } } @@ -269,117 +267,115 @@ } 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(); + int header = packet.readByte(); - 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()); + for (int i = packet.readByte(); i > 0; --i) + players.append(packet.readString()); - m_interface->setPlayerNames (players); + m_interface->setPlayerNames(players); } break; case SVRCU_ADMINCOUNT: - m_numAdmins = packet.read_byte(); + m_adminCount = packet.readByte(); m_interface->updateStatusBar(); break; case SVRCU_MAP: - m_level = packet.read_string(); + m_level = packet.readString(); m_interface->updateStatusBar(); 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()); - Bytestream packet; - packet.write_byte (CLRC_BEGINCONNECTION); - packet.write_byte (RCON_PROTOCOL_VERSION); - send (packet); - bump_last_ping(); + m_interface->print("Connecting to %s...\n", m_address.to_string(IPAddress::WITH_PORT).chars()); + send({CLRC_BEGINCONNECTION, RCON_PROTOCOL_VERSION}); + bumpLastPing(); } // ------------------------------------------------------------------------------------------------- // -void RCONSession::send_password() +void RCONSession::sendPassword() { - 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(); + m_interface->print("Authenticating...\n"); + ByteArray message; + Bytestream stream(message); + stream.writeByte(CLRC_PASSWORD); + stream.writeString((m_salt + m_password).md5()); + send(message); + 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& commandString) { - if (m_state != RCON_CONNECTED or message.isEmpty()) + if (m_state != RCON_CONNECTED or commandString.isEmpty()) return false; - Bytestream packet; - packet.write_byte (CLRC_COMMAND); - packet.write_string (message); - send (packet); - bump_last_ping(); + ByteArray message; + Bytestream stream(message); + stream.writeByte(CLRC_COMMAND); + stream.writeString(commandString); + send(message); + bumpLastPing(); return true; } // ------------------------------------------------------------------------------------------------- // -RCONSessionState RCONSession::state() const +RCONSessionState RCONSession::getState() const { return m_state; } @@ -393,42 +389,43 @@ // ------------------------------------------------------------------------------------------------- // -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(); + ByteArray message; + Bytestream stream(message); + stream.writeByte(CLRC_TABCOMPLETE); + stream.writeString(part); + send(message); + 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; } // -------------------------------------------------------------------------------------------------