Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/Connection.cc @ 3202

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

rest of the cleanup ( mostly client connection handling)
network is now single-threaded ( only in order to become multithreaded again, but thats another story ;) )

File size: 2.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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Connection.h"
30
31#include <iostream>
32#include <cassert>
33#include <OgreTimer.h>
34
35#include "util/Debug.h"
36#include "util/Math.h"
37#include "util/Sleep.h"
38#include "ClientInformation.h"
39#include "synchronisable/Synchronisable.h"
40#include "packet/ClassID.h"
41
42namespace orxonox
43{
44  Connection *Connection::instance_=0;
45
46  Connection::Connection():
47    host_(0)
48  {
49    assert(instance_==0);
50    Connection::instance_=this;
51    enet_initialize();
52    atexit(enet_deinitialize);
53  }
54
55  Connection::~Connection(){
56    Connection::instance_=0;
57  }
58
59  bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) {
60    if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
61      return false;
62    else
63      return true;
64  }
65
66  bool Connection::sendPackets() {
67    if ( !Connection::instance_ || this->host_==NULL )
68      return false;
69    enet_host_flush(this->host_);
70    return true;
71  }
72
73  void Connection::processQueue() {
74    ENetEvent event;
75   
76    assert(this->host_);
77    Ogre::Timer timer;
78
79    while( timer.getMilliseconds()<NETWORK_MAX_QUEUE_PROCESS_TIME && enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
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}
Note: See TracBrowser for help on using the repository browser.