- rcon session now works! woo!

Sat, 13 Dec 2014 04:32:15 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 13 Dec 2014 04:32:15 +0200
changeset 12
8d0d1b368de0
parent 11
cffa2777d917
child 13
09dcaeaa216b

- rcon session now works! woo!

sources/mystring.cpp file | annotate | diff | comparison | revisions
sources/mystring.h file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/mystring.cpp	Fri Dec 12 01:37:04 2014 +0200
+++ b/sources/mystring.cpp	Sat Dec 13 04:32:15 2014 +0200
@@ -489,3 +489,23 @@
 	checksum[sizeof checksum - 1] = '\0';
 	return String (checksum);
 }
+
+// -------------------------------------------------------------------------------------------------
+//
+METHOD
+String::normalize (int (*filter)(int)) -> void
+{
+	int a = 0;
+	int b = length() - 1;
+
+	while ((*filter) (m_string[a]) and a != b)
+		++a;
+
+	while ((*filter) (m_string[b]) and a != b)
+		--b;
+
+	if (a == b)
+		m_string = "";
+	else if (a != 0 or b != length() - 1)
+		m_string = m_string.substr (a, b - a + 1);
+}
--- a/sources/mystring.h	Fri Dec 12 01:37:04 2014 +0200
+++ b/sources/mystring.h	Sat Dec 13 04:32:15 2014 +0200
@@ -81,6 +81,7 @@
 	       METHOD md5() const -> String;
 	       METHOD mid (long a, long b = -1) const -> String;
 	inline METHOD modify_index (int& a) -> void;
+	       METHOD normalize (int (*filter)(int) = &std::isspace) -> void;
 	inline METHOD prepend (String a) -> void;
 	inline METHOD remove (int pos) -> void;
 	inline METHOD remove (int pos, int len) -> void;
--- 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;
 	}
 }
 
--- a/sources/network/rconsession.h	Fri Dec 12 01:37:04 2014 +0200
+++ b/sources/network/rconsession.h	Sat Dec 13 04:32:15 2014 +0200
@@ -94,6 +94,7 @@
 	METHOD connect (IPAddress address) -> void;
 	METHOD disconnect() -> void;
 	METHOD handle_packet (Bytestream& packet, const IPAddress& from) -> void;
+	METHOD process_server_updates (Bytestream& packet) -> void;
 	METHOD send (const Bytestream& packet) -> void;
 	METHOD send_hello() -> void;
 	METHOD send_password() -> void;
@@ -109,4 +110,6 @@
 	time_t m_lastPing;
 	String m_password;
 	String m_salt;
+	int m_serverProtocol;
+	String m_hostname;
 };

mercurial