Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/ClientConnection.cc @ 482

Last change on this file since 482 was 448, checked in by scheusso, 17 years ago

some bugfixes(clientinformation, client), enhancements(client, server) and tests(chatserver/client)

File size: 4.7 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
[448]14#ifdef WIN32
15#include <windows.h>
16#define usleep(x) Sleep((x)/1000)
17#else
18#include <unistd.h>
19#endif
20
[217]21namespace network{
[285]22
[346]23  static boost::thread_group network_threads;
[285]24
[217]25  ClientConnection::ClientConnection(int port, std::string address){
26    quit=false;
27    server=NULL;
28    enet_address_set_host(&serverAddress, address.c_str());
29    serverAddress.port = NETWORK_PORT;
30    established=false;
31  }
[285]32
[217]33  ClientConnection::ClientConnection(int port, const char *address){
34    quit=false;
35    server=NULL;
36    enet_address_set_host(&serverAddress, address);
37    serverAddress.port = NETWORK_PORT;
38    established=false;
39  }
[285]40
[217]41  bool ClientConnection::waitEstablished(int milisec){
42    for(int i=0; i<=milisec && !established; i++)
43      usleep(1000);
[448]44
[217]45    return established;
46  }
[285]47
48
[217]49  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
50    if(!buffer.isEmpty())
51      return buffer.pop(address);
52    else
53        return NULL;
54  }
[285]55
[369]56  ENetPacket *ClientConnection::getPacket(){
57    ENetAddress address;
58    return getPacket(address);
59  }
60 
[217]61  bool ClientConnection::queueEmpty(){
62    return buffer.isEmpty();
63  }
[285]64
[229]65  bool ClientConnection::createConnection(){
[217]66    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
[229]67    // wait 10 seconds for the connection to be established
68    return waitEstablished(10000);
[217]69  }
[285]70
[217]71  bool ClientConnection::closeConnection(){
72    quit=true;
73    network_threads.join_all();
74    established=false;
75    return true;
76  }
[285]77
78
[217]79  bool ClientConnection::addPacket(ENetPacket *packet){
80    if(server==NULL)
81      return false;
82    if(enet_peer_send(server, 1, packet)!=0)
83      return false;
[448]84    return true;
[217]85  }
[285]86
[217]87  bool ClientConnection::sendPackets(ENetEvent *event){
88    if(server==NULL)
89      return false;
[448]90    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
91      return true;}
[285]92    else
[217]93      return false;
94  }
[285]95
[229]96  bool ClientConnection::sendPackets(){
97    ENetEvent event;
98    if(server==NULL)
99      return false;
[448]100    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
101      return true;}
[285]102    else
[229]103      return false;
104  }
[285]105
[217]106  void ClientConnection::receiverThread(){
107    // what about some error-handling here ?
108    enet_initialize();
109    atexit(enet_deinitialize);
110    ENetEvent event;
111    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
112    if(client==NULL)
113      // add some error handling here ==========================
114      quit=true;
115    //connect to the server
[369]116    if(!establishConnection()){
[217]117      quit=true;
[369]118      return;
119    }
[217]120    //main loop
121    while(!quit){
122      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
123        // we should never reach this point
124        quit=true;
125        // add some error handling here ========================
126      }
127      switch(event.type){
128        // log handling ================
[352]129      case ENET_EVENT_TYPE_CONNECT:
[217]130      case ENET_EVENT_TYPE_RECEIVE:
[448]131        std::cout << "got packet" << std::endl;
[217]132        processData(&event);
133        break;
134      case ENET_EVENT_TYPE_DISCONNECT:
[369]135        quit=true;
136        // server closed the connection
137        return;
[217]138        break;
[352]139      case ENET_EVENT_TYPE_NONE:
140        continue;
[217]141      }
142    }
143    // now disconnect
[285]144
145    if(!disconnectConnection())
[217]146    // if disconnecting failed destroy conn.
147      enet_peer_reset(server);
148    return;
149  }
[285]150
[217]151  bool ClientConnection::disconnectConnection(){
152    ENetEvent event;
[298]153    enet_peer_disconnect(server, 0);
[217]154    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
155      switch (event.type)
156      {
[352]157        case ENET_EVENT_TYPE_NONE:
158        case ENET_EVENT_TYPE_CONNECT:
[217]159        case ENET_EVENT_TYPE_RECEIVE:
160          enet_packet_destroy(event.packet);
161          break;
162        case ENET_EVENT_TYPE_DISCONNECT:
163          return true;
164      }
165    }
166    enet_peer_reset(server);
[352]167    return false;
[217]168  }
[285]169
[217]170  bool ClientConnection::establishConnection(){
171    ENetEvent event;
172    // connect to peer
173    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
174    if(server==NULL)
175      // error handling
176      return false;
177    // handshake
178    if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
179      established=true;
180      return true;
181    }
182    else
183      return false;
184  }
[285]185
[217]186  bool ClientConnection::processData(ENetEvent *event){
187    // just add packet to the buffer
188    // this can be extended with some preprocessing
189    return buffer.push(event);
190  }
[285]191
192
[217]193}
Note: See TracBrowser for help on using the repository browser.