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