Merged with default protocol5

Wed, 20 Jul 2016 18:24:13 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Wed, 20 Jul 2016 18:24:13 +0300
branch
protocol5
changeset 153
82aac80a2f1d
parent 151
267ef11bd3ba (diff)
parent 152
6be21be4bea1 (current diff)
child 155
9f71f854474a

Merged with default

sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
sources/network/udpsocket.cpp file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Wed Jul 20 18:20:20 2016 +0300
+++ b/sources/interface.cpp	Wed Jul 20 18:24:13 2016 +0300
@@ -1094,6 +1094,13 @@
 		endwin();
 		throw Exitception();
 	}
+	else if (command == "watch")
+	{
+		if (not args.is_empty())
+			m_session.request_watch(args);
+		else
+			printError("No CVars to watch.\n");
+	}
 	else
 		printError("Unknown command: %s\n", command.chars());
 }
--- a/sources/network/rconsession.cpp	Wed Jul 20 18:20:20 2016 +0300
+++ b/sources/network/rconsession.cpp	Wed Jul 20 18:24:13 2016 +0300
@@ -128,6 +128,10 @@
 
 	try
 	{
+		int32_t header = datagram.message.read_long();
+		int32_t sequenceNumber = (header != 0) ? datagram.message.read_long() : 0;
+		m_interface->print("Recieved packet with header 0x%x and sequence number #%d\n", header, sequenceNumber);
+
 		while (datagram.message.bytes_left() > 0)
 		{
 			int header = datagram.message.read_byte();
@@ -183,6 +187,10 @@
 				}
 
 				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");
+				m_interface->print ("Watch requested.\n");
 				break;
 
 			case SVRC_UPDATE:
@@ -222,6 +230,40 @@
 					}
 				}
 				break;
+
+			case SVRC_WATCHINGCVAR:
+				m_interface->print ("You are now watching %s\n", datagram.message.read_string().chars());
+				m_interface->print ("Its value is: %s\n", datagram.message.read_string().chars());
+				break;
+
+			case SVRC_ALREADYWATCHINGCVAR:
+				m_interface->print ("You are already watching %s\n", datagram.message.read_string().chars());
+				break;
+
+			case SVRC_WATCHCVARNOTFOUND:
+				m_interface->print ("CVar %s not found\n", datagram.message.read_string().chars());
+				break;
+
+			case SVRC_CVARCHANGED:
+				{
+					String name = datagram.message.read_string();
+					String value = datagram.message.read_string();
+					m_interface->print ("The value of CVar %s", name.chars());
+					m_interface->print (" is now %s\n", value.chars());
+
+					// If sv_hostname changes, update the titlebar
+					if (name == "sv_hostname")
+					{
+						m_hostname = value;
+						m_interface->setTitle(m_hostname);
+					}
+				}
+				break;
+
+			case SVRC_YOUREDISCONNECTED:
+				m_interface->print ("You have been disconnected: %s\n", datagram.message.read_string().chars());
+				m_interface->disconnected();
+				break;
 			}
 		}
 	}
@@ -389,4 +431,27 @@
 	m_interface = iface;
 }
 
+// -------------------------------------------------------------------------------------------------
+//
+void RCONSession::request_watch (const String& cvar)
+{
+	StringList cvars;
+	cvars.append(cvar);
+	request_watch(cvars);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+void RCONSession::request_watch (const StringList& cvars)
+{
+	Bytestream packet;
+	packet.write_byte(CLRC_WATCHCVAR);
+
+	for (int i = 0; i < cvars.size(); ++i)
+		packet.write_string(cvars[i].normalized());
+
+	packet.write_string("");
+	send(packet);
+}
+
 END_ZFC_NAMESPACE
--- a/sources/network/rconsession.h	Wed Jul 20 18:20:20 2016 +0300
+++ b/sources/network/rconsession.h	Wed Jul 20 18:24:13 2016 +0300
@@ -54,6 +54,11 @@
 	SVRC_UPDATE,
 	SVRC_TABCOMPLETE,
 	SVRC_TOOMANYTABCOMPLETES,
+	SVRC_WATCHINGCVAR,
+	SVRC_ALREADYWATCHINGCVAR,
+	SVRC_WATCHCVARNOTFOUND,
+	SVRC_CVARCHANGED,
+	SVRC_YOUREDISCONNECTED,
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -66,6 +71,7 @@
 	CLRC_PONG,
 	CLRC_DISCONNECT,
 	CLRC_TABCOMPLETE,
+	CLRC_WATCHCVAR,
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -98,7 +104,7 @@
 	const IPAddress& address() const;
 	void connect (IPAddress address);
 	void disconnect();
-	void handle_packet (Datagram &packet);
+	void handle_packet (Datagram& datagram);
 	void process_server_updates (Bytestream& packet);
 	int num_admins() const;
 	void send (const Bytestream& packet);
@@ -114,6 +120,8 @@
 	bool is_active() const;
 	void request_tab_complete (const String& part);
 	void set_interface (class Interface* iface);
+	void request_watch (const String& cvar);
+	void request_watch (const StringList& cvars);
 
 private:
 	RCONSessionState m_state;
--- a/sources/network/udpsocket.cpp	Wed Jul 20 18:20:20 2016 +0300
+++ b/sources/network/udpsocket.cpp	Wed Jul 20 18:24:13 2016 +0300
@@ -129,6 +129,12 @@
 		return false;
 	}
 
+	if (length < 4)
+	{
+		m_error = "The server sent a too short packet";
+		return false;
+	}
+
 	unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
 	int decodedLength = sizeof decodedPacket;
 	HUFFMAN_Decode (reinterpret_cast<unsigned char*> (HuffmanBuffer),

mercurial