Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 498 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
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 "ClientConnection.h"
13
14#ifdef WIN32
15#include <windows.h>
16#define usleep(x) Sleep((x)/1000)
17#else
18#include <unistd.h>
19#endif
20
21namespace network{
22
23  static boost::thread_group network_threads;
24
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  }
32
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  }
40
41  bool ClientConnection::waitEstablished(int milisec){
42    for(int i=0; i<=milisec && !established; i++)
43      usleep(1000);
44
45    return established;
46  }
47
48
49  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
50    if(!buffer.isEmpty())
51      return buffer.pop(address);
52    else
53        return NULL;
54  }
55
56  ENetPacket *ClientConnection::getPacket(){
57    ENetAddress address;
58    return getPacket(address);
59  }
60 
61  bool ClientConnection::queueEmpty(){
62    return buffer.isEmpty();
63  }
64
65  bool ClientConnection::createConnection(){
66    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
67    // wait 10 seconds for the connection to be established
68    return waitEstablished(10000);
69  }
70
71  bool ClientConnection::closeConnection(){
72    quit=true;
73    network_threads.join_all();
74    established=false;
75    return true;
76  }
77
78
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;
84    return true;
85  }
86
87  bool ClientConnection::sendPackets(ENetEvent *event){
88    if(server==NULL)
89      return false;
90    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
91      return true;}
92    else
93      return false;
94  }
95
96  bool ClientConnection::sendPackets(){
97    ENetEvent event;
98    if(server==NULL)
99      return false;
100    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
101      return true;}
102    else
103      return false;
104  }
105
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
116    if(!establishConnection()){
117      quit=true;
118      return;
119    }
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 ================
129      case ENET_EVENT_TYPE_CONNECT:
130      case ENET_EVENT_TYPE_RECEIVE:
131        std::cout << "got packet" << std::endl;
132        processData(&event);
133        break;
134      case ENET_EVENT_TYPE_DISCONNECT:
135        quit=true;
136        // server closed the connection
137        return;
138        break;
139      case ENET_EVENT_TYPE_NONE:
140        continue;
141      }
142    }
143    // now disconnect
144
145    if(!disconnectConnection())
146    // if disconnecting failed destroy conn.
147      enet_peer_reset(server);
148    return;
149  }
150
151  bool ClientConnection::disconnectConnection(){
152    ENetEvent event;
153    enet_peer_disconnect(server, 0);
154    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
155      switch (event.type)
156      {
157        case ENET_EVENT_TYPE_NONE:
158        case ENET_EVENT_TYPE_CONNECT:
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);
167    return false;
168  }
169
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  }
185
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  }
191
192
193}
Note: See TracBrowser for help on using the repository browser.