Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/network/ClientConnection.cc @ 473

Last change on this file since 473 was 298, checked in by nicolasc, 17 years ago

some cleanups
eNet code is currently enet-1.1 compatible (incompatible enet-1.0!)

File size: 4.3 KB
RevLine 
[217]1//
2// C++ Interface: ClientConnection
3//
[285]4// Description: The Class ClientConnection manages the servers conenctions to the clients.
5// each connection is provided by a new process. communication between master process and
[217]6// connection processes is provided by ...
7//
8//
9// Author:  Oliver Scheuss
10//
11
[285]12#include "ClientConnection.h"
[217]13
14namespace network{
[285]15
[217]16  boost::thread_group network_threads;
[285]17
[217]18  ClientConnection::ClientConnection(int port, std::string address){
19    quit=false;
20    server=NULL;
21    enet_address_set_host(&serverAddress, address.c_str());
22    serverAddress.port = NETWORK_PORT;
23    established=false;
24  }
[285]25
[217]26  ClientConnection::ClientConnection(int port, const char *address){
27    quit=false;
28    server=NULL;
29    enet_address_set_host(&serverAddress, address);
30    serverAddress.port = NETWORK_PORT;
31    established=false;
32  }
[285]33
[217]34  bool ClientConnection::waitEstablished(int milisec){
35    for(int i=0; i<=milisec && !established; i++)
36      usleep(1000);
37    return established;
38  }
[285]39
40
[217]41  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
42    if(!buffer.isEmpty())
43      return buffer.pop(address);
44    else
45        return NULL;
46  }
[285]47
[217]48  bool ClientConnection::queueEmpty(){
49    return buffer.isEmpty();
50  }
[285]51
[229]52  bool ClientConnection::createConnection(){
[217]53    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
[229]54    // wait 10 seconds for the connection to be established
55    return waitEstablished(10000);
[217]56  }
[285]57
[217]58  bool ClientConnection::closeConnection(){
59    quit=true;
60    network_threads.join_all();
61    established=false;
62    return true;
63  }
[285]64
65
[217]66  bool ClientConnection::addPacket(ENetPacket *packet){
67    if(server==NULL)
68      return false;
69    if(enet_peer_send(server, 1, packet)!=0)
70      return false;
71    else
72      return true;
73  }
[285]74
[217]75  bool ClientConnection::sendPackets(ENetEvent *event){
76    if(server==NULL)
77      return false;
78    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0)
79      return true;
[285]80    else
[217]81      return false;
82  }
[285]83
[229]84  bool ClientConnection::sendPackets(){
85    ENetEvent event;
86    if(server==NULL)
87      return false;
88    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0)
89      return true;
[285]90    else
[229]91      return false;
92  }
[285]93
[217]94  void ClientConnection::receiverThread(){
95    // what about some error-handling here ?
96    enet_initialize();
97    atexit(enet_deinitialize);
98    ENetEvent event;
99    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
100    if(client==NULL)
101      // add some error handling here ==========================
102      quit=true;
103    //connect to the server
104    if(!establishConnection())
105      quit=true;
106    //main loop
107    while(!quit){
108      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
109        // we should never reach this point
110        quit=true;
111        // add some error handling here ========================
112      }
113      switch(event.type){
114        // log handling ================
115      case ENET_EVENT_TYPE_RECEIVE:
116        processData(&event);
117        break;
118      case ENET_EVENT_TYPE_DISCONNECT:
119        // add some error/log handling here
120        // extend =====================
121        break;
122      }
123    }
124    // now disconnect
[285]125
126    if(!disconnectConnection())
[217]127    // if disconnecting failed destroy conn.
128      enet_peer_reset(server);
129    return;
130  }
[285]131
[217]132  bool ClientConnection::disconnectConnection(){
133    ENetEvent event;
[298]134//     enet_peer_disconnect(server);
135    enet_peer_disconnect(server, 0);
[217]136    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
137      switch (event.type)
138      {
139        case ENET_EVENT_TYPE_RECEIVE:
140          enet_packet_destroy(event.packet);
141          break;
142        case ENET_EVENT_TYPE_DISCONNECT:
143          return true;
144      }
145    }
146    enet_peer_reset(server);
147  }
[285]148
[217]149  bool ClientConnection::establishConnection(){
150    ENetEvent event;
151    // connect to peer
152    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
153    if(server==NULL)
154      // error handling
155      return false;
156    // handshake
157    if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
158      established=true;
159      return true;
160    }
161    else
162      return false;
163  }
[285]164
[217]165  bool ClientConnection::processData(ENetEvent *event){
166    // just add packet to the buffer
167    // this can be extended with some preprocessing
168    return buffer.push(event);
169  }
[285]170
171
[217]172}
Note: See TracBrowser for help on using the repository browser.