Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/Connection.cc @ 9452

Last change on this file since 9452 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 10.9 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>
[7801]32#include <deque>
[5929]33#define WIN32_LEAN_AND_MEAN
[3214]34#include <enet/enet.h>
[7801]35#include <boost/thread.hpp>
36#include <boost/thread/mutex.hpp>
37#include <boost/date_time.hpp>
38
[3214]39#include "packet/Packet.h"
[8327]40#include <util/Sleep.h>
[3214]41
42namespace orxonox
43{
[8327]44  const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(200);
45  const unsigned int                NETWORK_DISCONNECT_TIMEOUT = 500;
[3214]46
[8327]47  Connection::Connection(uint32_t firstPeerID):
48    host_(0), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID)
[3214]49  {
50    enet_initialize();
51    atexit(enet_deinitialize);
[7801]52    this->incomingEventsMutex_ = new boost::mutex;
53    this->outgoingEventsMutex_ = new boost::mutex;
[8327]54//     this->overallMutex_ = new boost::mutex;
[3214]55  }
56
[7801]57  Connection::~Connection()
58  {
59    delete this->incomingEventsMutex_;
60    delete this->outgoingEventsMutex_;
[3214]61  }
62
[7801]63  void Connection::startCommunicationThread()
64  {
65    this->bCommunicationThreadRunning_ = true;
66    this->communicationThread_ = new boost::thread(&Connection::communicationThread, this);
[3214]67  }
[7801]68 
69  void Connection::stopCommunicationThread()
70  {
71    this->bCommunicationThreadRunning_ = false;
72    if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
73    {
74      // force thread to stop
75      this->communicationThread_->interrupt();
76    }
77    delete this->communicationThread_;
78  }
[3214]79
[8327]80  void Connection::disconnectPeer(uint32_t peerID)
[7801]81  {
[8327]82//     this->overallMutex_->lock();
83    outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, 0, 0 };
[7801]84   
85    this->outgoingEventsMutex_->lock();
86    this->outgoingEvents_.push_back(outEvent);
87    this->outgoingEventsMutex_->unlock();
[8327]88//     this->overallMutex_->unlock();
[3214]89  }
[8327]90 
91  void Connection::disconnectPeers()
92  {
93    outgoingEvent outEvent = { 0, outgoingEventType::disconnectPeers, 0, 0 };
94   
95    this->outgoingEventsMutex_->lock();
96    this->outgoingEvents_.push_back(outEvent);
97    this->outgoingEventsMutex_->unlock();
98  }
[3214]99
[8327]100  void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID)
[7801]101  {
[8327]102//     this->overallMutex_->lock();
103    outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID };
[7801]104   
105    this->outgoingEventsMutex_->lock();
106    this->outgoingEvents_.push_back(outEvent);
107    this->outgoingEventsMutex_->unlock();
[8327]108//     this->overallMutex_->unlock();
[3214]109  }
[7801]110 
111  void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
112  {
[8327]113//     this->overallMutex_->lock();
114    outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
[7801]115   
116    this->outgoingEventsMutex_->lock();
117    this->outgoingEvents_.push_back(outEvent);
118    this->outgoingEventsMutex_->unlock();
[8327]119//     this->overallMutex_->unlock();
[7801]120  }
[3214]121
[7801]122 
123  void Connection::communicationThread()
124  {
125    ENetEvent event;
126   
[8327]127//     this->overallMutex_->lock();
[7801]128    while( bCommunicationThreadRunning_ )
129    {
130      // Receive all pending incoming Events (such as packets, connects and disconnects)
131      while( enet_host_check_events( this->host_, &event ) > 0 )
132      {
[8327]133        processIncomingEvent(event);
[7801]134      }
135     
[8327]136//       this->overallMutex_->unlock();
137      msleep(1);
138//       this->overallMutex_->lock();
139     
[7801]140      // Send all waiting outgoing packets
141      this->outgoingEventsMutex_->lock();
142      uint32_t outgoingEventsCount = this->outgoingEvents_.size();
143      this->outgoingEventsMutex_->unlock();
144      while( outgoingEventsCount > 0 )
145      {
[8858]146//         orxout(verbose, context::network) << "outgoing event" << endl;
[7801]147        this->outgoingEventsMutex_->lock();
148        outgoingEvent outEvent = this->outgoingEvents_.front();
149        this->outgoingEvents_.pop_front();
150        this->outgoingEventsMutex_->unlock();
151       
[8327]152        processOutgoingEvent(outEvent);
153       
[7801]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      {
[8327]162        processIncomingEvent(event);
[7801]163      }
164    }
[8327]165//     this->overallMutex_->unlock();
[3214]166  }
[8327]167 
168  void Connection::processIncomingEvent(ENetEvent& event)
169  {
170    incomingEvent inEvent;
171    // preprocess event
172    switch( event.type )
173    {
174      case ENET_EVENT_TYPE_CONNECT:
175        inEvent = preprocessConnectEvent(event);
176        break;
177      case ENET_EVENT_TYPE_RECEIVE:
178        inEvent = preprocessReceiveEvent(event);
179        break;
180      case ENET_EVENT_TYPE_DISCONNECT:
181        inEvent = preprocessDisconnectEvent(event);
182        break;
183      case ENET_EVENT_TYPE_NONE:
184      default:
185        return;
186    }
187   
188    // pushing event to queue
189    this->incomingEventsMutex_->lock();
190    this->incomingEvents_.push_back(inEvent);
191    this->incomingEventsMutex_->unlock();
192  }
193 
194  void Connection::processOutgoingEvent(outgoingEvent& event)
195  {
196    ENetPeer* peer;
197    switch( event.type )
198    {
199      case outgoingEventType::sendPacket:
200        // check whether the peer is still/already in the peer list
201        if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
202        {
203          peer = this->peerMap_[event.peerID];
204          enet_peer_send( peer, event.channelID, event.packet );
205        }
206        else
207        {
208          // peer probably already disconnected so just discard packet
209          assert(event.peerID<this->nextPeerID_);
210          enet_packet_destroy(event.packet);
211        }
212        break;
213      case outgoingEventType::disconnectPeer:
214        if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
215        {
216          peer = this->peerMap_[event.peerID];
217          enet_peer_disconnect(peer, 0);
218        }
219        else
220        {
221          // peer probably already disconnected so just discard disconnect event
222          assert(event.peerID<this->nextPeerID_);
223        }
224        break;
225      case outgoingEventType::disconnectPeers:
226        disconnectPeersInternal();
227        break;
228      case outgoingEventType::broadcastPacket:
229        enet_host_broadcast( this->host_, event.channelID, event.packet );
230        break;
231      default:
232        assert(0);
233    }
234  }
[3214]235
[8327]236
237  void Connection::disconnectPeersInternal()
238  {
239    std::map<uint32_t, ENetPeer*>::iterator it;
240    for( it=this->peerMap_.begin(); it!=this->peerMap_.end(); ++it )
241    {
242      enet_peer_disconnect(it->second, 0);
243    }
244    uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT/NETWORK_WAIT_TIMEOUT;
245    uint32_t i = 0;
246    while( this->peerMap_.size() && i++ < iterations )
247    {
248      ENetEvent event;
249      if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
250      {
251        processIncomingEvent(event);
252      }
253    }
254  }
255
[7801]256  void Connection::processQueue()
257  {
[8327]258    incomingEvent inEvent;
[6417]259
[7801]260    this->incomingEventsMutex_->lock();
261    uint32_t incomingEventsCount = this->incomingEvents_.size();
262    this->incomingEventsMutex_->unlock();
263    while( incomingEventsCount > 0 )
[3214]264    {
[8327]265      // pop event from queue
[7801]266      this->incomingEventsMutex_->lock();
[8327]267      inEvent = this->incomingEvents_.front();
[7801]268      this->incomingEvents_.pop_front();
269      this->incomingEventsMutex_->unlock();
270     
[8327]271      // process event
272      switch( inEvent.type )
[7801]273      {
[8327]274        case incomingEventType::peerConnect:
275          addPeer(inEvent.peerID);
[3214]276          break;
[8327]277        case incomingEventType::peerDisconnect:
278          removePeer(inEvent.peerID);
[3214]279          break;
[8327]280        case incomingEventType::receivePacket:
281          processPacket(inEvent.packet);
[3214]282          break;
[8327]283        default:
[3214]284          break;
285      }
[7801]286     
[8327]287      // check whether there are still events in the queue
[7801]288      this->incomingEventsMutex_->lock();
289      incomingEventsCount = this->incomingEvents_.size();
290      this->incomingEventsMutex_->unlock();
[3214]291    }
292  }
[8327]293 
294  void Connection::waitOutgoingQueue()
295  {
296    uint32_t outgoingEventsCount;
297    this->outgoingEventsMutex_->lock();
298    outgoingEventsCount = this->outgoingEvents_.size();
299    this->outgoingEventsMutex_->unlock();
300    while( outgoingEventsCount )
301    {
302      msleep(1);
303      this->outgoingEventsMutex_->lock();
304      outgoingEventsCount = this->outgoingEvents_.size();
305      this->outgoingEventsMutex_->unlock();
306    }
307  }
[3214]308
[8327]309
310  incomingEvent Connection::preprocessConnectEvent(ENetEvent& event)
[7801]311  {
[8327]312    // make sure this peer doesn't exist
313    assert( this->peerMap_.find(this->nextPeerID_) == this->peerMap_.end() );
314    assert( this->peerIDMap_.find(event.peer) == this->peerIDMap_.end() );
315   
316    // give peer a new id and increase peerID for next peer
317    uint32_t peerID = this->nextPeerID_;
318    ++this->nextPeerID_;
319   
320    // add peer/peerID into peerMap_ and peerIDMap_
321    this->peerMap_[peerID] = event.peer;
322    this->peerIDMap_[event.peer] = peerID;
323   
324    // create new peerEvent and return it
325    incomingEvent inEvent = { peerID, incomingEventType::peerConnect, 0 };
326    return inEvent;
[3214]327  }
[7801]328 
[8327]329  incomingEvent Connection::preprocessDisconnectEvent(ENetEvent& event)
330  {
331    // assert that the peer exists and get peerID
332    assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() );
333    uint32_t peerID = this->peerIDMap_[event.peer];
334   
335    // remove peer/peerID from maps
336    this->peerIDMap_.erase(this->peerIDMap_.find(event.peer));
337    this->peerMap_.erase(this->peerMap_.find(peerID));
338   
339    // create new peerEvent and return it
340    incomingEvent inEvent = { peerID, incomingEventType::peerDisconnect, 0 };
341    return inEvent;
342  }
343 
344  incomingEvent Connection::preprocessReceiveEvent(ENetEvent& event)
345  {
346    // assert that the peer exists and get peerID
347    assert( this->peerIDMap_.find(event.peer) != this->peerIDMap_.end() );
348    uint32_t peerID = this->peerIDMap_[event.peer];
349   
350    // create new Packet from ENetPacket
351    packet::Packet* p = packet::Packet::createPacket(event.packet, peerID);
352   
353    // create new peerEvent and return it
354    incomingEvent inEvent = { peerID, incomingEventType::receivePacket, p };
355    return inEvent;
356  }
357
358 
[7801]359  void Connection::enableCompression()
360  {
361    enet_host_compress_with_range_coder( this->host_ );
362  }
[3214]363
[7801]364
[3214]365}
Note: See TracBrowser for help on using the repository browser.