Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/ConnectionManager.cc @ 664

Last change on this file since 664 was 636, checked in by scheusso, 17 years ago

synchronising of classid↔classname works now

File size: 7.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Oliver Scheuss, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28//
29// C++ Interface: ConnectionManager
30//
31// Description: The Class ConnectionManager manages the servers conenctions to the clients.
32// each connection is provided by a new process. communication between master process and
33// connection processes is provided by ...
34//
35//
36// Author:  Oliver Scheuss
37//
38
39#include "ConnectionManager.h"
40
41namespace std{
42  bool operator< (ENetAddress a, ENetAddress b){
43    if(a.host <= b.host)
44      return true;
45    else
46      return false;
47  }
48}
49
50namespace network{
51
52  boost::thread_group network_threads;
53
54  ConnectionManager::ConnectionManager(ClientInformation *head){
55    quit=false;
56    bindAddress.host = ENET_HOST_ANY;
57    bindAddress.port = NETWORK_PORT;
58    head_ = head;
59  }
60
61  ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head){
62    quit=false;
63    enet_address_set_host (& bindAddress, address.c_str());
64    bindAddress.port = NETWORK_PORT;
65    head_ = head;
66  }
67
68  ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head){
69    quit=false;
70    enet_address_set_host (& bindAddress, address);
71    bindAddress.port = NETWORK_PORT;
72    head_ = head;
73  }
74
75  ENetPacket *ConnectionManager::getPacket(ENetAddress &address){
76    if(!buffer.isEmpty())
77      return buffer.pop(address);
78    else
79        return NULL;
80  }
81
82  ENetPacket *ConnectionManager::getPacket(int &clientID){
83    ENetAddress address;
84    ENetPacket *packet=getPacket(address);
85    ClientInformation *temp =head_->findClient(&address);
86    clientID=temp->getID();
87    return packet;
88  }
89
90  bool ConnectionManager::queueEmpty(){
91    return buffer.isEmpty();
92  }
93
94  void ConnectionManager::createListener(){
95    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
96//     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
97    return;
98  }
99
100  bool ConnectionManager::quitListener(){
101    quit=true;
102    network_threads.join_all();
103    return true;
104  }
105
106  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
107    if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0)
108      return false;
109    return true;
110  }
111
112  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
113    if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0)
114      return false;
115    return true;
116  }
117
118  bool ConnectionManager::addPacketAll(ENetPacket *packet){
119    for(ClientInformation *i=head_->next(); i!=0; i=i->next()){
120      if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0)
121         return false;
122    }
123    return true;
124  }
125
126  bool ConnectionManager::sendPackets(ENetEvent *event){
127    if(server==NULL)
128      return false;
129    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
130      return true;
131    else
132      return false;
133  }
134
135  bool ConnectionManager::sendPackets(){
136    ENetEvent event;
137    if(server==NULL)
138      return false;
139    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
140      return true;
141    else
142      return false;
143  }
144
145  void ConnectionManager::receiverThread(){
146    // what about some error-handling here ?
147    enet_initialize();
148    atexit(enet_deinitialize);
149    ENetEvent event;
150    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
151    if(server==NULL){
152      // add some error handling here ==========================
153      quit=true;
154      return;
155    }
156
157    while(!quit){
158      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
159        // we should never reach this point
160        quit=true;
161        // add some error handling here ========================
162      }
163      switch(event.type){
164        // log handling ================
165        case ENET_EVENT_TYPE_CONNECT:
166        addClient(&event);
167        break;
168      case ENET_EVENT_TYPE_RECEIVE:
169        //std::cout << "received data" << std::endl;
170        processData(&event);
171        break;
172      case ENET_EVENT_TYPE_DISCONNECT:
173        // add some error/log handling here
174        clientDisconnect(event.peer);
175        break;
176      case ENET_EVENT_TYPE_NONE:
177        break;
178      }
179    }
180    disconnectClients();
181    // if we're finishied, destroy server
182    enet_host_destroy(server);
183  }
184
185  void ConnectionManager::disconnectClients(){
186    ENetEvent event;
187    ClientInformation *temp = head_->next();
188    while(temp!=0){
189      enet_peer_disconnect(temp->getPeer(), 0);
190      temp = temp->next();
191    }
192    temp = temp->next();
193    while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
194      switch (event.type)
195      {
196        case ENET_EVENT_TYPE_NONE:
197        case ENET_EVENT_TYPE_CONNECT:
198        case ENET_EVENT_TYPE_RECEIVE:
199          enet_packet_destroy(event.packet);
200          break;
201        case ENET_EVENT_TYPE_DISCONNECT:
202          std::cout << "disconnecting client" << std::endl;
203          delete head_->findClient(&(event.peer->address));
204          temp = temp->next();
205          break;
206      }
207    }
208    return;
209  }
210
211  bool ConnectionManager::processData(ENetEvent *event){
212    // just add packet to the buffer
213    // this can be extended with some preprocessing
214    return buffer.push(event);
215  }
216
217//   bool ConnectionManager::clientDisconnect(ENetPeer *peer){
218//     return clientDisconnect(*peer);
219//   }
220
221
222
223  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
224    return head_->removeClient(peer);
225    return true;
226  }
227
228  bool ConnectionManager::addClient(ENetEvent *event){
229    ClientInformation *temp = head_->insertBack(new ClientInformation);
230    if(temp->prev()->head)
231      temp->setID(1);
232    else
233      temp->setID(temp->prev()->getID()+1);
234    temp->setPeer(event->peer);
235    std::cout << "added client id: " << temp->getID() << std::endl;
236    syncClassid(temp->getID());
237    temp->setSynched(true);
238    return true;
239  }
240
241  int ConnectionManager::getClientID(ENetPeer peer){
242    return getClientID(peer.address);
243  }
244
245  int ConnectionManager::getClientID(ENetAddress address){
246    return head_->findClient(&address)->getID();
247  }
248
249  ENetPeer *ConnectionManager::getClientPeer(int clientID){
250    return head_->findClient(clientID)->getPeer();
251  }
252
253  void ConnectionManager::syncClassid(int clientID){
254    int i=0;
255    std::string classname;
256    bool abort=false;
257    orxonox::Identifier *id;
258    while(!abort){
259      id = ID(i);
260      std::cout << "syncid: " << i << ", ID(id): " << id << std::endl;
261      if(id == NULL){
262        if(i!=0)
263          abort=true;
264        else{
265          ++i;
266          continue;
267        }
268      }
269      else{
270        classname = id->getName();
271        addPacket(packet_gen.clid( i, classname ),clientID);
272      }
273      ++i;
274    }
275    sendPackets();
276  }
277
278}
Note: See TracBrowser for help on using the repository browser.