Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 569 was 514, checked in by nicolasc, 17 years ago

added copyright notice

File size: 5.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Oliver Scheuss, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28//
29// C++ Interface: ClientConnection
30//
31// Description: The Class ClientConnection manages the servers conenctions to the clients.
32// each connection is provided by a new process. communication between master process and
33// connection processes is provided by ...
34//
35//
36// Author:  Oliver Scheuss
37//
38
39#include "ClientConnection.h"
40
41#ifdef WIN32
42#include <windows.h>
43#define usleep(x) Sleep((x)/1000)
44#else
45#include <unistd.h>
46#endif
47
48namespace network{
49
50  static boost::thread_group network_threads;
51
52  ClientConnection::ClientConnection(int port, std::string address){
53    quit=false;
54    server=NULL;
55    enet_address_set_host(&serverAddress, address.c_str());
56    serverAddress.port = NETWORK_PORT;
57    established=false;
58  }
59
60  ClientConnection::ClientConnection(int port, const char *address){
61    quit=false;
62    server=NULL;
63    enet_address_set_host(&serverAddress, address);
64    serverAddress.port = NETWORK_PORT;
65    established=false;
66  }
67
68  bool ClientConnection::waitEstablished(int milisec){
69    for(int i=0; i<=milisec && !established; i++)
70      usleep(1000);
71
72    return established;
73  }
74
75
76  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
77    if(!buffer.isEmpty())
78      return buffer.pop(address);
79    else
80        return NULL;
81  }
82
83  ENetPacket *ClientConnection::getPacket(){
84    ENetAddress address;
85    return getPacket(address);
86  }
87
88  bool ClientConnection::queueEmpty(){
89    return buffer.isEmpty();
90  }
91
92  bool ClientConnection::createConnection(){
93    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
94    // wait 10 seconds for the connection to be established
95    return waitEstablished(10000);
96  }
97
98  bool ClientConnection::closeConnection(){
99    quit=true;
100    network_threads.join_all();
101    established=false;
102    return true;
103  }
104
105
106  bool ClientConnection::addPacket(ENetPacket *packet){
107    if(server==NULL)
108      return false;
109    if(enet_peer_send(server, 1, packet)!=0)
110      return false;
111    return true;
112  }
113
114  bool ClientConnection::sendPackets(ENetEvent *event){
115    if(server==NULL)
116      return false;
117    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
118      return true;}
119    else
120      return false;
121  }
122
123  bool ClientConnection::sendPackets(){
124    ENetEvent event;
125    if(server==NULL)
126      return false;
127    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
128      return true;}
129    else
130      return false;
131  }
132
133  void ClientConnection::receiverThread(){
134    // what about some error-handling here ?
135    enet_initialize();
136    atexit(enet_deinitialize);
137    ENetEvent event;
138    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
139    if(client==NULL)
140      // add some error handling here ==========================
141      quit=true;
142    //connect to the server
143    if(!establishConnection()){
144      quit=true;
145      return;
146    }
147    //main loop
148    while(!quit){
149      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
150        // we should never reach this point
151        quit=true;
152        // add some error handling here ========================
153      }
154      switch(event.type){
155        // log handling ================
156      case ENET_EVENT_TYPE_CONNECT:
157      case ENET_EVENT_TYPE_RECEIVE:
158        std::cout << "got packet" << std::endl;
159        processData(&event);
160        break;
161      case ENET_EVENT_TYPE_DISCONNECT:
162        quit=true;
163        // server closed the connection
164        return;
165        break;
166      case ENET_EVENT_TYPE_NONE:
167        continue;
168      }
169    }
170    // now disconnect
171
172    if(!disconnectConnection())
173    // if disconnecting failed destroy conn.
174      enet_peer_reset(server);
175    return;
176  }
177
178  bool ClientConnection::disconnectConnection(){
179    ENetEvent event;
180    enet_peer_disconnect(server, 0);
181    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
182      switch (event.type)
183      {
184        case ENET_EVENT_TYPE_NONE:
185        case ENET_EVENT_TYPE_CONNECT:
186        case ENET_EVENT_TYPE_RECEIVE:
187          enet_packet_destroy(event.packet);
188          break;
189        case ENET_EVENT_TYPE_DISCONNECT:
190          return true;
191      }
192    }
193    enet_peer_reset(server);
194    return false;
195  }
196
197  bool ClientConnection::establishConnection(){
198    ENetEvent event;
199    // connect to peer
200    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
201    if(server==NULL)
202      // error handling
203      return false;
204    // handshake
205    if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
206      established=true;
207      return true;
208    }
209    else
210      return false;
211  }
212
213  bool ClientConnection::processData(ENetEvent *event){
214    // just add packet to the buffer
215    // this can be extended with some preprocessing
216    return buffer.push(event);
217  }
218
219
220}
Note: See TracBrowser for help on using the repository browser.