Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

enet is not threadsafe (catched that now); some first step towards dedicated server

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