Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/ClientConnection.cc @ 1253

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

fixes over fixes

File size: 6.6 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; //sems that address is not needed
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(!packet){
115      COUT(4) << "Cl.con: addpacket: invalid packet" << std::endl;
116      return false;
117    }
118    if(enet_peer_send(server, 0, packet)<0)
119      return false;
120    else
121      return true;
122  }
123
124  bool ClientConnection::sendPackets(ENetEvent *event) {
125    if(server==NULL)
126      return false;
127    if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){
128      return true;}
129    else
130      return false;
131  }
132
133  bool ClientConnection::sendPackets() {
134    ENetEvent event;
135    if(server==NULL)
136      return false;
137    if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){
138      return true;}
139    else
140      return false;
141  }
142
143  void ClientConnection::receiverThread() {
144    // what about some error-handling here ?
145    enet_initialize();
146    atexit(enet_deinitialize);
147    ENetEvent event;
148    client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0);
149    if(client==NULL)
150      // add some error handling here ==========================
151      quit=true;
152    //connect to the server
153    if(!establishConnection()){
154      quit=true;
155      return;
156    }
157    //main loop
158    while(!quit){
159      //std::cout << "connection loop" << std::endl;
160      if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){
161        // we should never reach this point
162        quit=true;
163        // add some error handling here ========================
164      }
165      switch(event.type){
166        // log handling ================
167      case ENET_EVENT_TYPE_CONNECT:
168      case ENET_EVENT_TYPE_RECEIVE:
169        COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
170        if ( !processData(&event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
171        COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
172        break;
173      case ENET_EVENT_TYPE_DISCONNECT:
174        quit=true;
175        // server closed the connection
176        return;
177        break;
178      case ENET_EVENT_TYPE_NONE:
179        continue;
180      }
181    }
182    // now disconnect
183
184    if(!disconnectConnection())
185      // if disconnecting failed destroy conn.
186      enet_peer_reset(server);
187    return;
188  }
189
190  bool ClientConnection::disconnectConnection() {
191    ENetEvent event;
192    enet_peer_disconnect(server, 0);
193    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
194      switch (event.type)
195      {
196      case ENET_EVENT_TYPE_NONE:
197      case ENET_EVENT_TYPE_CONNECT:
198      case ENET_EVENT_TYPE_RECEIVE:
199        enet_packet_destroy(event.packet);
200        break;
201      case ENET_EVENT_TYPE_DISCONNECT:
202        return true;
203      }
204    }
205    enet_peer_reset(server);
206    return false;
207  }
208
209  bool ClientConnection::establishConnection() {
210    ENetEvent event;
211    // connect to peer (server is type ENetPeer*)
212    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
213    if(server==NULL)
214      // error handling
215      return false;
216    // handshake
217    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
218      established=true;
219      return true;
220    }
221    else
222      return false;
223  }
224
225  bool ClientConnection::processData(ENetEvent *event) {
226    COUT(5) << "Cl.Con: got packet, pushing to queue" << std::endl;
227    // just add packet to the buffer
228    // this can be extended with some preprocessing
229    return buffer.push(event);
230  }
231
232}
Note: See TracBrowser for help on using the repository browser.