Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 413 was 369, checked in by scheusso, 17 years ago

PacketDecoder:

Extended Class to make inheriting easier…

-added virtual function, that can be implemented by lower classes

Client:

Added some function, changed some things (input, gamestate, connectionhandling)

Server:

same as 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    else
85      return true;
86  }
87
88  bool ClientConnection::sendPackets(ENetEvent *event){
89    if(server==NULL)
90      return false;
91    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0)
92      return true;
93    else
94      return false;
95  }
96
97  bool ClientConnection::sendPackets(){
98    ENetEvent event;
99    if(server==NULL)
100      return false;
101    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0)
102      return true;
103    else
104      return false;
105  }
106
107  void ClientConnection::receiverThread(){
108    // what about some error-handling here ?
109    enet_initialize();
110    atexit(enet_deinitialize);
111    ENetEvent event;
112    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
113    if(client==NULL)
114      // add some error handling here ==========================
115      quit=true;
116    //connect to the server
117    if(!establishConnection()){
118      quit=true;
119      return;
120    }
121    //main loop
122    while(!quit){
123      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
124        // we should never reach this point
125        quit=true;
126        // add some error handling here ========================
127      }
128      switch(event.type){
129        // log handling ================
130      case ENET_EVENT_TYPE_CONNECT:
131      case ENET_EVENT_TYPE_RECEIVE:
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.