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, (C) 2007 |
---|
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 <iostream> |
---|
44 | |
---|
45 | |
---|
46 | #include "ConnectionManager.h" |
---|
47 | #include "GamestateManager.h" |
---|
48 | #include "ClientInformation.h" |
---|
49 | #include "util/Sleep.h" |
---|
50 | #include "objects/SpaceShip.h" |
---|
51 | #include "core/ConsoleCommand.h" |
---|
52 | #include "core/Iterator.h" |
---|
53 | #include "packet/Chat.h" |
---|
54 | #include "packet/Packet.h" |
---|
55 | #include "packet/Welcome.h" |
---|
56 | #include <util/Convert.h> |
---|
57 | |
---|
58 | namespace network |
---|
59 | { |
---|
60 | #define MAX_FAILURES 20; |
---|
61 | #define NETWORK_FREQUENCY 30 |
---|
62 | |
---|
63 | /** |
---|
64 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
---|
65 | * |
---|
66 | */ |
---|
67 | Server::Server() { |
---|
68 | timeSinceLastUpdate_=0; |
---|
69 | connection = new ConnectionManager(); |
---|
70 | gamestates_ = new GamestateManager(); |
---|
71 | isServer_ = true; |
---|
72 | } |
---|
73 | |
---|
74 | Server::Server(int port){ |
---|
75 | timeSinceLastUpdate_=0; |
---|
76 | connection = new ConnectionManager(port); |
---|
77 | gamestates_ = new GamestateManager(); |
---|
78 | isServer_ = true; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Constructor |
---|
83 | * @param port Port to listen on |
---|
84 | * @param bindAddress Address to listen on |
---|
85 | */ |
---|
86 | Server::Server(int port, std::string bindAddress) { |
---|
87 | timeSinceLastUpdate_=0; |
---|
88 | connection = new ConnectionManager(port, bindAddress); |
---|
89 | gamestates_ = new GamestateManager(); |
---|
90 | isServer_ = true; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Constructor |
---|
95 | * @param port Port to listen on |
---|
96 | * @param bindAddress Address to listen on |
---|
97 | */ |
---|
98 | Server::Server(int port, const char *bindAddress) { |
---|
99 | timeSinceLastUpdate_=0; |
---|
100 | connection = new ConnectionManager(port, bindAddress); |
---|
101 | gamestates_ = new GamestateManager(); |
---|
102 | isServer_ = true; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * This function opens the server by creating the listener thread |
---|
107 | */ |
---|
108 | void Server::open() { |
---|
109 | connection->createListener(); |
---|
110 | return; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * This function closes the server |
---|
115 | */ |
---|
116 | void Server::close() { |
---|
117 | connection->quitListener(); |
---|
118 | return; |
---|
119 | } |
---|
120 | |
---|
121 | bool Server::processChat(packet::Chat *message, unsigned int clientID){ |
---|
122 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
123 | while(temp){ |
---|
124 | message->setClientID(temp->getID()); |
---|
125 | if(!message->send()) |
---|
126 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
---|
127 | temp = temp->next(); |
---|
128 | } |
---|
129 | return message->process(); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * This function sends out a message to all clients |
---|
134 | * @param msg message |
---|
135 | * @return true/false |
---|
136 | */ |
---|
137 | bool Server::sendChat(packet::Chat *chat) { |
---|
138 | //TODO: change this (no informations about who wrote a message) |
---|
139 | assert(0); |
---|
140 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
141 | while(temp){ |
---|
142 | chat->setClientID(temp->getID()); |
---|
143 | if(!chat->send()) |
---|
144 | COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; |
---|
145 | } |
---|
146 | return chat->process();; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * This function sends out a message to all clients |
---|
151 | * @param msg message |
---|
152 | * @return true/false |
---|
153 | */ |
---|
154 | // bool Server::sendChat(const char *msg) { |
---|
155 | // char *message = new char [strlen(msg)+10+1]; |
---|
156 | // sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg); |
---|
157 | // COUT(1) << message << std::endl; |
---|
158 | // ENetPacket *packet = packet_gen.chatMessage(message); |
---|
159 | // COUT(5) <<"Server: adding Packets" << std::endl; |
---|
160 | // return connection->addPacketAll(packet); |
---|
161 | // } |
---|
162 | |
---|
163 | /** |
---|
164 | * Run this function once every tick |
---|
165 | * calls processQueue and updateGamestate |
---|
166 | * @param time time since last tick |
---|
167 | */ |
---|
168 | void Server::tick(float time) { |
---|
169 | processQueue(); |
---|
170 | //this steers our network frequency |
---|
171 | timeSinceLastUpdate_+=time; |
---|
172 | if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){ |
---|
173 | timeSinceLastUpdate_=(float)((int)(timeSinceLastUpdate_*NETWORK_FREQUENCY))/timeSinceLastUpdate_; |
---|
174 | // timeSinceLastUpdate_-=1./NETWORK_FREQUENCY; |
---|
175 | gamestates_->processGamestates(); |
---|
176 | updateGamestate(); |
---|
177 | } |
---|
178 | /*while(timeSinceLastUpdate_>1./NETWORK_FREQUENCY) |
---|
179 | timeSinceLastUpdate_-=1./NETWORK_FREQUENCY;*/ |
---|
180 | // usleep(5000); // TODO remove |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | bool Server::queuePacket(ENetPacket *packet, int clientID){ |
---|
185 | return connection->addPacket(packet, clientID); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * processes all the packets waiting in the queue |
---|
190 | */ |
---|
191 | void Server::processQueue() { |
---|
192 | ENetEvent *event; |
---|
193 | while(!connection->queueEmpty()){ |
---|
194 | //std::cout << "Client " << clientID << " sent: " << std::endl; |
---|
195 | //clientID here is a reference to grab clientID from ClientInformation |
---|
196 | event = connection->getEvent(); |
---|
197 | if(!event) |
---|
198 | continue; |
---|
199 | assert(event->type != ENET_EVENT_TYPE_NONE); |
---|
200 | switch( event->type ) { |
---|
201 | case ENET_EVENT_TYPE_CONNECT: |
---|
202 | COUT(3) << "processing event_Type_connect" << std::endl; |
---|
203 | addClient(event); |
---|
204 | break; |
---|
205 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
206 | if(ClientInformation::findClient(&event->peer->address)) |
---|
207 | disconnectClient(event); |
---|
208 | break; |
---|
209 | case ENET_EVENT_TYPE_RECEIVE: |
---|
210 | if(!processPacket(event->packet, event->peer)) |
---|
211 | COUT(3) << "processing incoming packet failed" << std::endl; |
---|
212 | break; |
---|
213 | default: |
---|
214 | break; |
---|
215 | } |
---|
216 | delete event; |
---|
217 | //if statement to catch case that packetbuffer is empty |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * takes a new snapshot of the gamestate and sends it to the clients |
---|
223 | */ |
---|
224 | void Server::updateGamestate() { |
---|
225 | gamestates_->update(); |
---|
226 | COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; |
---|
227 | //std::cout << "updated gamestate, sending it" << std::endl; |
---|
228 | //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) |
---|
229 | sendGameState(); |
---|
230 | COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; |
---|
231 | //std::cout << "sent gamestate" << std::endl; |
---|
232 | } |
---|
233 | |
---|
234 | bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){ |
---|
235 | packet::Packet *p = packet::Packet::createPacket(packet, peer); |
---|
236 | return p->process(); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * sends the gamestate |
---|
241 | */ |
---|
242 | bool Server::sendGameState() { |
---|
243 | COUT(5) << "Server: starting function sendGameState" << std::endl; |
---|
244 | ClientInformation *temp = ClientInformation::getBegin(); |
---|
245 | bool added=false; |
---|
246 | while(temp != NULL){ |
---|
247 | if( !(temp->getSynched()) ){ |
---|
248 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
249 | temp=temp->next(); |
---|
250 | if(!temp) |
---|
251 | break; |
---|
252 | //think this works without continue |
---|
253 | continue; |
---|
254 | } |
---|
255 | COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; |
---|
256 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
---|
257 | int gid = temp->getGamestateID(); //get gamestate id |
---|
258 | int cid = temp->getID(); //get client id |
---|
259 | COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl; |
---|
260 | packet::Gamestate *gs = gamestates_->popGameState(cid); |
---|
261 | if(gs==NULL){ |
---|
262 | COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; |
---|
263 | continue; |
---|
264 | } |
---|
265 | //std::cout << "adding gamestate" << std::endl; |
---|
266 | gs->setClientID(cid); |
---|
267 | if ( !gs->send() ){ |
---|
268 | COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; |
---|
269 | temp->addFailure(); |
---|
270 | }else |
---|
271 | temp->resetFailures(); |
---|
272 | added=true; |
---|
273 | temp=temp->next(); |
---|
274 | // gs gets automatically deleted by enet callback |
---|
275 | } |
---|
276 | /*if(added) { |
---|
277 | //std::cout << "send gamestates from server.cc in sendGameState" << std::endl; |
---|
278 | return connection->sendPackets(); |
---|
279 | }*/ |
---|
280 | //COUT(5) << "Server: had no gamestates to send" << std::endl; |
---|
281 | return true; |
---|
282 | } |
---|
283 | |
---|
284 | // void Server::processChat( chat *data, int clientId){ |
---|
285 | // char *message = new char [strlen(data->message)+10+1]; |
---|
286 | // sprintf(message, "Player %d: %s", clientId, data->message); |
---|
287 | // COUT(1) << message << std::endl; |
---|
288 | // ENetPacket *pck = packet_gen.chatMessage(message); |
---|
289 | // connection->addPacketAll(pck); |
---|
290 | // delete[] data->message; |
---|
291 | // delete data; |
---|
292 | // } |
---|
293 | |
---|
294 | bool Server::addClient(ENetEvent *event){ |
---|
295 | ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); |
---|
296 | if(!temp){ |
---|
297 | COUT(2) << "Server: could not add client" << std::endl; |
---|
298 | return false; |
---|
299 | } |
---|
300 | if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack |
---|
301 | temp->setID(1); |
---|
302 | } |
---|
303 | else |
---|
304 | temp->setID(temp->prev()->getID()+1); |
---|
305 | temp->setPeer(event->peer); |
---|
306 | COUT(3) << "Server: added client id: " << temp->getID() << std::endl; |
---|
307 | return createClient(temp->getID()); |
---|
308 | } |
---|
309 | |
---|
310 | bool Server::createClient(int clientID){ |
---|
311 | ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
312 | if(!temp){ |
---|
313 | COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; |
---|
314 | return false; |
---|
315 | } |
---|
316 | COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; |
---|
317 | connection->syncClassid(temp->getID()); |
---|
318 | COUT(5) << "creating spaceship for clientid: " << temp->getID() << std::endl; |
---|
319 | // TODO: this is only a hack, untill we have a possibility to define default player-join actions |
---|
320 | if(!createShip(temp)) |
---|
321 | COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl; |
---|
322 | else |
---|
323 | COUT(3) << "created spaceship" << std::endl; |
---|
324 | temp->setSynched(true); |
---|
325 | COUT(3) << "sending welcome" << std::endl; |
---|
326 | packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID()); |
---|
327 | w->setClientID(temp->getID()); |
---|
328 | assert(w->send()); |
---|
329 | packet::Gamestate *g = new packet::Gamestate(); |
---|
330 | g->setClientID(temp->getID()); |
---|
331 | assert(g->collectData(0)); |
---|
332 | assert(g->compressData()); |
---|
333 | assert(g->send()); |
---|
334 | return true; |
---|
335 | } |
---|
336 | |
---|
337 | bool Server::createShip(ClientInformation *client){ |
---|
338 | if(!client) |
---|
339 | return false; |
---|
340 | orxonox::Identifier* id = ID("SpaceShip"); |
---|
341 | if(!id){ |
---|
342 | COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; |
---|
343 | return false; |
---|
344 | } |
---|
345 | orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); |
---|
346 | no->classID = id->getNetworkID(); |
---|
347 | client->setShipID(no->objectID); |
---|
348 | no->setPosition(orxonox::Vector3(0,0,80)); |
---|
349 | no->setScale(10); |
---|
350 | //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); |
---|
351 | no->setMesh("assff.mesh"); |
---|
352 | no->setMaxSpeed(500); |
---|
353 | no->setMaxSideAndBackSpeed(50); |
---|
354 | no->setMaxRotation(1.0); |
---|
355 | no->setTransAcc(200); |
---|
356 | no->setRotAcc(3.0); |
---|
357 | no->setTransDamp(75); |
---|
358 | no->setRotDamp(1.0); |
---|
359 | no->setCamera(std::string("cam_") + convertToString(client->getID())); |
---|
360 | no->create(); |
---|
361 | |
---|
362 | return true; |
---|
363 | } |
---|
364 | |
---|
365 | bool Server::disconnectClient(ENetEvent *event){ |
---|
366 | COUT(4) << "removing client from list" << std::endl; |
---|
367 | //return removeClient(head_->findClient(&(peer->address))->getID()); |
---|
368 | |
---|
369 | //boost::recursive_mutex::scoped_lock lock(head_->mutex_); |
---|
370 | orxonox::ObjectList<orxonox::SpaceShip>::iterator it = orxonox::ObjectList<orxonox::SpaceShip>::begin(); |
---|
371 | ClientInformation *client = ClientInformation::findClient(&event->peer->address); |
---|
372 | if(!client) |
---|
373 | return false; |
---|
374 | gamestates_->removeClient(client); |
---|
375 | while(it){ |
---|
376 | if(it->objectID!=client->getShipID()){ |
---|
377 | ++it; |
---|
378 | continue; |
---|
379 | } |
---|
380 | orxonox::ObjectList<orxonox::SpaceShip>::iterator temp=it; |
---|
381 | ++it; |
---|
382 | delete *temp; |
---|
383 | return ClientInformation::removeClient(event->peer); |
---|
384 | } |
---|
385 | return false; |
---|
386 | } |
---|
387 | |
---|
388 | void Server::disconnectClient(int clientID){ |
---|
389 | ClientInformation *client = ClientInformation::findClient(clientID); |
---|
390 | if(client) |
---|
391 | disconnectClient(client); |
---|
392 | } |
---|
393 | void Server::disconnectClient( ClientInformation *client){ |
---|
394 | connection->disconnectClient(client); |
---|
395 | gamestates_->removeClient(client); |
---|
396 | } |
---|
397 | |
---|
398 | } |
---|