sources/network/rconsession.cpp

Sun, 14 Dec 2014 20:47:44 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 14 Dec 2014 20:47:44 +0200
changeset 23
f7221183a994
parent 22
77d02446edf0
child 24
e651d02802c0
permissions
-rw-r--r--

- the prompt is now colored
- the titlebar now changes to the server hostname upon connection

10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #include "rconsession.h"
23
f7221183a994 - the prompt is now colored
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
2 #include "../interface.h"
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3
15
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
4 RCONSession* g_rconSession = nullptr;
14
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
5
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 RCONSession::RCONSession() :
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 m_state (RCON_DISCONNECTED),
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 m_lastPing (0)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 {
15
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
12 if (g_rconSession != NULL)
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
13 {
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
14 g_rconSession->disconnect();
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
15 delete g_rconSession;
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
16 }
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
17
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
18 g_rconSession = this;
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
19
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 if (not m_socket.set_blocking (false))
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 // TODO: find a better way to deal with errors
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
23 print ("unable to set socket as non-blocking: %s\n",
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 m_socket.error_string().chars());
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 //
15
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
30 RCONSession::~RCONSession()
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
31 {
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
32 if (g_rconSession == this)
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
33 g_rconSession = nullptr;
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
34 }
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 RCONSession::connect (IPAddress address) -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 m_address = address;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 m_state = RCON_CONNECTING;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 RCONSession::disconnect() -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 if (m_state == RCON_CONNECTED)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // Say goodbye to remote
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 Bytestream packet;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 packet.write_byte (CLRC_DISCONNECT);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 this->send (packet);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 m_state = RCON_DISCONNECTED;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 RCONSession::send (const Bytestream& packet) -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 m_socket.send (m_address, packet);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 RCONSession::tick() -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 time_t now;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 time (&now);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 if (m_lastPing < now)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 if (m_state == RCON_CONNECTING)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 send_hello();
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 else if (m_state == RCON_AUTHENTICATING)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 send_password();
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 else if (m_state == RCON_CONNECTED and m_lastPing + 5 < now)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 Bytestream packet;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 packet.write_byte (CLRC_PONG);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 send (packet);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 bump_last_ping();
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 for (Datagram datagram; m_socket.read (datagram);)
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 handle_packet (datagram.data, datagram.from);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 RCONSession::handle_packet (Bytestream& packet, const IPAddress& from) -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 {
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
105 try
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
106 {
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
107 while (packet.bytes_left() > 0)
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 {
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
109 int header = packet.read_byte();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
110
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
111 switch (ServerResponse (header))
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
112 {
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
113 case SVRC_OLDPROTOCOL:
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
114 print ("wrong version\n");
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
115 m_state = RCON_DISCONNECTED;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
116 break;
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
118 case SVRC_BANNED:
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
119 print ("you're banned\n");
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
120 m_state = RCON_DISCONNECTED;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
121 break;
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
123 case SVRC_SALT:
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
124 {
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
125 String salt = packet.read_string();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
126 m_salt = salt;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
127 m_state = RCON_AUTHENTICATING;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
128 send_password();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
129 }
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
130 break;
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
132 case SVRC_INVALIDPASSWORD:
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
133 print ("bad password\n");
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
134 m_state = RCON_DISCONNECTED;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
135 break;
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
137 case SVRC_MESSAGE:
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
138 {
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
139 String message = packet.read_string();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
140 if (message.ends_with ("\n"))
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
141 message.remove_from_end (1);
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
142 print ("%1\n", message);
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
143 }
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
144 break;
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
146 case SVRC_LOGGEDIN:
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
147 print ("Login successful!\n");
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
148 m_serverProtocol = packet.read_byte();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
149 m_hostname = packet.read_string();
23
f7221183a994 - the prompt is now colored
Teemu Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
150 Interface::set_title (m_hostname);
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
151 m_state = RCON_CONNECTED;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
152
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
153 for (int i = packet.read_byte(); i > 0; --i)
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
154 process_server_updates (packet);
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
155
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
156 print ("Previous messages:\n");
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
157 for (int i = packet.read_byte(); i > 0; --i)
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
158 {
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
159 String message = packet.read_string();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
160 message.normalize();
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
161 print ("--- %1\n", message);
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
162 }
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
163 print ("End of previous messages.\n");
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
164 break;
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
165
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
166 case SVRC_UPDATE:
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
167 process_server_updates (packet);
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
168 break;
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
169 }
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 }
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
171 }
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
172 catch (std::exception& e)
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
173 {
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 12
diff changeset
174 print ("error while reading packet: %1\n", e.what());
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
175 }
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
176 }
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
177
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
178 METHOD
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
179 RCONSession::process_server_updates (Bytestream& packet) -> void
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
180 {
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
181 switch (RCONUpdateType (packet.read_byte()))
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
182 {
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
183 case SVRCU_PLAYERDATA:
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
184 {
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
185 Vector<String> players;
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
186 for (int i = packet.read_byte(); i > 0; --i)
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
187 players << packet.read_string();
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
188 print ("Players: %1\n", players);
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
189 }
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
190 break;
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
191
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
192 case SVRCU_ADMINCOUNT:
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
193 print ("Admin count: %d1\n", packet.read_byte());
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
194 break;
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
195
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
196 case SVRCU_MAP:
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
197 print ("New level: %1\n", packet.read_string());
12
8d0d1b368de0 - rcon session now works! woo!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
198 break;
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 RCONSession::socket() -> UDPSocket*
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 return &m_socket;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 RCONSession::send_hello() -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 {
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
215 print ("Connecting to %1...\n", m_address.to_string (IP_WITH_PORT));
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 Bytestream packet;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 packet.write_byte (CLRC_BEGINCONNECTION);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 packet.write_byte (RCON_PROTOCOL_VERSION);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 send (packet);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 bump_last_ping();
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 RCONSession::send_password() -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 {
21
12e4ff8bb471 - cleaned up rcon session messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 17
diff changeset
228 print ("Authenticating...\n");
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 Bytestream packet;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 packet.write_byte (CLRC_PASSWORD);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 packet.write_string ((m_salt + m_password).md5());
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 send (packet);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 bump_last_ping();
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 RCONSession::set_password (const String& password) -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 m_password = password;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 }
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 // -------------------------------------------------------------------------------------------------
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 //
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 METHOD
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 RCONSession::bump_last_ping() -> void
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 {
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249 time_t now;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 time (&now);
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 m_lastPing = now;
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 }
14
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
253
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
254 // -------------------------------------------------------------------------------------------------
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
255 //
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
256 STATIC METHOD
15
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
257 RCONSession::get_session() -> RCONSession*
14
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
258 {
15
33da84af4bba - log area done, re-enabled the rcon connection
Teemu Piippo <crimsondusk64@gmail.com>
parents: 14
diff changeset
259 return g_rconSession;
14
33b8f428bacb - begin work on interface - input line works for the most part :)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 13
diff changeset
260 }
17
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
261
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
262 // -------------------------------------------------------------------------------------------------
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
263 // Returns true if the message was successfully sent.
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
264 //
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
265 METHOD
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
266 RCONSession::send_command (const String& message) -> bool
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
267 {
22
77d02446edf0 - don't send empty messages
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
268 if (m_state != RCON_CONNECTED or message.is_empty())
17
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
269 return false;
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
270
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
271 Bytestream packet;
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
272 packet.write_byte (CLRC_COMMAND);
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
273 packet.write_string (message);
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
274 send (packet);
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
275 bump_last_ping();
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
276 return true;
50341dec533e - added command sending
Teemu Piippo <crimsondusk64@gmail.com>
parents: 15
diff changeset
277 }

mercurial