Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lod2/src/network/ConnectionManager.cc @ 2190

Last change on this file since 2190 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

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