Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/ClientConnection.cc @ 943

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

some bugfixing/debug output

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"
[891]45#include "orxonox/core/Debug.h"
[777]46#include "ClientConnection.h"
[448]47
[777]48namespace network
49{
[346]50  static boost::thread_group network_threads;
[285]51
[777]52  ClientConnection::ClientConnection(int port, std::string address) {
[217]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
[777]60  ClientConnection::ClientConnection(int port, const char *address) {
[217]61    quit=false;
62    server=NULL;
63    enet_address_set_host(&serverAddress, address);
64    serverAddress.port = NETWORK_PORT;
65    established=false;
66  }
[285]67
[777]68  bool ClientConnection::waitEstablished(int milisec) {
[217]69    for(int i=0; i<=milisec && !established; i++)
70      usleep(1000);
[448]71
[217]72    return established;
73  }
[285]74
75
[777]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{
[777]82      return NULL;
[632]83    }
[217]84  }
[285]85
[777]86  ENetPacket *ClientConnection::getPacket() {
[369]87    ENetAddress address;
88    return getPacket(address);
89  }
[514]90
[777]91  bool ClientConnection::queueEmpty() {
[217]92    return buffer.isEmpty();
93  }
[285]94
[777]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
[777]101  bool ClientConnection::closeConnection() {
[217]102    quit=true;
103    network_threads.join_all();
104    established=false;
105    return true;
106  }
[285]107
108
[777]109  bool ClientConnection::addPacket(ENetPacket *packet) {
[217]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
[777]117  bool ClientConnection::sendPackets(ENetEvent *event) {
[217]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
[777]126  bool ClientConnection::sendPackets() {
[229]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
[777]136  void ClientConnection::receiverThread() {
[217]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:
[889]162        COUT(5) << "receiver-Thread: got new 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())
[777]177      // if disconnecting failed destroy conn.
[217]178      enet_peer_reset(server);
179    return;
180  }
[285]181
[777]182  bool ClientConnection::disconnectConnection() {
[217]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      {
[777]188      case ENET_EVENT_TYPE_NONE:
189      case ENET_EVENT_TYPE_CONNECT:
190      case ENET_EVENT_TYPE_RECEIVE:
191        enet_packet_destroy(event.packet);
192        break;
193      case ENET_EVENT_TYPE_DISCONNECT:
194        return true;
[217]195      }
196    }
197    enet_peer_reset(server);
[352]198    return false;
[217]199  }
[285]200
[777]201  bool ClientConnection::establishConnection() {
[217]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
[777]217  bool ClientConnection::processData(ENetEvent *event) {
[889]218    COUT(5) << "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
[217]224}
Note: See TracBrowser for help on using the repository browser.