Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/ClientConnection.cc @ 217

Last change on this file since 217 was 217, checked in by scheusso, 16 years ago

-implemented class ClientConnection:

  • implements the network part of a client (analog to ConnectionManager for the server)
  • again there is a receiver-Thread and a buffer
  • (hopefully) clean disconnection and wait for connection

-extended dummyclient (dummyclient2.cc) to use new ClientConnection class
-make target client builds now dummyclient2
-adjusted the Makefile

File size: 4.1 KB
Line 
1//
2// C++ Interface: ClientConnection
3//
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
6// connection processes is provided by ...
7//
8//
9// Author:  Oliver Scheuss
10//
11
12#include "network/ClientConnection.h"
13
14namespace network{
15 
16  boost::thread_group network_threads;
17 
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  }
25 
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  }
33 
34  bool ClientConnection::waitEstablished(int milisec){
35    for(int i=0; i<=milisec && !established; i++)
36      usleep(1000);
37    return established;
38  }
39 
40 
41  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
42    if(!buffer.isEmpty())
43      return buffer.pop(address);
44    else
45        return NULL;
46  }
47 
48  bool ClientConnection::queueEmpty(){
49    return buffer.isEmpty();
50  }
51 
52  void ClientConnection::createConnection(){
53    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
54//     boost::thread thr(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
55    return;
56  }
57 
58  bool ClientConnection::closeConnection(){
59    quit=true;
60    network_threads.join_all();
61    established=false;
62    return true;
63  }
64 
65 
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  }
74 
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;
80    else 
81      return false;
82  }
83 
84  void ClientConnection::receiverThread(){
85    // what about some error-handling here ?
86    enet_initialize();
87    atexit(enet_deinitialize);
88    ENetEvent event;
89    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
90    if(client==NULL)
91      // add some error handling here ==========================
92      quit=true;
93    //connect to the server
94    if(!establishConnection())
95      quit=true;
96    //main loop
97    while(!quit){
98      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
99        // we should never reach this point
100        quit=true;
101        // add some error handling here ========================
102      }
103      switch(event.type){
104        // log handling ================
105      case ENET_EVENT_TYPE_RECEIVE:
106        processData(&event);
107        break;
108      case ENET_EVENT_TYPE_DISCONNECT:
109        // add some error/log handling here
110        // extend =====================
111        break;
112      }
113    }
114    // now disconnect
115   
116    if(!disconnectConnection()) 
117    // if disconnecting failed destroy conn.
118      enet_peer_reset(server);
119    return;
120  }
121 
122  bool ClientConnection::disconnectConnection(){
123    ENetEvent event;
124    enet_peer_disconnect(server, 0);
125    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
126      switch (event.type)
127      {
128        case ENET_EVENT_TYPE_RECEIVE:
129          enet_packet_destroy(event.packet);
130          break;
131        case ENET_EVENT_TYPE_DISCONNECT:
132          return true;
133      }
134    }
135    enet_peer_reset(server);
136  }
137 
138  bool ClientConnection::establishConnection(){
139    ENetEvent event;
140    // connect to peer
141    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
142    if(server==NULL)
143      // error handling
144      return false;
145    // handshake
146    if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
147      established=true;
148      return true;
149    }
150    else
151      return false;
152  }
153 
154  bool ClientConnection::processData(ENetEvent *event){
155    // just add packet to the buffer
156    // this can be extended with some preprocessing
157    return buffer.push(event);
158  }
159 
160 
161}
Note: See TracBrowser for help on using the repository browser.