Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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