# HG changeset patch # User Teemu Piippo # Date 1469026600 -10800 # Node ID 37db42ad451ab154ee07353507adb33572283840 # Parent d9073c13dc98d1d91a5576452042a7c4eb1e3751# Parent 7643c21d546b8a729344d62fee36a79c9077a012 Merged with default diff -r 7643c21d546b -r 37db42ad451a sources/coloredline.cpp diff -r 7643c21d546b -r 37db42ad451a sources/coloredline.h diff -r 7643c21d546b -r 37db42ad451a sources/interface.cpp --- a/sources/interface.cpp Wed Jul 20 17:53:13 2016 +0300 +++ b/sources/interface.cpp Wed Jul 20 17:56:40 2016 +0300 @@ -1094,6 +1094,13 @@ endwin(); throw Exitception(); } + else if (command == "watch") + { + if (not args.is_empty()) + m_session.request_watch(args); + else + print_error("No CVars to watch.\n"); + } else printError("Unknown command: %s\n", command.chars()); } diff -r 7643c21d546b -r 37db42ad451a sources/interface.h diff -r 7643c21d546b -r 37db42ad451a sources/mystring.cpp diff -r 7643c21d546b -r 37db42ad451a sources/mystring.h diff -r 7643c21d546b -r 37db42ad451a sources/network/rconsession.cpp --- a/sources/network/rconsession.cpp Wed Jul 20 17:53:13 2016 +0300 +++ b/sources/network/rconsession.cpp Wed Jul 20 17:56:40 2016 +0300 @@ -116,18 +116,23 @@ } for (Datagram datagram; m_socket.read (datagram);) - handle_packet (datagram.data, datagram.from); + handle_packet (datagram); } // ------------------------------------------------------------------------------------------------- // -void RCONSession::handle_packet (Bytestream& packet, const IPAddress& from) +void RCONSession::handle_packet (Datagram& datagram) { - if (from != m_address) + if (datagram.from != m_address) return; try { + Bytestream& packet = datagram.data; + int32_t header = packet.read_long(); + int32_t sequenceNumber = (header != 0) ? packet.read_long() : 0; + m_interface->print("Recieved packet with header 0x%x and sequence number #%d\n", header, sequenceNumber); + while (packet.bytes_left() > 0) { int header = packet.read_byte(); @@ -183,6 +188,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 +231,40 @@ } } break; + + case SVRC_WATCHINGCVAR: + m_interface->print ("You are now watching %s\n", packet.read_string().chars()); + m_interface->print ("Its value is: %s\n", packet.read_string().chars()); + break; + + case SVRC_ALREADYWATCHINGCVAR: + m_interface->print ("You are already watching %s\n", packet.read_string().chars()); + break; + + case SVRC_WATCHCVARNOTFOUND: + m_interface->print ("CVar %s not found\n", packet.read_string().chars()); + break; + + case SVRC_CVARCHANGED: + { + String name = packet.read_string(); + String value = packet.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->set_title(m_hostname); + } + } + break; + + case SVRC_YOUREDISCONNECTED: + m_interface->print ("You have been disconnected: %s\n", packet.read_string().chars()); + m_interface->disconnected(); + break; } } } @@ -389,4 +432,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 diff -r 7643c21d546b -r 37db42ad451a sources/network/rconsession.h --- a/sources/network/rconsession.h Wed Jul 20 17:53:13 2016 +0300 +++ b/sources/network/rconsession.h Wed Jul 20 17:56:40 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 (Bytestream& packet, const IPAddress& from); + 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; @@ -130,4 +138,4 @@ class Interface* m_interface; }; -END_ZFC_NAMESPACE \ No newline at end of file +END_ZFC_NAMESPACE diff -r 7643c21d546b -r 37db42ad451a sources/network/udpsocket.cpp --- a/sources/network/udpsocket.cpp Wed Jul 20 17:53:13 2016 +0300 +++ b/sources/network/udpsocket.cpp Wed Jul 20 17:56:40 2016 +0300 @@ -129,10 +129,15 @@ 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 (HuffmanBuffer), - decodedPacket, length, &decodedLength); + HUFFMAN_Decode (reinterpret_cast(HuffmanBuffer), decodedPacket, length, &decodedLength); datagram.from.host = ntohl (claddr.sin_addr.s_addr); datagram.from.port = ntohs (claddr.sin_port); datagram.data = Bytestream (decodedPacket, decodedLength);