Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialFS09/src/network/ClientConnection.cc @ 8924

Last change on this file since 8924 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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