Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/ConnectionManager.cc @ 1176

Last change on this file since 1176 was 1168, checked in by scheusso, 17 years ago

added new network files

File size: 10.1 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(){}
64 
65  ConnectionManager::ConnectionManager(ClientInformation *head) {
66    quit=false;
67    bindAddress.host = ENET_HOST_ANY;
68    bindAddress.port = NETWORK_PORT;
69    head_ = head;
70  }
71
72  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) {
73    quit=false;
74    enet_address_set_host (& bindAddress, address.c_str());
75    bindAddress.port = NETWORK_PORT;
76    head_ = head;
77  }
78
79  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) {
80    quit=false;
81    enet_address_set_host (& bindAddress, address);
82    bindAddress.port = NETWORK_PORT;
83    head_ = head;
84  }
85
86  ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {
87    if(!buffer.isEmpty())
88      return buffer.pop(address);
89    else
90      return NULL;
91  }
92/**
93This function only pops the first element in PacketBuffer (first in first out)
94used by processQueue in Server.cc
95*/
96  ENetPacket *ConnectionManager::getPacket(int &clientID) {
97    ENetAddress address;
98    ENetPacket *packet=getPacket(address);
99    ClientInformation *temp =head_->findClient(&address);
100    clientID=temp->getID();
101    return packet;
102  }
103
104  bool ConnectionManager::queueEmpty() {
105    return buffer.isEmpty();
106  }
107
108  void ConnectionManager::createListener() {
109    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
110    //     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
111    return;
112  }
113
114  bool ConnectionManager::quitListener() {
115    quit=true;
116    network_threads.join_all();
117    return true;
118  }
119
120  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {
121    if(enet_peer_send(peer, (enet_uint8)head_->findClient(&(peer->address))->getID() , packet)!=0)
122      return false;
123    return true;
124  }
125
126  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {
127    if(enet_peer_send(head_->findClient(clientID)->getPeer(), (enet_uint8)clientID, packet)!=0)
128      return false;
129    return true;
130  }
131
132  bool ConnectionManager::addPacketAll(ENetPacket *packet) {
133    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
134      if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0)
135        return false;
136    }
137    return true;
138  }
139
140  bool ConnectionManager::sendPackets(ENetEvent *event) {
141    if(server==NULL)
142      return false;
143    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
144      return true;
145    else
146      return false;
147  }
148
149  bool ConnectionManager::sendPackets() {
150    ENetEvent event;
151    if(server==NULL)
152      return false;
153    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
154      return true;
155    else
156      return false;
157  }
158
159  void ConnectionManager::receiverThread() {
160    // what about some error-handling here ?
161    enet_initialize();
162    atexit(enet_deinitialize);
163    ENetEvent event;
164    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
165    if(server==NULL){
166      // add some error handling here ==========================
167      quit=true;
168      return;
169    }
170
171    while(!quit){
172      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
173        // we should never reach this point
174        quit=true;
175        // add some error handling here ========================
176      }
177      switch(event.type){
178        // log handling ================
179        case ENET_EVENT_TYPE_CONNECT:
180          addClient(&event);
181          COUT(5) << "Con.Man: connection event has occured" << std::endl;
182          break;
183        case ENET_EVENT_TYPE_RECEIVE:
184          //std::cout << "received data" << std::endl;
185          COUT(5) << "Con.Man: receive event has occured" << std::endl;
186          processData(&event);
187          break;
188        case ENET_EVENT_TYPE_DISCONNECT:
189          // add some error/log handling here
190          clientDisconnect(event.peer);
191          break;
192        case ENET_EVENT_TYPE_NONE:
193          break;
194      }
195    }
196    disconnectClients();
197    // if we're finishied, destroy server
198    enet_host_destroy(server);
199  }
200 
201  //### added some bugfixes here, but we cannot test them because
202  //### the server crashes everytime because of some gamestates
203  //### (trying to resolve that now)
204  void ConnectionManager::disconnectClients() {
205    ENetEvent event;
206    ClientInformation *temp = head_->next();
207    while(temp!=0){
208      enet_peer_disconnect(temp->getPeer(), 0);
209      temp = temp->next();
210    }
211    //bugfix: might be the reason why server crashes when clients disconnects
212    //temp = temp->next();
213    temp = head_->next();
214    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
215      switch (event.type)
216      {
217      case ENET_EVENT_TYPE_NONE: break;
218      case ENET_EVENT_TYPE_CONNECT: break;
219      case ENET_EVENT_TYPE_RECEIVE:
220        enet_packet_destroy(event.packet);
221        break;
222      case ENET_EVENT_TYPE_DISCONNECT:
223        COUT(4) << "disconnecting all clients" << std::endl;
224        delete head_->findClient(&(event.peer->address));
225        //maybe needs bugfix: might also be a reason for the server to crash
226        temp = temp->next();
227        break;
228      }
229    }
230    return;
231  }
232
233  bool ConnectionManager::processData(ENetEvent *event) {
234    // just add packet to the buffer
235    // this can be extended with some preprocessing
236    return buffer.push(event);
237  }
238
239  //bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
240  //  return clientDisconnect(*peer);
241  //}
242
243  bool ConnectionManager::clientDisconnect(ENetPeer *peer) {
244    COUT(4) << "removing client from list" << std::endl;
245    return head_->removeClient(peer);
246  }
247/**
248This function adds a client that connects to the clientlist of the server
249NOTE: if you change this, don't forget to change the test function
250addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid
251*/
252  bool ConnectionManager::addClient(ENetEvent *event) {
253    ClientInformation *temp = head_->insertBack(new ClientInformation);
254    if(temp->prev()->head) { //not good if you use anything else than insertBack
255      temp->prev()->setID(0); //bugfix: not necessary but usefull
256      temp->setID(1);
257    }
258    else
259      temp->setID(temp->prev()->getID()+1);
260    temp->setPeer(event->peer);
261    COUT(4) << "Con.Man: added client id: " << temp->getID() << std::endl;
262    syncClassid(temp->getID());
263    temp->setSynched(true);
264    return true;
265  }
266
267  int ConnectionManager::getClientID(ENetPeer peer) {
268    return getClientID(peer.address);
269  }
270
271  int ConnectionManager::getClientID(ENetAddress address) {
272    return head_->findClient(&address)->getID();
273  }
274
275  ENetPeer *ConnectionManager::getClientPeer(int clientID) {
276    return head_->findClient(clientID)->getPeer();
277  }
278
279  void ConnectionManager::syncClassid(int clientID) {
280    unsigned int network_id=0;
281    std::string classname;
282    orxonox::Identifier *id;
283    std::map<std::string, orxonox::Identifier*>::const_iterator it = orxonox::Factory::getFactoryBegin();
284    while(it != orxonox::Factory::getFactoryEnd()){
285      id = (*it).second;
286      if(id == NULL)
287        continue;
288      classname = id->getName();
289      network_id = id->getNetworkID();
290      COUT(4) << "Con.Man:syncClassid:\tnetwork_id: " << network_id << ", classname: " << classname << std::endl;
291
292      addPacket(packet_gen.clid( (int)network_id, classname ), clientID);
293
294      ++it;
295    }
296    sendPackets();
297    COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl;
298  }
299
300
301
302  void ConnectionManager::addClientsObjectID( int clientID, int objectID ) {
303    COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl;
304    clientsShip.insert( std::make_pair( clientID, objectID ) );
305  }
306
307  int ConnectionManager::getClientsShipID( int clientID ) {
308    return clientsShip[clientID];
309  }
310
311  int ConnectionManager::getObjectsClientID( int objectID ) {
312    std::map<int, int>::iterator iter;
313    for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
314      if( iter->second == objectID ) return iter->first;
315    }
316    return -99;
317  }
318
319  void ConnectionManager::deleteClientIDReg( int clientID ) {
320    clientsShip.erase( clientID );
321  }
322
323  void ConnectionManager::deleteObjectIDReg( int objectID ) {
324    std::map<int, int>::iterator iter = clientsShip.begin();
325    for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {
326      if( iter->second == objectID ) break;
327    }
328    clientsShip.erase( iter->first );
329  }
330  int ConnectionManager::getNumberOfClients() {
331    return clientsShip.size();
332  }
333}
Note: See TracBrowser for help on using the repository browser.