Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud2/src/network/ClientConnection.cc @ 1075

Last change on this file since 1075 was 963, checked in by rgrieder, 17 years ago

reverted

File size: 5.9 KB
Line 
1/*
2*   ORXONOX - the hottest 3D action shooter ever to exist
3*
4*
5*   License notice:
6*
7*   This program is free software; you can redistribute it and/or
8*   modify it under the terms of the GNU General Public License
9*   as published by the Free Software Foundation; either version 2
10*   of the License, or (at your option) any later version.
11*
12*   This program is distributed in the hope that it will be useful,
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*   GNU General Public License for more details.
16*
17*   You should have received a copy of the GNU General Public License
18*   along with this program; if not, write to the Free Software
19*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*
21*   Author:
22*      Oliver Scheuss, (C) 2007
23*   Co-authors:
24*      ...
25*
26*/
27
28//
29// C++ Interface: ClientConnection
30//
31// Description: The Class ClientConnection manages the servers conenctions to the clients.
32// each connection is provided by a new process. communication between master process and
33// connection processes is provided by ...
34//
35//
36// Author:  Oliver Scheuss
37//
38
39#include <iostream>
40// boost.thread library for multithreading support
41#include <boost/thread/thread.hpp>
42#include <boost/bind.hpp>
43
44#include "util/Sleep.h"
45#include "orxonox/core/Debug.h"
46#include "ClientConnection.h"
47
48namespace network
49{
50  static boost::thread_group network_threads;
51
52  ClientConnection::ClientConnection(int port, std::string address) {
53    quit=false;
54    server=NULL;
55    enet_address_set_host(&serverAddress, address.c_str());
56    serverAddress.port = NETWORK_PORT;
57    established=false;
58  }
59
60  ClientConnection::ClientConnection(int port, const char *address) {
61    quit=false;
62    server=NULL;
63    enet_address_set_host(&serverAddress, address);
64    serverAddress.port = NETWORK_PORT;
65    established=false;
66  }
67
68  bool ClientConnection::waitEstablished(int milisec) {
69    for(int i=0; i<=milisec && !established; i++)
70      usleep(1000);
71
72    return established;
73  }
74
75
76  ENetPacket *ClientConnection::getPacket(ENetAddress &address) {
77    if(!buffer.isEmpty()) {
78      //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl;
79      return buffer.pop(address);
80    }
81    else{
82      return NULL;
83    }
84  }
85
86  ENetPacket *ClientConnection::getPacket() {
87    ENetAddress address;
88    return getPacket(address);
89  }
90
91  bool ClientConnection::queueEmpty() {
92    return buffer.isEmpty();
93  }
94
95  bool ClientConnection::createConnection() {
96    network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this));
97    // wait 10 seconds for the connection to be established
98    return waitEstablished(10000);
99  }
100
101  bool ClientConnection::closeConnection() {
102    quit=true;
103    network_threads.join_all();
104    established=false;
105    return true;
106  }
107
108
109  bool ClientConnection::addPacket(ENetPacket *packet) {
110    if(server==NULL)
111      return false;
112    if(enet_peer_send(server, 1, packet)!=0)
113      return false;
114    return true;
115  }
116
117  bool ClientConnection::sendPackets(ENetEvent *event) {
118    if(server==NULL)
119      return false;
120    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
121      return true;}
122    else
123      return false;
124  }
125
126  bool ClientConnection::sendPackets() {
127    ENetEvent event;
128    if(server==NULL)
129      return false;
130    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
131      return true;}
132    else
133      return false;
134  }
135
136  void ClientConnection::receiverThread() {
137    // what about some error-handling here ?
138    enet_initialize();
139    atexit(enet_deinitialize);
140    ENetEvent event;
141    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
142    if(client==NULL)
143      // add some error handling here ==========================
144      quit=true;
145    //connect to the server
146    if(!establishConnection()){
147      quit=true;
148      return;
149    }
150    //main loop
151    while(!quit){
152      //std::cout << "connection loop" << std::endl;
153      if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
154        // we should never reach this point
155        quit=true;
156        // add some error handling here ========================
157      }
158      switch(event.type){
159        // log handling ================
160      case ENET_EVENT_TYPE_CONNECT:
161      case ENET_EVENT_TYPE_RECEIVE:
162        COUT(5) << "receiver-Thread: got new packet" << std::endl;
163        processData(&event);
164        break;
165      case ENET_EVENT_TYPE_DISCONNECT:
166        quit=true;
167        // server closed the connection
168        return;
169        break;
170      case ENET_EVENT_TYPE_NONE:
171        continue;
172      }
173    }
174    // now disconnect
175
176    if(!disconnectConnection())
177      // if disconnecting failed destroy conn.
178      enet_peer_reset(server);
179    return;
180  }
181
182  bool ClientConnection::disconnectConnection() {
183    ENetEvent event;
184    enet_peer_disconnect(server, 0);
185    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
186      switch (event.type)
187      {
188      case ENET_EVENT_TYPE_NONE:
189      case ENET_EVENT_TYPE_CONNECT:
190      case ENET_EVENT_TYPE_RECEIVE:
191        enet_packet_destroy(event.packet);
192        break;
193      case ENET_EVENT_TYPE_DISCONNECT:
194        return true;
195      }
196    }
197    enet_peer_reset(server);
198    return false;
199  }
200
201  bool ClientConnection::establishConnection() {
202    ENetEvent event;
203    // connect to peer
204    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
205    if(server==NULL)
206      // error handling
207      return false;
208    // handshake
209    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
210      established=true;
211      return true;
212    }
213    else
214      return false;
215  }
216
217  bool ClientConnection::processData(ENetEvent *event) {
218    COUT(5) << "got packet, pushing to queue" << std::endl;
219    // just add packet to the buffer
220    // this can be extended with some preprocessing
221    return buffer.push(event);
222  }
223
224}
Note: See TracBrowser for help on using the repository browser.