Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound/src/network/ClientConnection.cc @ 2829

Last change on this file since 2829 was 2773, checked in by rgrieder, 16 years ago

Removed all enet and boost includes from header files in the network library.

  • Reduces dependencies
  • Minimises problems with windows.h
  • Speeds up the compiling process a little bit (probably negligible)
  • Also removes ugly WIN32_LEAN_AND_MEAN declarations before every enet.h include in the network library.

Removed windows.h header from util/Sleep.h by adding Sleep.cc

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
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 */
28
29//
30// C++ Interface: ClientConnection
31//
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
34// connection processes is provided by ...
35//
36//
37// Author:  Oliver Scheuss
38//
39
40#include "ClientConnection.h"
41
42#include <enet/enet.h>
43#include <iostream>
44// boost.thread library for multithreading support
45#include <boost/thread/thread.hpp>
46#include <boost/bind.hpp>
47#include <boost/thread/recursive_mutex.hpp>
48
49#include "util/Sleep.h"
50#include "util/Debug.h"
51
52namespace orxonox
53{
54  //static boost::thread_group network_threads;
55
56  static boost::recursive_mutex enet_mutex_g;
57
58  ClientConnection::ClientConnection(int port, const std::string& address) {
59    quit=false;
60    server=NULL;
61    serverAddress = new ENetAddress();
62    enet_address_set_host(serverAddress, address.c_str());
63    serverAddress->port = port;
64    established=false;
65  }
66
67  ClientConnection::ClientConnection(int port, const char *address) {
68    quit=false;
69    server=NULL;
70    serverAddress = new ENetAddress();
71    enet_address_set_host(serverAddress, address);
72    serverAddress->port = port;
73    established=false;
74  }
75
76  bool ClientConnection::waitEstablished(int milisec) {
77    for(int i=0; i<=milisec && !established; i++)
78      msleep(1);
79
80    return established;
81  }
82
83  ClientConnection::~ClientConnection(){
84    if(established)
85      closeConnection();
86    delete serverAddress; // surely was created
87  }
88
89  ENetEvent *ClientConnection::getEvent(){
90    if(!buffer.isEmpty())
91      return buffer.pop();
92    else
93      return NULL;
94  }
95
96  bool ClientConnection::queueEmpty() {
97    return buffer.isEmpty();
98  }
99
100  bool ClientConnection::createConnection() {
101    receiverThread_ = new boost::thread(boost::bind(&ClientConnection::receiverThread, this));
102    //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
103    // wait 10 seconds for the connection to be established
104    return waitEstablished(NETWORK_CLIENT_CONNECT_TIMEOUT);
105  }
106
107  bool ClientConnection::closeConnection() {
108    quit=true;
109    //network_threads.join_all();
110    receiverThread_->join();
111    established=false;
112    return true;
113  }
114
115
116  bool ClientConnection::addPacket(ENetPacket *packet) {
117    if(server==NULL)
118      return false;
119    if(packet==NULL){
120      COUT(3) << "Cl.con: addpacket: invalid packet" << std::endl;
121      return false;
122    }
123    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
124    if(enet_peer_send(server, 0, packet)<0)
125      return false;
126    else
127      return true;
128  }
129
130  bool ClientConnection::sendPackets() {
131    if(server==NULL)
132      return false;
133    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
134    enet_host_flush(client);
135    lock.unlock();
136    return true;
137  }
138
139  void ClientConnection::receiverThread() {
140    // what about some error-handling here ?
141    atexit(enet_deinitialize);
142    ENetEvent *event;
143    {
144      boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
145      enet_initialize();
146      client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
147      lock.unlock();
148    }
149    if(client==NULL) {
150      COUT(2) << "ClientConnection: could not create client host" << std::endl;
151      // add some error handling here ==========================
152      quit=true;
153    }
154    //connect to the server
155    if(!establishConnection()){
156      COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl;
157      quit=true;
158      return;
159    }
160    event = new ENetEvent;
161    //main loop
162    while(!quit){
163      //std::cout << "connection loop" << std::endl;
164      {
165        boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
166        if(enet_host_service(client, event, NETWORK_CLIENT_WAIT_TIME)<0){
167          // we should never reach this point
168          quit=true;
169          continue;
170          // add some error handling here ========================
171        }
172        lock.unlock();
173      }
174      switch(event->type){
175        // log handling ================
176      case ENET_EVENT_TYPE_CONNECT:
177        break;
178      case ENET_EVENT_TYPE_RECEIVE:
179        //COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
180        if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
181        //COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
182        event = new ENetEvent;
183        break;
184      case ENET_EVENT_TYPE_DISCONNECT:
185        quit=true;
186        // server closed the connection
187        return;
188        break;
189      case ENET_EVENT_TYPE_NONE:
190        //receiverThread_->yield();
191        msleep(1);
192        break;
193      }
194    }
195    // now disconnect
196
197    if(!disconnectConnection())
198      // if disconnecting failed destroy conn.
199      boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
200      enet_peer_reset(server);
201    return;
202  }
203
204  bool ClientConnection::disconnectConnection() {
205    ENetEvent event;
206    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
207    enet_peer_disconnect(server, 0);
208    while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME) > 0){
209      switch (event.type)
210      {
211      case ENET_EVENT_TYPE_NONE:
212      case ENET_EVENT_TYPE_CONNECT:
213      case ENET_EVENT_TYPE_RECEIVE:
214        enet_packet_destroy(event.packet);
215        break;
216      case ENET_EVENT_TYPE_DISCONNECT:
217        return true;
218      }
219    }
220    enet_peer_reset(server);
221    return false;
222  }
223
224  bool ClientConnection::establishConnection() {
225    ENetEvent event;
226    // connect to peer (server is type ENetPeer*)
227    boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
228    server = enet_host_connect(client, serverAddress, NETWORK_CLIENT_CHANNELS);
229    if(server==NULL) {
230      COUT(2) << "ClientConnection: server == NULL" << std::endl;
231      // error handling
232      return false;
233    }
234    // handshake
235    while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && !quit){
236      if( event.type == ENET_EVENT_TYPE_CONNECT ){
237        established=true;
238        return true;
239      }
240    }
241    COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl;
242    return false;
243  }
244
245  bool ClientConnection::processData(ENetEvent *event) {
246    COUT(5) << "Cl.Con: got packet, pushing to queue" << std::endl;
247    // just add packet to the buffer
248    // this can be extended with some preprocessing
249    return buffer.push(event);
250  }
251
252}
Note: See TracBrowser for help on using the repository browser.