[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 | // |
---|
| 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 | |
---|
[7801] | 45 | #include <deque> |
---|
[8327] | 46 | #include <map> |
---|
[7801] | 47 | #include <enet/enet.h> |
---|
| 48 | |
---|
| 49 | namespace boost |
---|
| 50 | { |
---|
| 51 | class thread; |
---|
| 52 | class mutex; |
---|
| 53 | } |
---|
| 54 | |
---|
[3214] | 55 | namespace orxonox |
---|
| 56 | { |
---|
[7801] | 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 | |
---|
[11006] | 62 | enum class IncomingEventType |
---|
[8327] | 63 | { |
---|
[11006] | 64 | receivePacket = 1, // incoming packet |
---|
| 65 | peerConnect = 2, // incoming connect request |
---|
| 66 | peerDisconnect = 3 // incoming disconnect request |
---|
| 67 | }; |
---|
[8327] | 68 | |
---|
[11006] | 69 | enum class OutgoingEventType |
---|
[7801] | 70 | { |
---|
[11006] | 71 | sendPacket = 1, // outgoing packet |
---|
| 72 | broadcastPacket = 2, // outgoing broadcast packet |
---|
| 73 | disconnectPeer = 3, // outgoing disconnect request |
---|
| 74 | disconnectPeers = 4 // outgoing disconnect request |
---|
| 75 | }; |
---|
[7801] | 76 | |
---|
[8327] | 77 | struct _NetworkExport incomingEvent |
---|
| 78 | { |
---|
[11006] | 79 | uint32_t peerID; |
---|
| 80 | IncomingEventType type; |
---|
| 81 | packet::Packet* packet; |
---|
[8327] | 82 | }; |
---|
| 83 | |
---|
[7801] | 84 | struct _NetworkExport outgoingEvent |
---|
| 85 | { |
---|
[11006] | 86 | uint32_t peerID; |
---|
| 87 | OutgoingEventType type; |
---|
| 88 | ENetPacket* packet; |
---|
| 89 | ENetChannelID channelID; |
---|
[7801] | 90 | }; |
---|
| 91 | |
---|
| 92 | class _NetworkExport Connection |
---|
| 93 | { |
---|
[3214] | 94 | public: |
---|
| 95 | virtual ~Connection(); |
---|
[6417] | 96 | |
---|
[3214] | 97 | protected: |
---|
[8327] | 98 | Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER+1); |
---|
| 99 | |
---|
[7801] | 100 | void startCommunicationThread(); |
---|
| 101 | void stopCommunicationThread(); |
---|
| 102 | |
---|
[8327] | 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 | |
---|
[7801] | 108 | void enableCompression(); |
---|
[6417] | 109 | |
---|
[3214] | 110 | void processQueue(); |
---|
[8327] | 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; |
---|
[7801] | 114 | virtual void processPacket( packet::Packet* packet)=0; |
---|
[8327] | 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(); |
---|
[6417] | 124 | |
---|
[8327] | 125 | ENetHost* host_; |
---|
[3214] | 126 | private: |
---|
[8327] | 127 | void communicationThread(); |
---|
| 128 | |
---|
| 129 | boost::thread* communicationThread_; |
---|
| 130 | bool bCommunicationThreadRunning_; |
---|
| 131 | ENetAddress* bindAddress_; |
---|
| 132 | std::deque<incomingEvent> incomingEvents_; |
---|
| 133 | std::deque<outgoingEvent> outgoingEvents_; |
---|
| 134 | boost::mutex* incomingEventsMutex_; |
---|
| 135 | boost::mutex* outgoingEventsMutex_; |
---|
| 136 | boost::mutex* overallMutex_; |
---|
| 137 | std::map<uint32_t, ENetPeer*> peerMap_; |
---|
| 138 | std::map<ENetPeer*, uint32_t> peerIDMap_; |
---|
| 139 | uint32_t nextPeerID_; |
---|
[3214] | 140 | |
---|
| 141 | }; |
---|
| 142 | |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | #endif /* _Connection_H__ */ |
---|