[65] | 1 | #pragma once |
---|
[68] | 2 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
| 3 | * |
---|
| 4 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
| 5 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
| 6 | * (at your option) any later version. The full license is in LICENSE file |
---|
| 7 | * included with this distribution, and on the openscenegraph.org website. |
---|
| 8 | * |
---|
| 9 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
| 10 | * You have to aquire licenses for all used proprietary modules. |
---|
| 11 | * |
---|
| 12 | * This library is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * OpenSceneGraph Public License for more details. |
---|
| 16 | */ |
---|
[65] | 17 | |
---|
[68] | 18 | |
---|
[66] | 19 | #include <osg/referenced> |
---|
[65] | 20 | #include <enet/enet.h> |
---|
| 21 | #include <iostream> |
---|
| 22 | #include <string> |
---|
| 23 | #include <vector> |
---|
| 24 | |
---|
[66] | 25 | namespace osgVisual |
---|
| 26 | { |
---|
[65] | 27 | |
---|
[77] | 28 | /** |
---|
| 29 | * \brief This class is a object oriented wrapper for the ENet UDP implementation. |
---|
| 30 | * |
---|
| 31 | * This class allows to create a handler for the complete ENet operations. |
---|
| 32 | * ENet is a cross plattform UDP implementation with some reliability functions. |
---|
| 33 | * For further details, loot at http://enet.bespin.org/index.html |
---|
| 34 | * |
---|
| 35 | * @author Torben Dannhauer |
---|
| 36 | * @date Jul 2010 |
---|
| 37 | */ |
---|
[66] | 38 | class dataIO_clusterENet_implementation : public osg::Referenced |
---|
[65] | 39 | { |
---|
| 40 | public: |
---|
[77] | 41 | /** |
---|
| 42 | * \brief Constructor: Constructs this class and starts the ENet subsystem if this instance is the first instantiated one in the program. |
---|
| 43 | * |
---|
| 44 | * @param receivedTransportContainer_ : TransportContainer to interact with. See also receivedTransportContainer |
---|
| 45 | */ |
---|
[69] | 46 | dataIO_clusterENet_implementation(std::string& receivedTransportContainer_); |
---|
[77] | 47 | |
---|
| 48 | /** |
---|
| 49 | * \brief Destructor: Destructs this class. In the last program wide application it shuts the ENet subsystem down. |
---|
| 50 | * |
---|
| 51 | */ |
---|
[66] | 52 | ~dataIO_clusterENet_implementation(); |
---|
[65] | 53 | |
---|
[77] | 54 | /** |
---|
| 55 | * This enum contains valid roles of this class. |
---|
| 56 | */ |
---|
[65] | 57 | enum role {SERVER, CLIENT}; |
---|
| 58 | |
---|
[77] | 59 | /** |
---|
| 60 | * \brief This function initializes this class and allow to use it's service. Call this function as MASTER as well as SLAVE prior to any usage. |
---|
| 61 | * |
---|
| 62 | * @param role_ : Role to act like (MASTER/SLAVE) |
---|
| 63 | * @param port_ : Port to use. |
---|
| 64 | * @param maxClients_ : Maximum of allowed connected clients. (Affects only the role MASTER) |
---|
| 65 | * @param maxChannels_ : Maximum of channels per client. (Affects only the role MASTER) |
---|
| 66 | * @param maxInBandwidth_ : Maximal incoming bandwith in bytes per second [bps]. Set 0 to rely on ENets internal throtteling algorithm. |
---|
| 67 | * @param maxOutBandwidth_ : Maximal outgoing bandwith in bytes per second [bps]. Set 0 to rely on ENets internal throtteling algorithm. |
---|
| 68 | * @return : True on successful init. |
---|
| 69 | */ |
---|
| 70 | bool init( dataIO_clusterENet_implementation::role role_, unsigned short port_, int maxClients_=10, int maxChannels_=2, int maxInBandwidth_=0, int maxOutBandwidth_=0 ); |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * \brief This function connects to a ENet serverat the specified adress. The function affects only the SLAVE role. |
---|
| 74 | * |
---|
| 75 | * @param remoteAddr_ : Address of the ENet server to connect to. |
---|
| 76 | * @param connectTimeout_ms_ : Connect timeout in milliseconds [ms]. |
---|
| 77 | * @param clientInfo_ : Client data to transfer to the MASTER during connect. Usually not required. |
---|
| 78 | * @param channelToAlloc_ : Max number of non-concurring channels to open. |
---|
| 79 | * @return : True on successful connect. |
---|
| 80 | */ |
---|
| 81 | bool connectTo( const char* remoteAddr_, int connectTimeout_ms_, int clientInfo_=0, int channelToAlloc_=2 ); |
---|
[65] | 82 | |
---|
[77] | 83 | /** |
---|
| 84 | * \brief This function send a packet to the peer with the specified peer ID (number of the peer in the peer vector). This function works bidirectional from MASTER to SLAVE and vice versa. |
---|
| 85 | * |
---|
| 86 | * @param packet_ : Data packet to broadcast. |
---|
| 87 | * @param channelID_ : ID on which channel the broadcast should be done. |
---|
| 88 | * @param peerID_ : Peer ID to send the packet to. |
---|
| 89 | * @param autoFlush_ : Indicates if the packed should be send immediately after append the packet to the ENets internal send list (like processEvents() with only sending the packet). This function works bidirectional from MASTER to SLAVE and vice versa. |
---|
| 90 | */ |
---|
| 91 | void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, unsigned int peerID_=0, bool autoFlush_=false ); |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * \brief This function send a packet to the peer with the specified peerName. |
---|
| 95 | * |
---|
| 96 | * @param channelID_ : ID on which channel the broadcast should be done. |
---|
| 97 | * @param peerName_ : Peer name to send to data to. |
---|
| 98 | * @param packet_ : Data packet to broadcast. |
---|
| 99 | * @param autoFlush_ : Indicates if the packed should be send immediately after append the packet to the ENets internal send list (like processEvents() with only sending the packet.). |
---|
| 100 | */ |
---|
| 101 | void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, std::string peerName_, bool autoFlush_=false ); |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * \brief : This function emulates a UDP broadcast by sending the packet manually to all connected peers. This function works only as MASTER. Calls as SLAVE are ignored. |
---|
| 105 | * |
---|
| 106 | * @param channelID_ : ID on which channel the broadcast should be done. |
---|
| 107 | * @param packet_ : Data packet to broadcast. |
---|
| 108 | * @param autoFlush_ : Indicates if the packed should be send immediately after append the packet to the ENets internal send list (like processEvents() with only sending the packet.). |
---|
| 109 | */ |
---|
| 110 | void broadcastPacket( enet_uint8 channelID_, ENetPacket* packet_, bool autoFlush_=false ); |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * \brief : Call this function to process all pending events like connects, disconnects, receives or sends. |
---|
| 114 | * |
---|
| 115 | * @param timeout_ms_ : Maximal time to wait for incomming events in milliseconds [ms]. |
---|
| 116 | */ |
---|
| 117 | void processEvents( int timeout_ms_ = 0 ); |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * \brief This function handles the receive of a data packet. |
---|
| 121 | * |
---|
| 122 | * @param event_ : Receive event. |
---|
| 123 | */ |
---|
[65] | 124 | virtual inline void onReceivePacket(ENetEvent* event_); |
---|
[77] | 125 | |
---|
| 126 | /** |
---|
| 127 | * \brief This function handles the connect of a peer. This function will be called only by the host waiting for the connection. |
---|
| 128 | * |
---|
| 129 | * @param event_ : Connect event. |
---|
| 130 | */ |
---|
| 131 | virtual inline void onConnect(ENetEvent* event_); |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * \brief This function handles the disconnect of a peer. |
---|
| 135 | * |
---|
| 136 | * @param event_ : Disconnect event. |
---|
| 137 | */ |
---|
[65] | 138 | virtual inline void onDisconnect(ENetEvent* event_); |
---|
| 139 | |
---|
| 140 | protected: |
---|
[77] | 141 | /** |
---|
| 142 | * UDP port to use. |
---|
| 143 | */ |
---|
[65] | 144 | unsigned short port; |
---|
[77] | 145 | |
---|
| 146 | /** |
---|
| 147 | * This enum indicates the current role this class at as. |
---|
| 148 | */ |
---|
[66] | 149 | dataIO_clusterENet_implementation::role currentRole; |
---|
[77] | 150 | |
---|
| 151 | /** |
---|
| 152 | * This variable indicates the number of programm wide used instances of this class. one the first class starts the ENet subsystem and only the |
---|
| 153 | * last one shuts the ENet subsystem down. |
---|
| 154 | */ |
---|
[65] | 155 | static int activeENetInstances; |
---|
[77] | 156 | |
---|
| 157 | /** |
---|
| 158 | * This flag indicates if the complete ENet wrapper class is initialized. (After init()) |
---|
| 159 | */ |
---|
| 160 | bool enetInitialized; |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * ENet adress structure, which contains information regarding the interface and address. |
---|
| 164 | */ |
---|
| 165 | ENetAddress address; |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Host structure, which contains the own host role for the conmmunication. |
---|
| 169 | */ |
---|
[65] | 170 | ENetHost* host; |
---|
[77] | 171 | |
---|
| 172 | /** |
---|
| 173 | * Lisst of the ENet remote peers. This list contains only the server during acting as SLAVE (peerList[0]=server), and multiple slave entries during acting as MASTER (peerList[0-n]=client0-n). |
---|
| 174 | */ |
---|
| 175 | std::vector<ENetPeer*> peerList; |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * Eventclass which contains the data of the events dureing connect, resieve or send. |
---|
| 179 | */ |
---|
[65] | 180 | ENetEvent event; |
---|
[77] | 181 | |
---|
| 182 | /** |
---|
| 183 | * Reference to the transportContainer class to store data into on receive and read from during sending. |
---|
| 184 | */ |
---|
[69] | 185 | std::string& receivedTransportContainer; |
---|
[66] | 186 | }; |
---|
| 187 | |
---|
| 188 | } // END NAMESPACE |
---|