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 <OgreTimer.h> |
---|
34 | #include "packet/Packet.h" |
---|
35 | |
---|
36 | namespace orxonox |
---|
37 | { |
---|
38 | Connection *Connection::instance_=0; |
---|
39 | |
---|
40 | Connection::Connection(): |
---|
41 | host_(0) |
---|
42 | { |
---|
43 | assert(instance_==0); |
---|
44 | Connection::instance_=this; |
---|
45 | enet_initialize(); |
---|
46 | atexit(enet_deinitialize); |
---|
47 | } |
---|
48 | |
---|
49 | Connection::~Connection(){ |
---|
50 | Connection::instance_=0; |
---|
51 | } |
---|
52 | |
---|
53 | int Connection::service(ENetEvent* event) { |
---|
54 | return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); |
---|
55 | } |
---|
56 | |
---|
57 | void Connection::disconnectPeer(ENetPeer *peer) { |
---|
58 | enet_peer_disconnect(peer, 0); |
---|
59 | } |
---|
60 | |
---|
61 | bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) { |
---|
62 | if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0) |
---|
63 | return false; |
---|
64 | else |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | bool Connection::sendPackets() { |
---|
69 | if ( !Connection::instance_ || this->host_==NULL ) |
---|
70 | return false; |
---|
71 | enet_host_flush(this->host_); |
---|
72 | return true; |
---|
73 | } |
---|
74 | |
---|
75 | void Connection::processQueue() { |
---|
76 | ENetEvent event; |
---|
77 | |
---|
78 | assert(this->host_); |
---|
79 | Ogre::Timer timer; |
---|
80 | |
---|
81 | while( timer.getMilliseconds()<NETWORK_MAX_QUEUE_PROCESS_TIME && enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) |
---|
82 | { |
---|
83 | switch(event.type){ |
---|
84 | // log handling ================ |
---|
85 | case ENET_EVENT_TYPE_CONNECT: |
---|
86 | addClient( &event ); |
---|
87 | break; |
---|
88 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
89 | disconnectPeer( &event ); |
---|
90 | break; |
---|
91 | case ENET_EVENT_TYPE_RECEIVE: |
---|
92 | processPacket( &event ); |
---|
93 | break; |
---|
94 | case ENET_EVENT_TYPE_NONE: |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | bool Connection::processPacket(ENetEvent* event) { |
---|
101 | packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer); |
---|
102 | return p->process(); |
---|
103 | } |
---|
104 | |
---|
105 | } |
---|