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++ Interface: ConnectionManager |
---|
31 | // |
---|
32 | // Description: The Class ConnectionManager manages the servers conenctions to the clients. |
---|
33 | // each connection is provided by a new process. communication between master process and |
---|
34 | // connection processes is provided by ... |
---|
35 | // |
---|
36 | // |
---|
37 | // Author: Oliver Scheuss |
---|
38 | // |
---|
39 | |
---|
40 | #include <iostream> |
---|
41 | // boost.thread library for multithreading support |
---|
42 | #include <boost/bind.hpp> |
---|
43 | |
---|
44 | #include "core/CoreIncludes.h" |
---|
45 | #include "core/BaseObject.h" |
---|
46 | #include "util/Math.h" |
---|
47 | #include "objects/SpaceShip.h" |
---|
48 | #include "ClientInformation.h" |
---|
49 | #include "ConnectionManager.h" |
---|
50 | #include "Synchronisable.h" |
---|
51 | |
---|
52 | namespace std |
---|
53 | { |
---|
54 | bool operator< (ENetAddress a, ENetAddress b) { |
---|
55 | if(a.host <= b.host) |
---|
56 | return true; |
---|
57 | else |
---|
58 | return false; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | namespace network |
---|
63 | { |
---|
64 | //boost::thread_group network_threads; |
---|
65 | |
---|
66 | ConnectionManager::ConnectionManager():receiverThread_(0){} |
---|
67 | |
---|
68 | ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) { |
---|
69 | quit=false; |
---|
70 | bindAddress.host = ENET_HOST_ANY; |
---|
71 | bindAddress.port = NETWORK_PORT; |
---|
72 | head_ = head; |
---|
73 | } |
---|
74 | |
---|
75 | ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) { |
---|
76 | quit=false; |
---|
77 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
78 | bindAddress.port = NETWORK_PORT; |
---|
79 | head_ = head; |
---|
80 | } |
---|
81 | |
---|
82 | ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) : receiverThread_(0) { |
---|
83 | quit=false; |
---|
84 | enet_address_set_host (& bindAddress, address); |
---|
85 | bindAddress.port = NETWORK_PORT; |
---|
86 | head_ = head; |
---|
87 | } |
---|
88 | |
---|
89 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { |
---|
90 | if(!buffer.isEmpty()) |
---|
91 | return buffer.pop(address); |
---|
92 | else |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | /** |
---|
96 | This function only pops the first element in PacketBuffer (first in first out) |
---|
97 | used by processQueue in Server.cc |
---|
98 | */ |
---|
99 | ENetPacket *ConnectionManager::getPacket(int &clientID) { |
---|
100 | ENetAddress address; |
---|
101 | ENetPacket *packet=getPacket(address); |
---|
102 | ClientInformation *temp =head_->findClient(&address); |
---|
103 | clientID=temp->getID(); |
---|
104 | return packet; |
---|
105 | } |
---|
106 | |
---|
107 | bool ConnectionManager::queueEmpty() { |
---|
108 | return buffer.isEmpty(); |
---|
109 | } |
---|
110 | |
---|
111 | void ConnectionManager::createListener() { |
---|
112 | receiverThread_ = new boost::thread(boost::bind(&ConnectionManager::receiverThread, this)); |
---|
113 | //network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
114 | //boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
115 | return; |
---|
116 | } |
---|
117 | |
---|
118 | bool ConnectionManager::quitListener() { |
---|
119 | quit=true; |
---|
120 | //network_threads.join_all(); |
---|
121 | receiverThread_->join(); |
---|
122 | return true; |
---|
123 | } |
---|
124 | |
---|
125 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { |
---|
126 | if(enet_peer_send(peer, (enet_uint8)head_->findClient(&(peer->address))->getID() , packet)!=0) |
---|
127 | return false; |
---|
128 | return true; |
---|
129 | } |
---|
130 | |
---|
131 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { |
---|
132 | if(enet_peer_send(head_->findClient(clientID)->getPeer(), (enet_uint8)clientID, packet)!=0) |
---|
133 | return false; |
---|
134 | return true; |
---|
135 | } |
---|
136 | |
---|
137 | bool ConnectionManager::addPacketAll(ENetPacket *packet) { |
---|
138 | for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ |
---|
139 | if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0) |
---|
140 | return false; |
---|
141 | } |
---|
142 | return true; |
---|
143 | } |
---|
144 | |
---|
145 | bool ConnectionManager::sendPackets(ENetEvent *event) { |
---|
146 | if(server==NULL) |
---|
147 | return false; |
---|
148 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
149 | return true; |
---|
150 | else |
---|
151 | return false; |
---|
152 | } |
---|
153 | |
---|
154 | bool ConnectionManager::sendPackets() { |
---|
155 | ENetEvent event; |
---|
156 | if(server==NULL) |
---|
157 | return false; |
---|
158 | if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) |
---|
159 | return true; |
---|
160 | else |
---|
161 | return false; |
---|
162 | } |
---|
163 | |
---|
164 | void ConnectionManager::receiverThread() { |
---|
165 | // what about some error-handling here ? |
---|
166 | enet_initialize(); |
---|
167 | atexit(enet_deinitialize); |
---|
168 | ENetEvent *event = new ENetEvent; |
---|
169 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
170 | if(server==NULL){ |
---|
171 | // add some error handling here ========================== |
---|
172 | quit=true; |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | while(!quit){ |
---|
177 | if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
178 | // we should never reach this point |
---|
179 | quit=true; |
---|
180 | // add some error handling here ======================== |
---|
181 | } |
---|
182 | switch(event->type){ |
---|
183 | // log handling ================ |
---|
184 | case ENET_EVENT_TYPE_CONNECT: |
---|
185 | addClient(event); |
---|
186 | //this is a workaround to ensure thread safety |
---|
187 | /*if(!addFakeConnectRequest(&event)) |
---|
188 | COUT(3) << "Problem pushing fakeconnectRequest to queue" << std::endl;*/ |
---|
189 | COUT(5) << "Con.Man: connection event has occured" << std::endl; |
---|
190 | break; |
---|
191 | case ENET_EVENT_TYPE_RECEIVE: |
---|
192 | //std::cout << "received data" << std::endl; |
---|
193 | COUT(5) << "Con.Man: receive event has occured" << std::endl; |
---|
194 | // only add, if client has connected yet and not been disconnected |
---|
195 | if(head_->findClient(&event->peer->address)) |
---|
196 | processData(event); |
---|
197 | break; |
---|
198 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
199 | clientDisconnect(event->peer); |
---|
200 | break; |
---|
201 | case ENET_EVENT_TYPE_NONE: |
---|
202 | break; |
---|
203 | } |
---|
204 | // usleep(100); |
---|
205 | receiverThread_->yield(); //TODO: find apropriate |
---|
206 | } |
---|
207 | disconnectClients(); |
---|
208 | // if we're finishied, destroy server |
---|
209 | enet_host_destroy(server); |
---|
210 | } |
---|
211 | |
---|
212 | //### added some bugfixes here, but we cannot test them because |
---|
213 | //### the server crashes everytime because of some gamestates |
---|
214 | //### (trying to resolve that now) |
---|
215 | void ConnectionManager::disconnectClients() { |
---|
216 | ENetEvent event; |
---|
217 | ClientInformation *temp = head_->next(); |
---|
218 | while(temp!=0){ |
---|
219 | enet_peer_disconnect(temp->getPeer(), 0); |
---|
220 | temp = temp->next(); |
---|
221 | } |
---|
222 | //bugfix: might be the reason why server crashes when clients disconnects |
---|
223 | //temp = temp->next(); |
---|
224 | temp = head_->next(); |
---|
225 | while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
226 | switch (event.type) |
---|
227 | { |
---|
228 | case ENET_EVENT_TYPE_NONE: break; |
---|
229 | case ENET_EVENT_TYPE_CONNECT: break; |
---|
230 | case ENET_EVENT_TYPE_RECEIVE: |
---|
231 | enet_packet_destroy(event.packet); |
---|
232 | break; |
---|
233 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
234 | COUT(4) << "disconnecting all clients" << std::endl; |
---|
235 | delete head_->findClient(&(event.peer->address)); |
---|
236 | //maybe needs bugfix: might also be a reason for the server to crash |
---|
237 | temp = temp->next(); |
---|
238 | break; |
---|
239 | } |
---|
240 | } |
---|
241 | return; |
---|
242 | } |
---|
243 | |
---|
244 | bool ConnectionManager::processData(ENetEvent *event) { |
---|
245 | // just add packet to the buffer |
---|
246 | // this can be extended with some preprocessing |
---|
247 | return buffer.push(event); |
---|
248 | } |
---|
249 | |
---|
250 | bool ConnectionManager::clientDisconnect(ENetPeer *peer) { |
---|
251 | COUT(4) << "removing client from list" << std::endl; |
---|
252 | return removeClient(head_->findClient(&(peer->address))->getID()); |
---|
253 | } |
---|
254 | /** |
---|
255 | This function adds a client that connects to the clientlist of the server |
---|
256 | NOTE: if you change this, don't forget to change the test function |
---|
257 | addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid |
---|
258 | */ |
---|
259 | bool ConnectionManager::addClient(ENetEvent *event) { |
---|
260 | ClientInformation *temp = head_->insertBack(new ClientInformation); |
---|
261 | if(temp->prev()->head) { //not good if you use anything else than insertBack |
---|
262 | temp->prev()->setID(0); //bugfix: not necessary but usefull |
---|
263 | temp->setID(1); |
---|
264 | } |
---|
265 | else |
---|
266 | temp->setID(temp->prev()->getID()+1); |
---|
267 | temp->setPeer(event->peer); |
---|
268 | COUT(4) << "Con.Man: added client id: " << temp->getID() << std::endl; |
---|
269 | return true; |
---|
270 | } |
---|
271 | |
---|
272 | int ConnectionManager::getClientID(ENetPeer peer) { |
---|
273 | return getClientID(peer.address); |
---|
274 | } |
---|
275 | |
---|
276 | int ConnectionManager::getClientID(ENetAddress address) { |
---|
277 | return head_->findClient(&address)->getID(); |
---|
278 | } |
---|
279 | |
---|
280 | ENetPeer *ConnectionManager::getClientPeer(int clientID) { |
---|
281 | return head_->findClient(clientID)->getPeer(); |
---|
282 | } |
---|
283 | |
---|
284 | void ConnectionManager::syncClassid(int clientID) { |
---|
285 | unsigned int network_id=0; |
---|
286 | std::string classname; |
---|
287 | orxonox::Identifier *id; |
---|
288 | std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin(); |
---|
289 | while(it != orxonox::Factory::getFactoryEnd()){ |
---|
290 | id = (*it).second; |
---|
291 | if(id == NULL) |
---|
292 | continue; |
---|
293 | classname = id->getName(); |
---|
294 | network_id = id->getNetworkID(); |
---|
295 | COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl; |
---|
296 | |
---|
297 | addPacket(packet_gen.clid( (int)network_id, classname ), clientID); |
---|
298 | |
---|
299 | ++it; |
---|
300 | } |
---|
301 | sendPackets(); |
---|
302 | COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; |
---|
303 | } |
---|
304 | |
---|
305 | bool ConnectionManager::createClient(int clientID){ |
---|
306 | ClientInformation *temp = head_->findClient(clientID); |
---|
307 | COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; |
---|
308 | syncClassid(temp->getID()); |
---|
309 | COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl; |
---|
310 | // TODO: this is only a hack, untill we have a possibility to define default player-join actions |
---|
311 | createShip(temp); |
---|
312 | COUT(4) << "created spaceship" << std::endl; |
---|
313 | temp->setSynched(true); |
---|
314 | COUT(4) << "sending welcome" << std::endl; |
---|
315 | sendWelcome(temp->getID(), temp->getShipID(), true); |
---|
316 | return true; |
---|
317 | } |
---|
318 | |
---|
319 | bool ConnectionManager::removeClient(int clientID){ |
---|
320 | orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); |
---|
321 | while(it){ |
---|
322 | if(it->objectID!=head_->findClient(clientID)->getShipID()){ |
---|
323 | ++it; |
---|
324 | continue; |
---|
325 | } |
---|
326 | orxonox::Iterator<orxonox::SpaceShip> temp=it; |
---|
327 | ++it; |
---|
328 | delete *temp; |
---|
329 | return head_->removeClient(clientID); |
---|
330 | } |
---|
331 | return false; |
---|
332 | } |
---|
333 | |
---|
334 | bool ConnectionManager::createShip(ClientInformation *client){ |
---|
335 | orxonox::Identifier* id = ID("SpaceShip"); |
---|
336 | if(!id){ |
---|
337 | COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; |
---|
338 | return false; |
---|
339 | } |
---|
340 | orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); |
---|
341 | no->setPosition(orxonox::Vector3(0,80,0)); |
---|
342 | no->setScale(10); |
---|
343 | no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); |
---|
344 | no->setMesh("assff.mesh"); |
---|
345 | no->setMaxSpeed(500); |
---|
346 | no->setMaxSideAndBackSpeed(50); |
---|
347 | no->setMaxRotation(1.0); |
---|
348 | no->setTransAcc(200); |
---|
349 | no->setRotAcc(3.0); |
---|
350 | no->setTransDamp(75); |
---|
351 | no->setRotDamp(1.0); |
---|
352 | no->setCamera("cam_"+client->getID()); |
---|
353 | no->create(); |
---|
354 | |
---|
355 | client->setShipID(no->objectID); |
---|
356 | return true; |
---|
357 | } |
---|
358 | |
---|
359 | bool ConnectionManager::removeShip(ClientInformation *client){ |
---|
360 | int id=client->getShipID(); |
---|
361 | orxonox::Iterator<orxonox::SpaceShip> it; |
---|
362 | for(it = orxonox::ObjectList<orxonox::SpaceShip>::start(); it; ++it){ |
---|
363 | if(it->objectID!=id) |
---|
364 | continue; |
---|
365 | delete *it; |
---|
366 | } |
---|
367 | return true; |
---|
368 | } |
---|
369 | |
---|
370 | bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){ |
---|
371 | addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID); |
---|
372 | sendPackets(); |
---|
373 | return true; |
---|
374 | } |
---|
375 | |
---|
376 | void ConnectionManager::disconnectClient(ClientInformation *client){ |
---|
377 | enet_peer_disconnect(client->getPeer(), 0); |
---|
378 | removeShip(client); |
---|
379 | } |
---|
380 | |
---|
381 | bool ConnectionManager::addFakeConnectRequest(ENetEvent *ev){ |
---|
382 | ENetEvent event; |
---|
383 | event.peer=ev->peer; |
---|
384 | event.packet = packet_gen.generateConnectRequest(); |
---|
385 | return buffer.push(&event); |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | // int ConnectionManager::getNumberOfClients() { |
---|
390 | // |
---|
391 | // return clientsShip.size(); |
---|
392 | // } |
---|
393 | |
---|
394 | /*void ConnectionManager::addClientsObjectID( int clientID, int objectID ) { |
---|
395 | COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl; |
---|
396 | clientsShip.insert( std::make_pair( clientID, objectID ) ); |
---|
397 | } |
---|
398 | |
---|
399 | int ConnectionManager::getClientsShipID( int clientID ) { |
---|
400 | return clientsShip[clientID]; |
---|
401 | } |
---|
402 | |
---|
403 | int ConnectionManager::getObjectsClientID( int objectID ) { |
---|
404 | std::map<int, int>::iterator iter; |
---|
405 | for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { |
---|
406 | if( iter->second == objectID ) return iter->first; |
---|
407 | } |
---|
408 | return -99; |
---|
409 | } |
---|
410 | |
---|
411 | void ConnectionManager::deleteClientIDReg( int clientID ) { |
---|
412 | clientsShip.erase( clientID ); |
---|
413 | } |
---|
414 | |
---|
415 | void ConnectionManager::deleteObjectIDReg( int objectID ) { |
---|
416 | std::map<int, int>::iterator iter = clientsShip.begin(); |
---|
417 | for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) { |
---|
418 | if( iter->second == objectID ) break; |
---|
419 | } |
---|
420 | clientsShip.erase( iter->first ); |
---|
421 | }*/ |
---|
422 | } |
---|