sources/network/rconsession.cpp

changeset 12
8d0d1b368de0
parent 11
cffa2777d917
child 13
09dcaeaa216b
--- a/sources/network/rconsession.cpp	Fri Dec 12 01:37:04 2014 +0200
+++ b/sources/network/rconsession.cpp	Sat Dec 13 04:32:15 2014 +0200
@@ -9,7 +9,7 @@
 	if (not m_socket.set_blocking (false))
 	{
 		// TODO: find a better way to deal with errors
-		fprintf (stderr, "unable to set socket as non-blocking: %s\n",
+		print ("unable to set socket as non-blocking: %s\n",
 			m_socket.error_string().chars());
 	}
 }
@@ -93,16 +93,17 @@
 	while (packet.bytes_left() > 0)
 	{
 		int header = packet.read_byte (&ok);
+		print ("Recieved packet with header %1\n", header);
 
 		switch (ServerResponse (header))
 		{
 		case SVRC_OLDPROTOCOL:
-			fprintf (stderr, "wrong version\n");
+			print ("wrong version\n");
 			m_state = RCON_DISCONNECTED;
 			break;
 
 		case SVRC_BANNED:
-			fprintf (stderr, "you're banned\n");
+			print ("you're banned\n");
 			m_state = RCON_DISCONNECTED;
 			break;
 
@@ -115,52 +116,69 @@
 			}
 			break;
 
-		case SVRC_LOGGEDIN:
-			fprintf (stderr, "login successful\n");
-			m_state = RCON_CONNECTED;
-			break;
-
 		case SVRC_INVALIDPASSWORD:
-			fprintf (stderr, "bad password\n");
+			print ("bad password\n");
 			m_state = RCON_DISCONNECTED;
 			break;
 
 		case SVRC_MESSAGE:
 			{
 				String message = packet.read_string();
-				print_to (stderr, "message: %1\n", message);
+				if (message.ends_with ("\n"))
+					message.remove_from_end (1);
+				print ("message: %1\n", message);
 			}
 			break;
 
+		case SVRC_LOGGEDIN:
+			print ("login successful\n");
+			m_serverProtocol = packet.read_byte();
+			m_hostname = packet.read_string();
+			m_state = RCON_CONNECTED;
+
+			for (int i = packet.read_byte(); i > 0; --i)
+				process_server_updates (packet);
+
+			for (int i = packet.read_byte(); i > 0; --i)
+			{
+				String message = packet.read_string();
+				message.normalize();
+				print ("--- %1\n", message);
+			}
+
+			break;
+
 		case SVRC_UPDATE:
-			switch (RCONUpdateType (packet.read_byte (&ok)))
-			{
-			case SVRCU_PLAYERDATA:
-				{
-					int numplayers = packet.read_byte (&ok);
-					Vector<String> players;
-
-					while (numplayers--)
-						players << packet.read_string (&ok);
-
-					print_to (stderr, "players: %1\n", players);
-				}
-				break;
-
-			case SVRCU_ADMINCOUNT:
-				print_to (stderr, "num admins: %1\n", packet.read_byte (&ok));
-				break;
-
-			case SVRCU_MAP:
-				print_to (stderr, "new map: %1\n", packet.read_string (&ok));
-				break;
-			}
-
+			process_server_updates (packet);
 			break;
 		}
 
 		if (not ok)
-			print_to (stderr, "error while reading packet\n");
+			print ("error while reading packet\n");
+	}
+}
+
+METHOD
+RCONSession::process_server_updates (Bytestream& packet) -> void
+{
+	switch (RCONUpdateType (packet.read_byte()))
+	{
+	case SVRCU_PLAYERDATA:
+		{
+			Vector<String> players;
+			for (int i = packet.read_byte(); i > 0; --i)
+				players << packet.read_string();
+			print ("players: %1\n", players);
+		}
+		break;
+
+	case SVRCU_ADMINCOUNT:
+		print ("num admins: %d1\n", packet.read_byte());
+		break;
+
+	case SVRCU_MAP:
+		print ("new map: %1\n", packet.read_string());
+		break;
 	}
 }
 

mercurial