Wed, 27 Jan 2021 14:05:39 +0200
renamed String and StringList uses
/* Copyright 2014 - 2016 Teemu Piippo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include <time.h> #include "rconsession.h" #include "../interface.h" #include "../md5.h" BEGIN_ZFC_NAMESPACE // ------------------------------------------------------------------------------------------------- // RCONSession::RCONSession() : m_state(RCON_DISCONNECTED), m_lastPing(0), m_adminCount(0), m_interface(nullptr) { if (not m_socket.set_blocking(false)) { fprintf(stderr, "unable to set socket as non-blocking: %s\n", m_socket.error_string().data()); exit(EXIT_FAILURE); } } // ------------------------------------------------------------------------------------------------- // RCONSession::~RCONSession() {} // ------------------------------------------------------------------------------------------------- // void RCONSession::connect(IPAddress address) { m_address = address; m_state = RCON_CONNECTING; m_interface->updateStatusBar(); sendHello(); } // ------------------------------------------------------------------------------------------------- // void RCONSession::disconnect() { if (m_state > RCON_CONNECTING) { // Say goodbye to remote send({CLRC_DISCONNECT}); m_interface->disconnected(); } m_state = RCON_DISCONNECTED; } // ------------------------------------------------------------------------------------------------- // void RCONSession::send(const ByteArray& packet) { m_socket.send(m_address, packet); } // ------------------------------------------------------------------------------------------------- // void RCONSession::tick() { if (m_state == RCON_DISCONNECTED) return; time_t now; time(&now); if (m_lastPing < now) { if (m_state == RCON_CONNECTING) { sendHello(); } else if (m_state == RCON_AUTHENTICATING) { sendPassword(); } else if (m_state == RCON_CONNECTED and m_lastPing + 5 < now) { send({CLRC_PONG}); bumpLastPing(); } } for (Datagram datagram; m_socket.read(datagram);) { // Only process packets that originate from the game server. if (datagram.address == m_address) handlePacket(datagram.message); } } // ------------------------------------------------------------------------------------------------- // void RCONSession::handlePacket(ByteArray& message) { Bytestream stream(message); try { while (stream.bytesLeft() > 0) { int header = stream.readByte(); switch (ServerResponse(header)) { case SVRC_OLDPROTOCOL: 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_state = RCON_DISCONNECTED; break; case SVRC_SALT: m_salt = stream.readString(); m_state = RCON_AUTHENTICATING; sendPassword(); break; case SVRC_INVALIDPASSWORD: m_interface->printError("Login failed.\n"); m_state = RCON_DISCONNECTED; break; case SVRC_MESSAGE: { std::string message = stream.readString(); normalize(message); m_interface->printText("%s\n", message.data()); } break; case SVRC_LOGGEDIN: 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 = stream.readByte(); i > 0; --i) processServerUpdates(stream); m_interface->print("Previous messages:\n"); for (int i = stream.readByte(); i > 0; --i) { std::string message = stream.readString(); normalize(message); m_interface->printText("--- %s\n", message.data()); } m_interface->print("End of previous messages.\n"); break; case SVRC_UPDATE: processServerUpdates(stream); break; case SVRC_TOOMANYTABCOMPLETES: { unsigned int numCompletions = stream.readShort(); m_interface->print("%d completions for '%s'.\n", int(numCompletions), m_lastTabComplete.data()); } break; case SVRC_TABCOMPLETE: { std::vector<std::string> completes; completes.resize(stream.readByte()); for (std::string& completion : completes) completion = stream.readString(); if (completes.size() == 1) { m_interface->tabComplete(m_lastTabComplete, completes[0]); } else if (completes.size() > 0) { m_interface->print("Completions for '%s':\n", m_lastTabComplete.data()); for (int i : range(0, static_cast<int>(completes.size()), 8)) { const int end = min(i + 8, static_cast<int>(completes.size())); std::vector<std::string> splices = splice(completes, i, end); m_interface->print("- %s\n", join_string_list(splices, ", ").data()); } } } break; } } } catch (std::exception& e) { m_interface->printWarning("Couldn't process packet: %s\n", e.what()); m_interface->printWarning("Packet contents was: %s\n", quote(message).data()); m_interface->printWarning("Stream position in payload was: %d\n", stream.position()); } } void RCONSession::processServerUpdates(Bytestream& packet) { int header = packet.readByte(); switch (RCONUpdateType(header)) { case SVRCU_PLAYERDATA: { std::vector<std::string> players; for (int i = packet.readByte(); i > 0; --i) players.push_back(packet.readString()); m_interface->setPlayerNames(players); } break; case SVRCU_ADMINCOUNT: m_adminCount = packet.readByte(); m_interface->updateStatusBar(); break; case SVRCU_MAP: m_level = packet.readString(); m_interface->updateStatusBar(); break; default: m_interface->printWarning("Unknown server update type: %d\n", header); break; } } // ------------------------------------------------------------------------------------------------- // UDPSocket* RCONSession::getSocket() { return &m_socket; } // ------------------------------------------------------------------------------------------------- // void RCONSession::sendHello() { m_interface->print("Connecting to %s...\n", m_address.to_string(IPAddress::WITH_PORT).data()); send({CLRC_BEGINCONNECTION, RCON_PROTOCOL_VERSION}); bumpLastPing(); } // ------------------------------------------------------------------------------------------------- // void RCONSession::sendPassword() { m_interface->print("Authenticating...\n"); ByteArray message; Bytestream stream(message); stream.writeByte(CLRC_PASSWORD); stream.writeString(md5((m_salt + m_password).data())); send(message); bumpLastPing(); } // ------------------------------------------------------------------------------------------------- // void RCONSession::setPassword(const std::string& password) { m_password = password; } // ------------------------------------------------------------------------------------------------- // void RCONSession::bumpLastPing() { time_t now; time(&now); m_lastPing = now; } // ------------------------------------------------------------------------------------------------- // bool RCONSession::isActive() const { return getState() != RCON_DISCONNECTED; } // ------------------------------------------------------------------------------------------------- // Returns true if the message was successfully sent. // bool RCONSession::sendCommand(const std::string& commandString) { if (m_state != RCON_CONNECTED or commandString.empty()) return false; ByteArray message; Bytestream stream(message); stream.writeByte(CLRC_COMMAND); stream.writeString(commandString); send(message); bumpLastPing(); return true; } // ------------------------------------------------------------------------------------------------- // RCONSessionState RCONSession::getState() const { return m_state; } // ------------------------------------------------------------------------------------------------- // const IPAddress& RCONSession::address() const { return m_address; } // ------------------------------------------------------------------------------------------------- // int RCONSession::getAdminCount() const { return m_adminCount; } // ------------------------------------------------------------------------------------------------- // const std::string& RCONSession::getLevel() const { return m_level; } // ------------------------------------------------------------------------------------------------- // void RCONSession::requestTabCompletion(const std::string& part) { if (m_serverProtocol >= 4) { 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); } } // ------------------------------------------------------------------------------------------------- // void RCONSession::setInterface(Interface* interface) { m_interface = interface; } END_ZFC_NAMESPACE