Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/shader/src/network/ConnectionManager.cc @ 11629

Last change on this file since 11629 was 1505, checked in by rgrieder, 17 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 11.3 KB
Line 
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 "objects/SpaceShip.h"
47#include "util/Math.h"
48#include "util/Sleep.h"
49#include "ClientInformation.h"
50#include "ConnectionManager.h"
51#include "Synchronisable.h"
52
53namespace std
54{
55  bool operator< (ENetAddress a, ENetAddress b) {
56    if(a.host <= b.host)
57      return true;
58    else
59      return false;
60  }
61}
62
63namespace network
64{
65  //boost::thread_group network_threads;
66 
67  ConnectionManager::ConnectionManager():receiverThread_(0){}
68  boost::recursive_mutex ConnectionManager::enet_mutex_;
69 
70  ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) {
71    quit=false;
72    bindAddress.host = ENET_HOST_ANY;
73    bindAddress.port = NETWORK_PORT;
74    head_ = head;
75  }
76 
77  ConnectionManager::ConnectionManager(ClientInformation *head, int port){
78    quit=false;
79    bindAddress.host = ENET_HOST_ANY;
80    bindAddress.port = port;
81    head_ = head;
82  }
83
84  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) {
85    quit=false;
86    enet_address_set_host (& bindAddress, address.c_str());
87    bindAddress.port = NETWORK_PORT;
88    head_ = head;
89  }
90
91  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) : receiverThread_(0) {
92    quit=false;
93    enet_address_set_host (& bindAddress, address);
94    bindAddress.port = NETWORK_PORT;
95    head_ = head;
96  }
97
98  /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
99    if(!buffer.isEmpty())
100      return buffer.pop(address);
101    else
102      return NULL;
103  }*/
104/**
105This function only pops the first element in PacketBuffer (first in first out)
106used by processQueue in Server.cc
107*/
108  /*ENetPacket *ConnectionManager::getPacket(int &clientID) {
109    ENetAddress address;
110    ENetPacket *packet=getPacket(address);
111    ClientInformation *temp =head_->findClient(&address);
112    if(!temp)
113      return NULL;
114    clientID=temp->getID();
115    return packet;
116  }*/
117 
118  ENetEvent *ConnectionManager::getEvent(){
119    if(!buffer.isEmpty())
120      return buffer.pop();
121    else
122      return NULL;
123  }
124
125  bool ConnectionManager::queueEmpty() {
126    return buffer.isEmpty();
127  }
128
129  void ConnectionManager::createListener() {
130    receiverThread_ = new boost::thread(boost::bind(&ConnectionManager::receiverThread, this));
131    //network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
132         //boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
133    return;
134  }
135
136  bool ConnectionManager::quitListener() {
137    quit=true;
138    //network_threads.join_all();
139    receiverThread_->join();
140    return true;
141  }
142
143  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
144    ClientInformation *temp = head_->findClient(&(peer->address));
145    if(!temp)
146      return false;
147    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
148    if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0)
149      return false;
150    return true;
151  }
152
153  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
154    ClientInformation *temp = head_->findClient(clientID);
155    if(!temp){
156      COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
157      return false;
158    }
159    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
160    if(enet_peer_send(temp->getPeer(), 0, packet)!=0){
161      COUT(3) << "C.Man: addPacket enet_peer_send failed" << std::endl;
162      return false;
163    }
164    return true;
165  }
166
167  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
168    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
169    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
170      if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
171        return false;
172    }
173    return true;
174  }
175
176  // we actually dont need that function, because host_service does that for us
177  bool ConnectionManager::sendPackets() {
178    if(server==NULL)
179      return false;
180    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
181    enet_host_flush(server);
182    lock.unlock();
183    return true;
184  }
185
186  void ConnectionManager::receiverThread() {
187    // what about some error-handling here ?
188    ENetEvent *event;
189    atexit(enet_deinitialize);
190    { //scope of the mutex
191      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
192      enet_initialize();
193      server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
194      lock.unlock();
195    }
196    if(server==NULL){
197      // add some error handling here ==========================
198      quit=true;
199      return;
200    }
201
202    event = new ENetEvent;
203    while(!quit){
204      { //mutex scope
205        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
206        if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
207          // we should never reach this point
208          quit=true;
209          continue;
210          // add some error handling here ========================
211        }
212        lock.unlock();
213      }
214      switch(event->type){
215        // log handling ================
216        case ENET_EVENT_TYPE_CONNECT:
217          COUT(3) << "adding event_type_connect to queue" << std::endl;
218        case ENET_EVENT_TYPE_DISCONNECT:
219          //addClient(event);
220          //this is a workaround to ensure thread safety
221          //COUT(5) << "Con.Man: connection event has occured" << std::endl;
222          //break;
223        case ENET_EVENT_TYPE_RECEIVE:
224          //std::cout << "received data" << std::endl;
225          COUT(5) << "Con.Man: receive event has occured" << std::endl;
226          // only add, if client has connected yet and not been disconnected
227          //if(head_->findClient(&event->peer->address))
228            processData(event);
229            event = new ENetEvent;
230//           else
231//             COUT(3) << "received a packet from a client we don't know" << std::endl;
232          break;
233        //case ENET_EVENT_TYPE_DISCONNECT:
234          //clientDisconnect(event->peer);
235          //break;
236        case ENET_EVENT_TYPE_NONE:
237          //receiverThread_->yield();
238          usleep(1000);
239          break;
240      }
241//       usleep(100);
242      //receiverThread_->yield(); //TODO: find apropriate
243    }
244    disconnectClients();
245    // if we're finishied, destroy server
246    {
247      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
248      enet_host_destroy(server);
249      lock.unlock();
250    }
251  }
252 
253  //### added some bugfixes here, but we cannot test them because
254  //### the server crashes everytime because of some gamestates
255  //### (trying to resolve that now)
256  void ConnectionManager::disconnectClients() {
257    ENetEvent event;
258    ClientInformation *temp = head_->next();
259    while(temp!=0){
260      {
261        boost::recursive_mutex::scoped_lock lock(enet_mutex_);
262        enet_peer_disconnect(temp->getPeer(), 0);
263        lock.unlock();
264      }
265      temp = temp->next();
266    }
267    //bugfix: might be the reason why server crashes when clients disconnects
268    temp = head_->next();
269    boost::recursive_mutex::scoped_lock lock(enet_mutex_);
270    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){
271      switch (event.type)
272      {
273      case ENET_EVENT_TYPE_NONE: break;
274      case ENET_EVENT_TYPE_CONNECT: break;
275      case ENET_EVENT_TYPE_RECEIVE:
276        enet_packet_destroy(event.packet);
277        break;
278      case ENET_EVENT_TYPE_DISCONNECT:
279        COUT(4) << "disconnecting all clients" << std::endl;
280        if(head_->findClient(&(event.peer->address)))
281          delete head_->findClient(&(event.peer->address));
282        //maybe needs bugfix: might also be a reason for the server to crash
283        temp = temp->next();
284        break;
285      }
286    }
287    return;
288  }
289
290  bool ConnectionManager::processData(ENetEvent *event) {
291    // just add packet to the buffer
292    // this can be extended with some preprocessing
293    return buffer.push(event);
294  }
295
296
297
298  int ConnectionManager::getClientID(ENetPeer peer) {
299    return getClientID(peer.address);
300  }
301
302  int ConnectionManager::getClientID(ENetAddress address) {
303    return head_->findClient(&address)->getID();
304  }
305
306  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
307    return head_->findClient(clientID)->getPeer();
308  }
309
310  void ConnectionManager::syncClassid(int clientID) {
311    unsigned int network_id=0, failures=0;
312    std::string classname;
313    orxonox::Identifier *id;
314    std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin();
315    while(it != orxonox::Factory::getFactoryEnd()){
316      id = (*it).second;
317      if(id == NULL)
318        continue;
319      classname = id->getName();
320      network_id = id->getNetworkID();
321      if(network_id==0)
322        COUT(3) << "we got a null class id: " << id->getName() << std::endl;
323      COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl;
324
325      while(!addPacket(packet_gen.clid( (int)network_id, classname ), clientID) && failures < 10){
326        failures++;
327      }
328      ++it;
329    }
330    //sendPackets();
331    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
332  }
333
334 
335 
336  bool ConnectionManager::removeShip(ClientInformation *client){
337    int id=client->getShipID();
338    orxonox::Iterator<orxonox::SpaceShip> it;
339    for(it = orxonox::ObjectList<orxonox::SpaceShip>::start(); it; ++it){
340      if(it->objectID!=id)
341        continue;
342      delete *it;
343    }
344    return true;
345  }
346 
347  bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){
348    if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){
349      //sendPackets();
350      return true;
351    }else
352      return false;
353  }
354 
355  void ConnectionManager::disconnectClient(ClientInformation *client){
356    {
357      boost::recursive_mutex::scoped_lock lock(enet_mutex_);
358      enet_peer_disconnect(client->getPeer(), 0);
359      lock.unlock();
360    }
361    removeShip(client);
362  }
363 
364  bool ConnectionManager::addFakeConnectRequest(ENetEvent *ev){
365    ENetEvent event;
366    event.peer=ev->peer;
367    event.packet = packet_gen.generateConnectRequest();
368    return buffer.push(&event);
369  }
370 
371 
372
373}
Note: See TracBrowser for help on using the repository browser.