1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Oliver Scheuss |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | // |
---|
30 | // C++ Implementation: Server |
---|
31 | // |
---|
32 | // Description: |
---|
33 | // |
---|
34 | // |
---|
35 | // Author: Oliver Scheuss, (C) 2007 |
---|
36 | // |
---|
37 | // Copyright: See COPYING file that comes with this distribution |
---|
38 | // |
---|
39 | // |
---|
40 | |
---|
41 | #include "Server.h" |
---|
42 | |
---|
43 | #include <enet/enet.h> |
---|
44 | #include <iostream> |
---|
45 | #include <cassert> |
---|
46 | |
---|
47 | |
---|
48 | // #include "ConnectionManager.h" |
---|
49 | #include "ClientConnectionListener.h" |
---|
50 | #include "GamestateManager.h" |
---|
51 | #include "ClientInformation.h" |
---|
52 | #include "util/Sleep.h" |
---|
53 | #include "core/Clock.h" |
---|
54 | #include "core/ConsoleCommand.h" |
---|
55 | #include "core/CoreIncludes.h" |
---|
56 | #include "packet/Chat.h" |
---|
57 | #include "packet/Packet.h" |
---|
58 | #include "packet/Welcome.h" |
---|
59 | #include "packet/DeleteObjects.h" |
---|
60 | #include "packet/ClassID.h" |
---|
61 | #include "util/Convert.h" |
---|
62 | #include "ChatListener.h" |
---|
63 | #include "FunctionCallManager.h" |
---|
64 | #include "packet/FunctionIDs.h" |
---|
65 | |
---|
66 | |
---|
67 | namespace orxonox |
---|
68 | { |
---|
69 | const unsigned int MAX_FAILURES = 20; |
---|
70 | |
---|
71 | /** |
---|
72 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
---|
73 | * |
---|
74 | */ |
---|
75 | Server::Server() { |
---|
76 | timeSinceLastUpdate_=0; |
---|
77 | gamestates_ = new GamestateManager(); |
---|
78 | } |
---|
79 | |
---|
80 | Server::Server(int port){ |
---|
81 | this->setPort( port ); |
---|
82 | timeSinceLastUpdate_=0; |
---|
83 | gamestates_ = new GamestateManager(); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Constructor |
---|
88 | * @param port Port to listen on |
---|
89 | * @param bindAddress Address to listen on |
---|
90 | */ |
---|
91 | Server::Server(int port, const std::string& bindAddress) { |
---|
92 | this->setPort( port ); |
---|
93 | this->setBindAddress( bindAddress ); |
---|
94 | timeSinceLastUpdate_=0; |
---|
95 | gamestates_ = new GamestateManager(); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @brief Destructor |
---|
100 | */ |
---|
101 | Server::~Server(){ |
---|
102 | if(gamestates_) |
---|
103 | delete gamestates_; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * This function opens the server by creating the listener thread |
---|
108 | */ |
---|
109 | void Server::open() { |
---|
110 | COUT(4) << "opening server" << endl; |
---|
111 | this->openListener(); |
---|
112 | return; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * This function closes the server |
---|
117 | */ |
---|
118 | void Server::close() { |
---|
119 | COUT(4) << "closing server" << endl; |
---|
120 | this->disconnectClients(); |
---|
121 | this->closeListener(); |
---|
122 | return; |
---|
123 | } |
---|
124 | |
---|
125 | bool Server::processChat(const std::string& message, unsigned int playerID){ |
---|
126 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
127 | packet::Chat *chat; |
---|
128 | while(temp){ |
---|
129 | chat = new packet::Chat(message, playerID); |
---|
130 | chat->setClientID(temp->getID()); |
---|
131 | if(!chat->send()) |
---|
132 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
---|
133 | temp = temp->next(); |
---|
134 | } |
---|
135 | // COUT(1) << "Player " << playerID << ": " << message << std::endl; |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /** |
---|
141 | * Run this function once every tick |
---|
142 | * calls processQueue and updateGamestate |
---|
143 | * @param time time since last tick |
---|
144 | */ |
---|
145 | void Server::update(const Clock& time) { |
---|
146 | Connection::processQueue(); |
---|
147 | gamestates_->processGamestates(); |
---|
148 | //this steers our network frequency |
---|
149 | timeSinceLastUpdate_+=time.getDeltaTime(); |
---|
150 | if(timeSinceLastUpdate_>=NETWORK_PERIOD) |
---|
151 | { |
---|
152 | timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; |
---|
153 | updateGamestate(); |
---|
154 | FunctionCallManager::sendCalls(); |
---|
155 | } |
---|
156 | sendPackets(); // flush the enet queue |
---|
157 | } |
---|
158 | |
---|
159 | bool Server::queuePacket(ENetPacket *packet, int clientID){ |
---|
160 | return ServerConnection::addPacket(packet, clientID); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * @brief: returns ping time to client in milliseconds |
---|
165 | */ |
---|
166 | unsigned int Server::getPing(unsigned int clientID){ |
---|
167 | assert(ClientInformation::findClient(clientID)); |
---|
168 | return ClientInformation::findClient(clientID)->getRTT(); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * @brief: return packet loss ratio to client (scales from 0 to 1) |
---|
173 | */ |
---|
174 | double Server::getPacketLoss(unsigned int clientID){ |
---|
175 | assert(ClientInformation::findClient(clientID)); |
---|
176 | return ClientInformation::findClient(clientID)->getPacketLoss(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * takes a new snapshot of the gamestate and sends it to the clients |
---|
181 | */ |
---|
182 | void Server::updateGamestate() { |
---|
183 | // if( ClientInformation::getBegin()==NULL ) |
---|
184 | //no client connected |
---|
185 | // return; |
---|
186 | gamestates_->update(); |
---|
187 | COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; |
---|
188 | //std::cout << "updated gamestate, sending it" << std::endl; |
---|
189 | //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) |
---|
190 | sendGameState(); |
---|
191 | sendObjectDeletes(); |
---|
192 | COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; |
---|
193 | //std::cout << "sent gamestate" << std::endl; |
---|
194 | } |
---|
195 | |
---|
196 | bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){ |
---|
197 | packet::Packet *p = packet::Packet::createPacket(packet, peer); |
---|
198 | return p->process(); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * sends the gamestate |
---|
203 | */ |
---|
204 | bool Server::sendGameState() { |
---|
205 | COUT(5) << "Server: starting function sendGameState" << std::endl; |
---|
206 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
207 | bool added=false; |
---|
208 | while(temp != NULL){ |
---|
209 | if( !(temp->getSynched()) ){ |
---|
210 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
211 | temp=temp->next(); |
---|
212 | if(!temp) |
---|
213 | break; |
---|
214 | //think this works without continue |
---|
215 | continue; |
---|
216 | } |
---|
217 | COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; |
---|
218 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
---|
219 | int gid = temp->getGamestateID(); //get gamestate id |
---|
220 | int cid = temp->getID(); //get client id |
---|
221 | COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl; |
---|
222 | packet::Gamestate *gs = gamestates_->popGameState(cid); |
---|
223 | if(gs==NULL){ |
---|
224 | COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; |
---|
225 | temp = temp->next(); |
---|
226 | continue; |
---|
227 | } |
---|
228 | //std::cout << "adding gamestate" << std::endl; |
---|
229 | gs->setClientID(cid); |
---|
230 | if ( !gs->send() ){ |
---|
231 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
---|
232 | temp->addFailure(); |
---|
233 | }else |
---|
234 | temp->resetFailures(); |
---|
235 | added=true; |
---|
236 | temp=temp->next(); |
---|
237 | // gs gets automatically deleted by enet callback |
---|
238 | } |
---|
239 | return true; |
---|
240 | } |
---|
241 | |
---|
242 | bool Server::sendObjectDeletes(){ |
---|
243 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
244 | if( temp == NULL ) |
---|
245 | //no client connected |
---|
246 | return true; |
---|
247 | packet::DeleteObjects *del = new packet::DeleteObjects(); |
---|
248 | if(!del->fetchIDs()) |
---|
249 | return true; //everything ok (no deletes this tick) |
---|
250 | // COUT(3) << "sending DeleteObjects" << std::endl; |
---|
251 | while(temp != NULL){ |
---|
252 | if( !(temp->getSynched()) ){ |
---|
253 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
254 | temp=temp->next(); |
---|
255 | continue; |
---|
256 | } |
---|
257 | int cid = temp->getID(); //get client id |
---|
258 | packet::DeleteObjects *cd = new packet::DeleteObjects(*del); |
---|
259 | assert(cd); |
---|
260 | cd->setClientID(cid); |
---|
261 | if ( !cd->send() ) |
---|
262 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
---|
263 | temp=temp->next(); |
---|
264 | // gs gets automatically deleted by enet callback |
---|
265 | } |
---|
266 | delete del; |
---|
267 | return true; |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | void Server::addClient(ENetEvent *event){ |
---|
272 | static unsigned int newid=1; |
---|
273 | |
---|
274 | COUT(2) << "Server: adding client" << std::endl; |
---|
275 | ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); |
---|
276 | if(!temp){ |
---|
277 | COUT(2) << "Server: could not add client" << std::endl; |
---|
278 | } |
---|
279 | temp->setID(newid); |
---|
280 | temp->setPeer(event->peer); |
---|
281 | |
---|
282 | // inform all the listeners |
---|
283 | ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin(); |
---|
284 | while(listener){ |
---|
285 | listener->clientConnected(newid); |
---|
286 | listener++; |
---|
287 | } |
---|
288 | |
---|
289 | ++newid; |
---|
290 | |
---|
291 | COUT(3) << "Server: added client id: " << temp->getID() << std::endl; |
---|
292 | createClient(temp->getID()); |
---|
293 | } |
---|
294 | |
---|
295 | bool Server::createClient(int clientID){ |
---|
296 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
297 | if(!temp){ |
---|
298 | COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; |
---|
299 | return false; |
---|
300 | } |
---|
301 | COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl; |
---|
302 | |
---|
303 | // synchronise class ids |
---|
304 | syncClassid(temp->getID()); |
---|
305 | |
---|
306 | // now synchronise functionIDs |
---|
307 | packet::FunctionIDs *fIDs = new packet::FunctionIDs(); |
---|
308 | fIDs->setClientID(clientID); |
---|
309 | bool b = fIDs->send(); |
---|
310 | assert(b); |
---|
311 | |
---|
312 | temp->setSynched(true); |
---|
313 | COUT(4) << "sending welcome" << std::endl; |
---|
314 | packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID()); |
---|
315 | w->setClientID(temp->getID()); |
---|
316 | b = w->send(); |
---|
317 | assert(b); |
---|
318 | packet::Gamestate *g = new packet::Gamestate(); |
---|
319 | g->setClientID(temp->getID()); |
---|
320 | b = g->collectData(0,0x1); |
---|
321 | if(!b) |
---|
322 | return false; //no data for the client |
---|
323 | b = g->compressData(); |
---|
324 | assert(b); |
---|
325 | b = g->send(); |
---|
326 | assert(b); |
---|
327 | return true; |
---|
328 | } |
---|
329 | |
---|
330 | void Server::disconnectClient( ClientInformation *client ){ |
---|
331 | ServerConnection::disconnectClient( client ); |
---|
332 | gamestates_->removeClient(client); |
---|
333 | // inform all the listeners |
---|
334 | ObjectList<ClientConnectionListener>::iterator listener = ObjectList<ClientConnectionListener>::begin(); |
---|
335 | while(listener){ |
---|
336 | listener->clientDisconnected(client->getID()); |
---|
337 | ++listener; |
---|
338 | } |
---|
339 | delete client; //remove client from list |
---|
340 | } |
---|
341 | |
---|
342 | bool Server::chat(const std::string& message){ |
---|
343 | return this->sendChat(message, Host::getPlayerID()); |
---|
344 | } |
---|
345 | |
---|
346 | bool Server::broadcast(const std::string& message){ |
---|
347 | return this->sendChat(message, CLIENTID_UNKNOWN); |
---|
348 | } |
---|
349 | |
---|
350 | bool Server::sendChat(const std::string& message, unsigned int clientID){ |
---|
351 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
352 | packet::Chat *chat; |
---|
353 | while(temp){ |
---|
354 | chat = new packet::Chat(message, clientID); |
---|
355 | chat->setClientID(temp->getID()); |
---|
356 | if(!chat->send()) |
---|
357 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
---|
358 | temp = temp->next(); |
---|
359 | } |
---|
360 | // COUT(1) << "Player " << Host::getPlayerID() << ": " << message << std::endl; |
---|
361 | for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) |
---|
362 | it->incomingChat(message, clientID); |
---|
363 | |
---|
364 | return true; |
---|
365 | } |
---|
366 | |
---|
367 | void Server::syncClassid(unsigned int clientID) { |
---|
368 | int failures=0; |
---|
369 | packet::ClassID *classid = new packet::ClassID(); |
---|
370 | classid->setClientID(clientID); |
---|
371 | while(!classid->send() && failures < 10){ |
---|
372 | failures++; |
---|
373 | } |
---|
374 | assert(failures<10); |
---|
375 | COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; |
---|
376 | } |
---|
377 | |
---|
378 | } |
---|