Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1056 was 1056, checked in by landauf, 17 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

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