Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/ClientConnection.cc @ 909

Last change on this file since 909 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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