| 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 | #define WIN32_LEAN_AND_MEAN | 
|---|
| 33 | #include <enet/enet.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 |  | 
|---|
| 80 | while( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) | 
|---|
| 81 | { | 
|---|
| 82 | switch(event.type){ | 
|---|
| 83 | // log handling ================ | 
|---|
| 84 | case ENET_EVENT_TYPE_CONNECT: | 
|---|
| 85 | addPeer( &event ); | 
|---|
| 86 | break; | 
|---|
| 87 | case ENET_EVENT_TYPE_DISCONNECT: | 
|---|
| 88 | removePeer( &event ); | 
|---|
| 89 | break; | 
|---|
| 90 | case ENET_EVENT_TYPE_RECEIVE: | 
|---|
| 91 | processPacket( &event ); | 
|---|
| 92 | break; | 
|---|
| 93 | case ENET_EVENT_TYPE_NONE: | 
|---|
| 94 | break; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | bool Connection::processPacket(ENetEvent* event) { | 
|---|
| 100 | packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer); | 
|---|
| 101 | return p->process(); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | } | 
|---|