Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added gamestatesending

File size: 5.7 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      //std::cout << "connection loop" << std::endl;
150      if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){
151        // we should never reach this point
152        quit=true;
153        // add some error handling here ========================
154      }
155      switch(event.type){
156        // log handling ================
157      case ENET_EVENT_TYPE_CONNECT:
158      case ENET_EVENT_TYPE_RECEIVE:
159        std::cout << "got packet" << std::endl;
160        processData(&event);
161        break;
162      case ENET_EVENT_TYPE_DISCONNECT:
163        quit=true;
164        // server closed the connection
165        return;
166        break;
167      case ENET_EVENT_TYPE_NONE:
168        continue;
169      }
170    }
171    // now disconnect
172
173    if(!disconnectConnection())
174    // if disconnecting failed destroy conn.
175      enet_peer_reset(server);
176    return;
177  }
178
179  bool ClientConnection::disconnectConnection(){
180    ENetEvent event;
181    enet_peer_disconnect(server, 0);
182    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
183      switch (event.type)
184      {
185        case ENET_EVENT_TYPE_NONE:
186        case ENET_EVENT_TYPE_CONNECT:
187        case ENET_EVENT_TYPE_RECEIVE:
188          enet_packet_destroy(event.packet);
189          break;
190        case ENET_EVENT_TYPE_DISCONNECT:
191          return true;
192      }
193    }
194    enet_peer_reset(server);
195    return false;
196  }
197
198  bool ClientConnection::establishConnection(){
199    ENetEvent event;
200    // connect to peer
201    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
202    if(server==NULL)
203      // error handling
204      return false;
205    // handshake
206    if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
207      established=true;
208      return true;
209    }
210    else
211      return false;
212  }
213
214  bool ClientConnection::processData(ENetEvent *event){
215    std::cout << "got packet, pushing to queue" << std::endl;
216    // just add packet to the buffer
217    // this can be extended with some preprocessing
218    return buffer.push(event);
219  }
220
221
222}
Note: See TracBrowser for help on using the repository browser.