Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/network/ConnectionManager.cc @ 1155

Last change on this file since 1155 was 1064, checked in by rgrieder, 17 years ago
  • replaced all String2Number with ConvertValue
  • replaced all tokenize with SubString
  • dealt with warnings under msvc
  • removed some warnings by placing casts
  • bugfix in audio: local variable pushed into member variable std::vector
  • updated StableHeaders.h
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 <iostream>
41// boost.thread library for multithreading support
42#include <boost/thread/thread.hpp>
43#include <boost/bind.hpp>
44
45#include "core/CoreIncludes.h"
46#include "ClientInformation.h"
47#include "ConnectionManager.h"
48
49namespace std
50{
51  bool operator< (ENetAddress a, ENetAddress b) {
52    if(a.host <= b.host)
53      return true;
54    else
55      return false;
56  }
57}
58
59namespace network
60{
61  boost::thread_group network_threads;
62
63  ConnectionManager::ConnectionManager(ClientInformation *head) {
64    quit=false;
65    bindAddress.host = ENET_HOST_ANY;
66    bindAddress.port = NETWORK_PORT;
67    head_ = head;
68  }
69
70  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) {
71    quit=false;
72    enet_address_set_host (& bindAddress, address.c_str());
73    bindAddress.port = NETWORK_PORT;
74    head_ = head;
75  }
76
77  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) {
78    quit=false;
79    enet_address_set_host (& bindAddress, address);
80    bindAddress.port = NETWORK_PORT;
81    head_ = head;
82  }
83
84  ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
85    if(!buffer.isEmpty())
86      return buffer.pop(address);
87    else
88      return NULL;
89  }
90
91  ENetPacket *ConnectionManager::getPacket(int &clientID) {
92    ENetAddress address;
93    ENetPacket *packet=getPacket(address);
94    ClientInformation *temp =head_->findClient(&address);
95    clientID=temp->getID();
96    return packet;
97  }
98
99  bool ConnectionManager::queueEmpty() {
100    return buffer.isEmpty();
101  }
102
103  void ConnectionManager::createListener() {
104    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
105    //     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
106    return;
107  }
108
109  bool ConnectionManager::quitListener() {
110    quit=true;
111    network_threads.join_all();
112    return true;
113  }
114
115  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
116    if(enet_peer_send(peer, (enet_uint8)head_->findClient(&(peer->address))->getID() , packet)!=0)
117      return false;
118    return true;
119  }
120
121  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
122    if(enet_peer_send(head_->findClient(clientID)->getPeer(), (enet_uint8)clientID, packet)!=0)
123      return false;
124    return true;
125  }
126
127  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
128    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
129      if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
130        return false;
131    }
132    return true;
133  }
134
135  bool ConnectionManager::sendPackets(ENetEvent *event) {
136    if(server==NULL)
137      return false;
138    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
139      return true;
140    else
141      return false;
142  }
143
144  bool ConnectionManager::sendPackets() {
145    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  void ConnectionManager::receiverThread() {
155    // what about some error-handling here ?
156    enet_initialize();
157    atexit(enet_deinitialize);
158    ENetEvent event;
159    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
160    if(server==NULL){
161      // add some error handling here ==========================
162      quit=true;
163      return;
164    }
165
166    while(!quit){
167      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
168        // we should never reach this point
169        quit=true;
170        // add some error handling here ========================
171      }
172      switch(event.type){
173        // log handling ================
174        case ENET_EVENT_TYPE_CONNECT:
175          addClient(&event);
176          break;
177        case ENET_EVENT_TYPE_RECEIVE:
178          //std::cout << "received data" << std::endl;
179          processData(&event);
180          break;
181        case ENET_EVENT_TYPE_DISCONNECT:
182          // add some error/log handling here
183          clientDisconnect(event.peer);
184          break;
185        case ENET_EVENT_TYPE_NONE:
186          break;
187      }
188    }
189    disconnectClients();
190    // if we're finishied, destroy server
191    enet_host_destroy(server);
192  }
193
194  void ConnectionManager::disconnectClients() {
195    ENetEvent event;
196    ClientInformation *temp = head_->next();
197    while(temp!=0){
198      enet_peer_disconnect(temp->getPeer(), 0);
199      temp = temp->next();
200    }
201    temp = temp->next();
202    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
203      switch (event.type)
204      {
205      case ENET_EVENT_TYPE_NONE:
206      case ENET_EVENT_TYPE_CONNECT:
207      case ENET_EVENT_TYPE_RECEIVE:
208        enet_packet_destroy(event.packet);
209        break;
210      case ENET_EVENT_TYPE_DISCONNECT:
211        std::cout << "disconnecting client" << std::endl;
212        delete head_->findClient(&(event.peer->address));
213        temp = temp->next();
214        break;
215      }
216    }
217    return;
218  }
219
220  bool ConnectionManager::processData(ENetEvent *event) {
221    // just add packet to the buffer
222    // this can be extended with some preprocessing
223    return buffer.push(event);
224  }
225
226  //bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
227  //  return clientDisconnect(*peer);
228  //}
229
230  bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
231    return head_->removeClient(peer);
232  }
233
234  bool ConnectionManager::addClient(ENetEvent *event) {
235    ClientInformation *temp = head_->insertBack(new ClientInformation);
236    if(temp->prev()->head)
237      temp->setID(1);
238    else
239      temp->setID(temp->prev()->getID()+1);
240    temp->setPeer(event->peer);
241    std::cout << "added client id: " << temp->getID() << std::endl;
242    syncClassid(temp->getID());
243    temp->setSynched(true);
244    return true;
245  }
246
247  int ConnectionManager::getClientID(ENetPeer peer) {
248    return getClientID(peer.address);
249  }
250
251  int ConnectionManager::getClientID(ENetAddress address) {
252    return head_->findClient(&address)->getID();
253  }
254
255  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
256    return head_->findClient(clientID)->getPeer();
257  }
258
259  void ConnectionManager::syncClassid(int clientID) {
260    unsigned int network_id=0;
261    std::string classname;
262    orxonox::Identifier *id;
263    std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin();
264    while(it != orxonox::Factory::getFactoryEnd()){
265      id = (*it).second;
266      if(id == NULL)
267        continue;
268      classname = id->getName();
269      network_id = id->getNetworkID();
270      COUT(4) << "network_id: " << network_id << ", classname: " << classname << std::endl;
271
272      addPacket(packet_gen.clid( (int)network_id, classname ), clientID);
273
274      ++it;
275    }
276    sendPackets();
277  }
278
279
280
281  void ConnectionManager::addClientsObjectID( int clientID, int objectID ) {
282    COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl;
283    clientsShip.insert( std::make_pair( clientID, objectID ) );
284  }
285
286  int ConnectionManager::getClientsShipID( int clientID ) {
287    return clientsShip[clientID];
288  }
289
290  int ConnectionManager::getObjectsClientID( int objectID ) {
291    std::map<int, int>::iterator iter = clientsShip.begin();
292    while( iter != clientsShip.end() ) {
293      if( iter->second == objectID ) return iter->first;
294    }
295    return -99;
296  }
297
298  void ConnectionManager::deleteClientIDReg( int clientID ) {
299    clientsShip.erase( clientID );
300  }
301
302  void ConnectionManager::deleteObjectIDReg( int objectID ) {
303    std::map<int, int>::iterator iter = clientsShip.begin();
304    while( iter != clientsShip.end() ) {
305      if( iter->second == objectID ) break;
306    }
307    clientsShip.erase( iter->first );
308  }
309
310}
Note: See TracBrowser for help on using the repository browser.