Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/ConnectionManager.cc @ 3102

Last change on this file since 3102 was 3093, checked in by scheusso, 15 years ago

it is now possible to send packets/network function calls to all clients by passing CLIENTID_UNKNOWN as clientID

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