sources/network/rconsession.cpp

changeset 157
42bb29924218
parent 156
ce66d7e374bf
child 158
de7574d292ad
--- a/sources/network/rconsession.cpp	Wed Jul 20 22:56:16 2016 +0300
+++ b/sources/network/rconsession.cpp	Fri Jul 22 17:50:00 2016 +0300
@@ -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,7 +79,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-void RCONSession::send(const Bytestream& packet)
+void RCONSession::send(const Vector<unsigned char>& packet)
 {
 	m_socket.send(m_address, packet);
 }
@@ -108,9 +106,7 @@
 		}
 		else if (m_state == RCON_CONNECTED and m_lastPing + 5 < now)
 		{
-			Bytestream packet;
-			packet.write_byte(CLRC_PONG);
-			send(packet);
+			send({CLRC_PONG});
 			bumpLastPing();
 		}
 	}
@@ -126,11 +122,13 @@
 	if (datagram.address != m_address)
 		return;
 
+	Bytestream stream(datagram.message);
+
 	try
 	{
-		while (datagram.message.bytes_left() > 0)
+		while (stream.bytesLeft() > 0)
 		{
-			int header = datagram.message.read_byte();
+			int header = stream.readByte();
 
 			switch (ServerResponse(header))
 			{
@@ -145,7 +143,7 @@
 				break;
 
 			case SVRC_SALT:
-				m_salt = datagram.message.read_string();
+				m_salt = stream.readString();
 				m_state = RCON_AUTHENTICATING;
 				sendPassword();
 				break;
@@ -157,7 +155,7 @@
 
 			case SVRC_MESSAGE:
 				{
-					String message = datagram.message.read_string();
+					String message = stream.readString();
 					message.normalize();
 					m_interface->printText("%s\n", message.chars());
 				}
@@ -165,19 +163,19 @@
 
 			case SVRC_LOGGEDIN:
 				m_interface->print("Login successful!\n");
-				m_serverProtocol = datagram.message.read_byte();
-				m_hostname = datagram.message.read_string();
+				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)
-					processServerUpdates(datagram.message);
+				for (int i = stream.readByte(); i > 0; --i)
+					processServerUpdates(stream);
 
 				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());
 				}
@@ -186,12 +184,12 @@
 				break;
 
 			case SVRC_UPDATE:
-				processServerUpdates(datagram.message);
+				processServerUpdates(stream);
 				break;
 
 			case SVRC_TOOMANYTABCOMPLETES:
 				{
-					unsigned int numCompletions = datagram.message.read_short();
+					unsigned int numCompletions = stream.readShort();
 					m_interface->print("%d completions for '%s'.\n",
 						int(numCompletions), m_lastTabComplete.chars());
 				}
@@ -200,10 +198,10 @@
 			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)
 					{
@@ -233,7 +231,7 @@
 
 void RCONSession::processServerUpdates(Bytestream& packet)
 {
-	int header = packet.read_byte();
+	int header = packet.readByte();
 
 	switch (RCONUpdateType(header))
 	{
@@ -241,20 +239,20 @@
 		{
 			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);
 		}
 		break;
 
 	case SVRCU_ADMINCOUNT:
-		m_adminCount = 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;
 
@@ -276,10 +274,7 @@
 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);
+	send({CLRC_BEGINCONNECTION, RCON_PROTOCOL_VERSION});
 	bumpLastPing();
 }
 
@@ -288,10 +283,11 @@
 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);
+	Vector<unsigned char> message;
+	Bytestream stream(message);
+	stream.writeByte(CLRC_PASSWORD);
+	stream.writeString((m_salt + m_password).md5());
+	send(message);
 	bumpLastPing();
 }
 
@@ -321,15 +317,16 @@
 // -------------------------------------------------------------------------------------------------
 // Returns true if the message was successfully sent.
 //
-bool RCONSession::sendCommand(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);
+	Vector<unsigned char> message;
+	Bytestream stream(message);
+	stream.writeByte(CLRC_COMMAND);
+	stream.writeString(commandString);
+	send(message);
 	bumpLastPing();
 	return true;
 }
@@ -368,10 +365,11 @@
 {
 	if (m_serverProtocol >= 4)
 	{
-		Bytestream packet;
-		packet.write_byte(CLRC_TABCOMPLETE);
-		packet.write_string(part);
-		send(packet);
+		Vector<unsigned char> message;
+		Bytestream stream(message);
+		stream.writeByte(CLRC_TABCOMPLETE);
+		stream.writeString(part);
+		send(message);
 		bumpLastPing();
 		m_lastTabComplete = part;
 	}

mercurial