Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 658 was 632, checked in by dumenim, 17 years ago

networkstuff bluber fubber

File size: 5.8 KB
RevLine 
[514]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
[217]28//
29// C++ Interface: ClientConnection
30//
[285]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
[217]33// connection processes is provided by ...
34//
35//
36// Author:  Oliver Scheuss
37//
38
[285]39#include "ClientConnection.h"
[217]40
[448]41#ifdef WIN32
42#include <windows.h>
43#define usleep(x) Sleep((x)/1000)
44#else
45#include <unistd.h>
46#endif
47
[217]48namespace network{
[285]49
[346]50  static boost::thread_group network_threads;
[285]51
[217]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  }
[285]59
[217]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  }
[285]67
[217]68  bool ClientConnection::waitEstablished(int milisec){
69    for(int i=0; i<=milisec && !established; i++)
70      usleep(1000);
[448]71
[217]72    return established;
73  }
[285]74
75
[217]76  ENetPacket *ClientConnection::getPacket(ENetAddress &address){
[632]77    if(!buffer.isEmpty()) {
78      //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
[217]79      return buffer.pop(address);
[632]80    }
81    else{
[217]82        return NULL;
[632]83    }
[217]84  }
[285]85
[369]86  ENetPacket *ClientConnection::getPacket(){
87    ENetAddress address;
88    return getPacket(address);
89  }
[514]90
[217]91  bool ClientConnection::queueEmpty(){
92    return buffer.isEmpty();
93  }
[285]94
[229]95  bool ClientConnection::createConnection(){
[217]96    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
[229]97    // wait 10 seconds for the connection to be established
98    return waitEstablished(10000);
[217]99  }
[285]100
[217]101  bool ClientConnection::closeConnection(){
102    quit=true;
103    network_threads.join_all();
104    established=false;
105    return true;
106  }
[285]107
108
[217]109  bool ClientConnection::addPacket(ENetPacket *packet){
110    if(server==NULL)
111      return false;
112    if(enet_peer_send(server, 1, packet)!=0)
113      return false;
[448]114    return true;
[217]115  }
[285]116
[217]117  bool ClientConnection::sendPackets(ENetEvent *event){
118    if(server==NULL)
119      return false;
[448]120    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
121      return true;}
[285]122    else
[217]123      return false;
124  }
[285]125
[229]126  bool ClientConnection::sendPackets(){
127    ENetEvent event;
128    if(server==NULL)
129      return false;
[448]130    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
131      return true;}
[285]132    else
[229]133      return false;
134  }
[285]135
[217]136  void ClientConnection::receiverThread(){
137    // what about some error-handling here ?
138    enet_initialize();
139    atexit(enet_deinitialize);
140    ENetEvent event;
141    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
142    if(client==NULL)
143      // add some error handling here ==========================
144      quit=true;
145    //connect to the server
[369]146    if(!establishConnection()){
[217]147      quit=true;
[369]148      return;
149    }
[217]150    //main loop
151    while(!quit){
[605]152      //std::cout << "connection loop" << std::endl;
[620]153      if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
[217]154        // we should never reach this point
155        quit=true;
156        // add some error handling here ========================
157      }
158      switch(event.type){
159        // log handling ================
[352]160      case ENET_EVENT_TYPE_CONNECT:
[217]161      case ENET_EVENT_TYPE_RECEIVE:
[620]162        //std::cout << "got packet" << std::endl;
[217]163        processData(&event);
164        break;
165      case ENET_EVENT_TYPE_DISCONNECT:
[369]166        quit=true;
167        // server closed the connection
168        return;
[217]169        break;
[352]170      case ENET_EVENT_TYPE_NONE:
171        continue;
[217]172      }
173    }
174    // now disconnect
[285]175
176    if(!disconnectConnection())
[217]177    // if disconnecting failed destroy conn.
178      enet_peer_reset(server);
179    return;
180  }
[285]181
[217]182  bool ClientConnection::disconnectConnection(){
183    ENetEvent event;
[298]184    enet_peer_disconnect(server, 0);
[620]185    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
[217]186      switch (event.type)
187      {
[352]188        case ENET_EVENT_TYPE_NONE:
189        case ENET_EVENT_TYPE_CONNECT:
[217]190        case ENET_EVENT_TYPE_RECEIVE:
191          enet_packet_destroy(event.packet);
192          break;
193        case ENET_EVENT_TYPE_DISCONNECT:
194          return true;
195      }
196    }
197    enet_peer_reset(server);
[352]198    return false;
[217]199  }
[285]200
[217]201  bool ClientConnection::establishConnection(){
202    ENetEvent event;
203    // connect to peer
204    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
205    if(server==NULL)
206      // error handling
207      return false;
208    // handshake
[620]209    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
[217]210      established=true;
211      return true;
212    }
213    else
214      return false;
215  }
[285]216
[217]217  bool ClientConnection::processData(ENetEvent *event){
[620]218    //std::cout << "got packet, pushing to queue" << std::endl;
[217]219    // just add packet to the buffer
220    // this can be extended with some preprocessing
221    return buffer.push(event);
222  }
[285]223
224
[217]225}
Note: See TracBrowser for help on using the repository browser.