Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/libraries/network/Connection.cc @ 7996

Last change on this file since 7996 was 7801, checked in by dafrick, 14 years ago

Merging presentation2 branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.6 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 <cassert>
32#include <deque>
33#define WIN32_LEAN_AND_MEAN
34#include <enet/enet.h>
35#include <boost/thread.hpp>
36#include <boost/thread/mutex.hpp>
37#include <boost/date_time.hpp>
38
39#include "packet/Packet.h"
40
41namespace orxonox
42{
43  const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20);
44
45  Connection::Connection():
46    host_(0), bCommunicationThreadRunning_(false)
47  {
48    enet_initialize();
49    atexit(enet_deinitialize);
50    this->incomingEventsMutex_ = new boost::mutex;
51    this->outgoingEventsMutex_ = new boost::mutex;
52  }
53
54  Connection::~Connection()
55  {
56    delete this->incomingEventsMutex_;
57    delete this->outgoingEventsMutex_;
58  }
59
60  void Connection::startCommunicationThread()
61  {
62    this->bCommunicationThreadRunning_ = true;
63    this->communicationThread_ = new boost::thread(&Connection::communicationThread, this);
64  }
65 
66  void Connection::stopCommunicationThread()
67  {
68    this->bCommunicationThreadRunning_ = false;
69    if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
70    {
71      // force thread to stop
72      this->communicationThread_->interrupt();
73    }
74    delete this->communicationThread_;
75  }
76
77
78//   int Connection::service(ENetEvent* event) {
79//     return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT );
80//   }
81
82  void Connection::disconnectPeer(ENetPeer *peer)
83  {
84    assert(peer);
85    outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 };
86   
87    this->outgoingEventsMutex_->lock();
88    this->outgoingEvents_.push_back(outEvent);
89    this->outgoingEventsMutex_->unlock();
90  }
91
92  void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID)
93  {
94    assert(peer);
95    outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID };
96   
97    this->outgoingEventsMutex_->lock();
98    this->outgoingEvents_.push_back(outEvent);
99    this->outgoingEventsMutex_->unlock();
100  }
101 
102  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
103  {
104    outgoingEvent outEvent = { (ENetPeer*)15, outgoingEventType::broadcastPacket, packet, channelID };
105   
106    this->outgoingEventsMutex_->lock();
107    this->outgoingEvents_.push_back(outEvent);
108    this->outgoingEventsMutex_->unlock();
109  }
110
111 
112  void Connection::communicationThread()
113  {
114    ENetEvent event;
115   
116    while( bCommunicationThreadRunning_ )
117    {
118      // Receive all pending incoming Events (such as packets, connects and disconnects)
119      while( enet_host_check_events( this->host_, &event ) > 0 )
120      {
121//         COUT(0) << "incoming event" << endl;
122        // received an event
123        this->incomingEventsMutex_->lock();
124        this->incomingEvents_.push_back(event);
125        this->incomingEventsMutex_->unlock();
126      }
127     
128      // Send all waiting outgoing packets
129      this->outgoingEventsMutex_->lock();
130      uint32_t outgoingEventsCount = this->outgoingEvents_.size();
131      this->outgoingEventsMutex_->unlock();
132      while( outgoingEventsCount > 0 )
133      {
134//         COUT(0) << "outgoing event" << endl;
135        this->outgoingEventsMutex_->lock();
136        outgoingEvent outEvent = this->outgoingEvents_.front();
137        this->outgoingEvents_.pop_front();
138        this->outgoingEventsMutex_->unlock();
139       
140        switch( outEvent.type )
141        {
142          case outgoingEventType::sendPacket:
143            enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet );
144            break;
145          case outgoingEventType::disconnectPeer:
146            enet_peer_disconnect(outEvent.peer, 0);
147            break;
148          case outgoingEventType::broadcastPacket:
149            enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet );
150            break;
151          default:
152            assert(0);
153        }
154        this->outgoingEventsMutex_->lock();
155        outgoingEventsCount = this->outgoingEvents_.size();
156        this->outgoingEventsMutex_->unlock();
157      }
158     
159      // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms)
160      if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
161      {
162//         COUT(0) << "incoming event after wait" << endl;
163        //received an event
164        this->incomingEventsMutex_->lock();
165        this->incomingEvents_.push_back(event);
166        this->incomingEventsMutex_->unlock();
167      }
168    }
169  }
170
171  void Connection::processQueue()
172  {
173    ENetEvent event;
174
175    this->incomingEventsMutex_->lock();
176    uint32_t incomingEventsCount = this->incomingEvents_.size();
177    this->incomingEventsMutex_->unlock();
178    while( incomingEventsCount > 0 )
179    {
180      packet::Packet* p;
181      this->incomingEventsMutex_->lock();
182      event = this->incomingEvents_.front();
183      this->incomingEvents_.pop_front();
184      this->incomingEventsMutex_->unlock();
185     
186      switch(event.type)
187      {
188        // log handling ================
189        case ENET_EVENT_TYPE_CONNECT:
190          addPeer( &event );
191          break;
192        case ENET_EVENT_TYPE_DISCONNECT:
193          removePeer( &event );
194          break;
195        case ENET_EVENT_TYPE_RECEIVE:
196//           COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl;
197          p = createPacket( &event );
198          processPacket(p);
199          break;
200        case ENET_EVENT_TYPE_NONE:
201          break;
202      }
203     
204      this->incomingEventsMutex_->lock();
205      incomingEventsCount = this->incomingEvents_.size();
206      this->incomingEventsMutex_->unlock();
207    }
208  }
209
210  packet::Packet* Connection::createPacket(ENetEvent* event)
211  {
212    packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer);
213    return p;
214//     return p->process();
215  }
216 
217  void Connection::enableCompression()
218  {
219    enet_host_compress_with_range_coder( this->host_ );
220  }
221
222
223}
Note: See TracBrowser for help on using the repository browser.