Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/network/Connection.cc @ 4220

Last change on this file since 4220 was 3302, checked in by scheusso, 15 years ago

changed some debug output
removed some uneccessary "multi"threading
some enhancements in Connection

File size: 2.6 KB
RevLine 
[3214]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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Connection.h"
30
31#include <cassert>
32#include <enet/enet.h>
33#include "packet/Packet.h"
34
35namespace orxonox
36{
37  Connection *Connection::instance_=0;
38
39  Connection::Connection():
40    host_(0)
41  {
42    assert(instance_==0);
43    Connection::instance_=this;
44    enet_initialize();
45    atexit(enet_deinitialize);
46  }
47
48  Connection::~Connection(){
49    Connection::instance_=0;
50  }
51
52  int Connection::service(ENetEvent* event) {
53    return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT );
54  }
55
56  void Connection::disconnectPeer(ENetPeer *peer) {
57    enet_peer_disconnect(peer, 0);
58  }
59
60  bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) {
61    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
62      return false;
63    else
64      return true;
65  }
66
67  bool Connection::sendPackets() {
68    if ( !Connection::instance_ || this->host_==NULL )
69      return false;
70    enet_host_flush(this->host_);
71    return true;
72  }
73
74  void Connection::processQueue() {
75    ENetEvent event;
76   
77    assert(this->host_);
78
[3302]79    while( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
[3214]80    {
81      switch(event.type){
82        // log handling ================
83        case ENET_EVENT_TYPE_CONNECT:
84          addClient( &event );
85          break;
86        case ENET_EVENT_TYPE_DISCONNECT:
87          disconnectPeer( &event );
88          break;
89        case ENET_EVENT_TYPE_RECEIVE:
90          processPacket( &event );
91          break;
92        case ENET_EVENT_TYPE_NONE:
93          break;
94      }
95    }
96  }
97
98  bool Connection::processPacket(ENetEvent* event) {
99    packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer);
100    return p->process();
101  }
102
103}
Note: See TracBrowser for help on using the repository browser.