Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network2/src/network/ClientConnection.cc @ 1150

Last change on this file since 1150 was 1150, checked in by dumenim, 17 years ago

either i'm wrong or the hud is. it seems to create a segfault in startRenderLoop, don't know if only in network branch. there are some std::couts commented with 3 / (blue :-) and the hud calls are commented out too. now it goes a bit further untill the next segfault.

File size: 6.5 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(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) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl;
165        if ( !processData(&event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;
166        COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl;
167        break;
168      case ENET_EVENT_TYPE_DISCONNECT:
169        quit=true;
170        // server closed the connection
171        return;
172        break;
173      case ENET_EVENT_TYPE_NONE:
174        continue;
175      }
176    }
177    // now disconnect
178
179    if(!disconnectConnection())
180      // if disconnecting failed destroy conn.
181      enet_peer_reset(server);
182    return;
183  }
184
185  bool ClientConnection::disconnectConnection() {
186    ENetEvent event;
187    enet_peer_disconnect(server, 0);
188    while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){
189      switch (event.type)
190      {
191      case ENET_EVENT_TYPE_NONE:
192      case ENET_EVENT_TYPE_CONNECT:
193      case ENET_EVENT_TYPE_RECEIVE:
194        enet_packet_destroy(event.packet);
195        break;
196      case ENET_EVENT_TYPE_DISCONNECT:
197        return true;
198      }
199    }
200    enet_peer_reset(server);
201    return false;
202  }
203
204  bool ClientConnection::establishConnection() {
205    ENetEvent event;
206    // connect to peer (server is type ENetPeer*)
207    server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS);
208    if(server==NULL)
209      // error handling
210      return false;
211    // handshake
212    if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){
213      established=true;
214      return true;
215    }
216    else
217      return false;
218  }
219
220  bool ClientConnection::processData(ENetEvent *event) {
221    COUT(5) << "Cl.Con: got packet, pushing to queue" << std::endl;
222    // just add packet to the buffer
223    // this can be extended with some preprocessing
224    return buffer.push(event);
225  }
226
227}
Note: See TracBrowser for help on using the repository browser.