| 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 | #include <boost/concept_check.hpp> | 
|---|
| 49 |  | 
|---|
| 50 | namespace boost | 
|---|
| 51 | { | 
|---|
| 52 | class thread; | 
|---|
| 53 | class mutex; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | namespace orxonox | 
|---|
| 57 | { | 
|---|
| 58 | const unsigned int NETWORK_PORT                   = 55556; | 
|---|
| 59 | const unsigned int NETWORK_MAX_CONNECTIONS        = 50; | 
|---|
| 60 | const unsigned int NETWORK_WAIT_TIMEOUT           = 1; | 
|---|
| 61 | const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5; | 
|---|
| 62 |  | 
|---|
| 63 | namespace incomingEventType | 
|---|
| 64 | { | 
|---|
| 65 | enum Value | 
|---|
| 66 | { | 
|---|
| 67 | receivePacket   = 1,  // incoming packet | 
|---|
| 68 | peerConnect     = 2,  // incoming connect request | 
|---|
| 69 | peerDisconnect  = 3   // incoming disconnect request | 
|---|
| 70 | }; | 
|---|
| 71 |  | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | namespace outgoingEventType | 
|---|
| 75 | { | 
|---|
| 76 | enum Value | 
|---|
| 77 | { | 
|---|
| 78 | sendPacket      = 1,  // outgoing packet | 
|---|
| 79 | broadcastPacket = 2,  // outgoing broadcast packet | 
|---|
| 80 | disconnectPeer  = 3,  // outgoing disconnect request | 
|---|
| 81 | disconnectPeers = 4   // outgoing disconnect request | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | struct _NetworkExport incomingEvent | 
|---|
| 87 | { | 
|---|
| 88 | uint32_t                  peerID; | 
|---|
| 89 | incomingEventType::Value  type; | 
|---|
| 90 | packet::Packet*           packet; | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | struct _NetworkExport outgoingEvent | 
|---|
| 94 | { | 
|---|
| 95 | uint32_t                  peerID; | 
|---|
| 96 | outgoingEventType::Value  type; | 
|---|
| 97 | ENetPacket*               packet; | 
|---|
| 98 | ENetChannelID             channelID; | 
|---|
| 99 | }; | 
|---|
| 100 |  | 
|---|
| 101 | class _NetworkExport Connection | 
|---|
| 102 | { | 
|---|
| 103 | public: | 
|---|
| 104 | virtual ~Connection(); | 
|---|
| 105 |  | 
|---|
| 106 | protected: | 
|---|
| 107 | Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER+1); | 
|---|
| 108 |  | 
|---|
| 109 | void startCommunicationThread(); | 
|---|
| 110 | void stopCommunicationThread(); | 
|---|
| 111 |  | 
|---|
| 112 | void addPacket(ENetPacket *packet, uint32_t peerID, uint8_t channelID); | 
|---|
| 113 | void broadcastPacket(ENetPacket* packet, uint8_t channelID); | 
|---|
| 114 | void disconnectPeer(uint32_t peerID); | 
|---|
| 115 | void disconnectPeers(); | 
|---|
| 116 |  | 
|---|
| 117 | void enableCompression(); | 
|---|
| 118 |  | 
|---|
| 119 | void processQueue(); | 
|---|
| 120 | void waitOutgoingQueue();     // wait for the outgoing queue to become empty (everything processed by communication thread) | 
|---|
| 121 | virtual void addPeer(uint32_t peerID)=0; | 
|---|
| 122 | virtual void removePeer(uint32_t peerID)=0; | 
|---|
| 123 | virtual void processPacket( packet::Packet* packet)=0; | 
|---|
| 124 |  | 
|---|
| 125 | incomingEvent preprocessConnectEvent(ENetEvent& event); | 
|---|
| 126 | incomingEvent preprocessDisconnectEvent(ENetEvent& event); | 
|---|
| 127 | incomingEvent preprocessReceiveEvent(ENetEvent& event); | 
|---|
| 128 |  | 
|---|
| 129 | void processIncomingEvent(ENetEvent& event); | 
|---|
| 130 | void processOutgoingEvent(outgoingEvent& event); | 
|---|
| 131 |  | 
|---|
| 132 | void disconnectPeersInternal(); | 
|---|
| 133 |  | 
|---|
| 134 | ENetHost*                     host_; | 
|---|
| 135 | private: | 
|---|
| 136 | void communicationThread(); | 
|---|
| 137 |  | 
|---|
| 138 | boost::thread*                communicationThread_; | 
|---|
| 139 | bool                          bCommunicationThreadRunning_; | 
|---|
| 140 | ENetAddress*                  bindAddress_; | 
|---|
| 141 | std::deque<incomingEvent>     incomingEvents_; | 
|---|
| 142 | std::deque<outgoingEvent>     outgoingEvents_; | 
|---|
| 143 | boost::mutex*                 incomingEventsMutex_; | 
|---|
| 144 | boost::mutex*                 outgoingEventsMutex_; | 
|---|
| 145 | boost::mutex*                 overallMutex_; | 
|---|
| 146 | std::map<uint32_t, ENetPeer*> peerMap_; | 
|---|
| 147 | std::map<ENetPeer*, uint32_t> peerIDMap_; | 
|---|
| 148 | uint32_t                      nextPeerID_; | 
|---|
| 149 |  | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | #endif /* _Connection_H__ */ | 
|---|