Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/network/ConnectionManager.cc @ 2988

Last change on this file since 2988 was 2965, checked in by scheusso, 16 years ago

some fixes (bidirectional variables, …), some changes (client tickrate, connection handling)

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