Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/network/ClientConnection.cc @ 884

Last change on this file since 884 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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