Orxonox  0.0.5 Codename: Arcturus
Connection.h
Go to the documentation of this file.
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 //
30 // C++ Interface: Connection
31 //
32 // Description:
33 //
34 //
35 // Author: Oliver Scheuss, (C) 2007
36 //
37 // Copyright: See COPYING file that comes with this distribution
38 //
39 //
40 #ifndef _Connection_H__
41 #define _Connection_H__
42 
43 #include "NetworkPrereqs.h"
44 
45 #include <deque>
46 #include <map>
47 #include <enet/enet.h>
48 
49 namespace boost
50 {
51  class thread;
52  class mutex;
53 }
54 
55 namespace orxonox
56 {
57  const unsigned int NETWORK_PORT = 55556;
58  const unsigned int NETWORK_MAX_CONNECTIONS = 50;
59  const unsigned int NETWORK_WAIT_TIMEOUT = 1;
60  const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5;
61 
62  enum class IncomingEventType
63  {
64  receivePacket = 1, // incoming packet
65  peerConnect = 2, // incoming connect request
66  peerDisconnect = 3 // incoming disconnect request
67  };
68 
69  enum class OutgoingEventType
70  {
71  sendPacket = 1, // outgoing packet
72  broadcastPacket = 2, // outgoing broadcast packet
73  disconnectPeer = 3, // outgoing disconnect request
74  disconnectPeers = 4 // outgoing disconnect request
75  };
76 
78  {
79  uint32_t peerID;
82  };
83 
85  {
86  uint32_t peerID;
90  };
91 
93  {
94  public:
95  virtual ~Connection();
96 
97  protected:
98  Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER + 1);
99 
100  void startCommunicationThread();
101  void stopCommunicationThread();
102 
103  void addPacket(ENetPacket *packet, uint32_t peerID, uint8_t channelID);
104  void broadcastPacket(ENetPacket* packet, uint8_t channelID);
105  void disconnectPeer(uint32_t peerID);
106  void disconnectPeers();
107 
108  void enableCompression();
109 
110  void processQueue();
111  void waitOutgoingQueue(); // wait for the outgoing queue to become empty (everything processed by communication thread)
112  virtual void addPeer(uint32_t peerID) = 0;
113  virtual void removePeer(uint32_t peerID) = 0;
114  virtual void processPacket( packet::Packet* packet) = 0;
115 
116  incomingEvent preprocessConnectEvent(ENetEvent& event);
117  incomingEvent preprocessDisconnectEvent(ENetEvent& event);
118  incomingEvent preprocessReceiveEvent(ENetEvent& event);
119 
120  void processIncomingEvent(ENetEvent& event);
121  void processOutgoingEvent(outgoingEvent& event);
122 
123  void disconnectPeersInternal();
124 
126 
127  private:
128  void communicationThread();
129 
130  boost::thread* communicationThread_;
133 
134  // Queue for incoming events
135  std::deque<incomingEvent> incomingEvents_;
136 
137  // Queue for outgoing events
138  std::deque<outgoingEvent> outgoingEvents_;
139 
140  boost::mutex* incomingEventsMutex_;
141  boost::mutex* outgoingEventsMutex_;
142  boost::mutex* overallMutex_;
143  std::map<uint32_t, ENetPeer*> peerMap_;
144  std::map<ENetPeer*, uint32_t> peerIDMap_;
145  uint32_t nextPeerID_;
146 
147  };
148 
149 }
150 
151 #endif /* _Connection_H__ */
Portable internet address structure.
Definition: enet.h:82
std::deque< outgoingEvent > outgoingEvents_
Definition: Connection.h:138
Shared library macros, enums, constants and forward declarations for the network library ...
const unsigned int NETWORK_WAIT_TIMEOUT
Definition: Connection.h:59
boost::mutex * outgoingEventsMutex_
Definition: Connection.h:141
OutgoingEventType
Definition: Connection.h:69
Definition: CorePrereqs.h:309
packet::Packet * packet
Definition: Connection.h:81
uint8_t ENetChannelID
Definition: NetworkPrereqs.h:119
ENetHost * host_
Definition: Connection.h:125
static constexpr unsigned int NETWORK_PEER_ID_SERVER
Definition: NetworkPrereqs.h:73
Structure to describe a single event.
Definition: ScriptController.h:42
OutgoingEventType type
Definition: Connection.h:87
boost::mutex * incomingEventsMutex_
Definition: Connection.h:140
An ENet host for communicating with peers.
Definition: enet.h:345
Definition: Connection.h:77
boost::thread * communicationThread_
Definition: Connection.h:130
An ENet event as returned by enet_host_service().
Definition: enet.h:416
IncomingEventType
Definition: Connection.h:62
boost::mutex * overallMutex_
Definition: Connection.h:142
IncomingEventType type
Definition: Connection.h:80
std::map< ENetPeer *, uint32_t > peerIDMap_
Definition: Connection.h:144
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
#define _NetworkExport
Definition: NetworkPrereqs.h:59
std::map< uint32_t, ENetPeer * > peerMap_
Definition: Connection.h:143
Definition: Packet.h:61
std::deque< incomingEvent > incomingEvents_
Definition: Connection.h:135
ENetChannelID channelID
Definition: Connection.h:89
uint32_t peerID
Definition: Connection.h:86
ENetAddress * bindAddress_
Definition: Connection.h:132
Definition: Connection.h:84
const unsigned int NETWORK_PORT
Definition: Connection.h:57
ENet public header file.
uint32_t nextPeerID_
Definition: Connection.h:145
uint32_t peerID
Definition: Connection.h:79
ENetPacket * packet
Definition: Connection.h:88
const unsigned int NETWORK_MAX_CONNECTIONS
Definition: Connection.h:58
Definition: Connection.h:92
const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME
Definition: Connection.h:60
bool bCommunicationThreadRunning_
Definition: Connection.h:131
ENet packet structure.
Definition: enet.h:147