sources/network/rconsession.cpp

changeset 12
8d0d1b368de0
parent 11
cffa2777d917
child 13
09dcaeaa216b
equal deleted inserted replaced
11:cffa2777d917 12:8d0d1b368de0
7 m_lastPing (0) 7 m_lastPing (0)
8 { 8 {
9 if (not m_socket.set_blocking (false)) 9 if (not m_socket.set_blocking (false))
10 { 10 {
11 // TODO: find a better way to deal with errors 11 // TODO: find a better way to deal with errors
12 fprintf (stderr, "unable to set socket as non-blocking: %s\n", 12 print ("unable to set socket as non-blocking: %s\n",
13 m_socket.error_string().chars()); 13 m_socket.error_string().chars());
14 } 14 }
15 } 15 }
16 16
17 // ------------------------------------------------------------------------------------------------- 17 // -------------------------------------------------------------------------------------------------
91 bool ok = true; 91 bool ok = true;
92 92
93 while (packet.bytes_left() > 0) 93 while (packet.bytes_left() > 0)
94 { 94 {
95 int header = packet.read_byte (&ok); 95 int header = packet.read_byte (&ok);
96 print ("Recieved packet with header %1\n", header);
96 97
97 switch (ServerResponse (header)) 98 switch (ServerResponse (header))
98 { 99 {
99 case SVRC_OLDPROTOCOL: 100 case SVRC_OLDPROTOCOL:
100 fprintf (stderr, "wrong version\n"); 101 print ("wrong version\n");
101 m_state = RCON_DISCONNECTED; 102 m_state = RCON_DISCONNECTED;
102 break; 103 break;
103 104
104 case SVRC_BANNED: 105 case SVRC_BANNED:
105 fprintf (stderr, "you're banned\n"); 106 print ("you're banned\n");
106 m_state = RCON_DISCONNECTED; 107 m_state = RCON_DISCONNECTED;
107 break; 108 break;
108 109
109 case SVRC_SALT: 110 case SVRC_SALT:
110 { 111 {
113 m_state = RCON_AUTHENTICATING; 114 m_state = RCON_AUTHENTICATING;
114 send_password(); 115 send_password();
115 } 116 }
116 break; 117 break;
117 118
118 case SVRC_LOGGEDIN:
119 fprintf (stderr, "login successful\n");
120 m_state = RCON_CONNECTED;
121 break;
122
123 case SVRC_INVALIDPASSWORD: 119 case SVRC_INVALIDPASSWORD:
124 fprintf (stderr, "bad password\n"); 120 print ("bad password\n");
125 m_state = RCON_DISCONNECTED; 121 m_state = RCON_DISCONNECTED;
126 break; 122 break;
127 123
128 case SVRC_MESSAGE: 124 case SVRC_MESSAGE:
129 { 125 {
130 String message = packet.read_string(); 126 String message = packet.read_string();
131 print_to (stderr, "message: %1\n", message); 127 if (message.ends_with ("\n"))
128 message.remove_from_end (1);
129 print ("message: %1\n", message);
132 } 130 }
133 break; 131 break;
134 132
133 case SVRC_LOGGEDIN:
134 print ("login successful\n");
135 m_serverProtocol = packet.read_byte();
136 m_hostname = packet.read_string();
137 m_state = RCON_CONNECTED;
138
139 for (int i = packet.read_byte(); i > 0; --i)
140 process_server_updates (packet);
141
142 for (int i = packet.read_byte(); i > 0; --i)
143 {
144 String message = packet.read_string();
145 message.normalize();
146 print ("--- %1\n", message);
147 }
148
149 break;
150
135 case SVRC_UPDATE: 151 case SVRC_UPDATE:
136 switch (RCONUpdateType (packet.read_byte (&ok))) 152 process_server_updates (packet);
137 {
138 case SVRCU_PLAYERDATA:
139 {
140 int numplayers = packet.read_byte (&ok);
141 Vector<String> players;
142
143 while (numplayers--)
144 players << packet.read_string (&ok);
145
146 print_to (stderr, "players: %1\n", players);
147 }
148 break;
149
150 case SVRCU_ADMINCOUNT:
151 print_to (stderr, "num admins: %1\n", packet.read_byte (&ok));
152 break;
153
154 case SVRCU_MAP:
155 print_to (stderr, "new map: %1\n", packet.read_string (&ok));
156 break;
157 }
158
159 break; 153 break;
160 } 154 }
161 155
162 if (not ok) 156 if (not ok)
163 print_to (stderr, "error while reading packet\n"); 157 print ("error while reading packet\n");
158 }
159 }
160
161 METHOD
162 RCONSession::process_server_updates (Bytestream& packet) -> void
163 {
164 switch (RCONUpdateType (packet.read_byte()))
165 {
166 case SVRCU_PLAYERDATA:
167 {
168 Vector<String> players;
169 for (int i = packet.read_byte(); i > 0; --i)
170 players << packet.read_string();
171 print ("players: %1\n", players);
172 }
173 break;
174
175 case SVRCU_ADMINCOUNT:
176 print ("num admins: %d1\n", packet.read_byte());
177 break;
178
179 case SVRCU_MAP:
180 print ("new map: %1\n", packet.read_string());
181 break;
164 } 182 }
165 } 183 }
166 184
167 // ------------------------------------------------------------------------------------------------- 185 // -------------------------------------------------------------------------------------------------
168 // 186 //

mercurial