Changeset 77 for osgVisual/include
- Timestamp:
- Jul 25, 2010, 5:05:29 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
osgVisual/include/cluster/dataIO_clusterENet_implementation.h
r69 r77 26 26 { 27 27 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 */ 28 38 class dataIO_clusterENet_implementation : public osg::Referenced 29 39 { 30 40 public: 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 */ 31 46 dataIO_clusterENet_implementation(std::string& receivedTransportContainer_); 47 48 /** 49 * \brief Destructor: Destructs this class. In the last program wide application it shuts the ENet subsystem down. 50 * 51 */ 32 52 ~dataIO_clusterENet_implementation(); 33 53 54 /** 55 * This enum contains valid roles of this class. 56 */ 34 57 enum role {SERVER, CLIENT}; 35 58 36 bool init( dataIO_clusterENet_implementation::role role_, unsigned short port_, int maxClients_=10, int maxChannels_=2, int maxInBandwidth_=0, int maxOutBandwidth_=0 ); // client & server // Done 37 bool connectTo( const char* remoteAddr_, int connectTimeout_ms_, int clientInfo_=0, int channelToAlloc_=2 ); // only as client // Done 38 void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, unsigned int peerID_=0, bool autoFlush_=false ); // by PeerID // Done 39 void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, std::string peerName_, bool autoFlush_=false ); // by peer name // Done 40 void broadcastPacket( enet_uint8 channelID_, ENetPacket* packet_, bool autoFlush_=false ); // only as server. // Done 41 void processEvents( int timeout_ms_ = 0 ); // Done 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 ); 42 82 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 */ 43 124 virtual inline void onReceivePacket(ENetEvent* event_); 44 virtual inline void onConnect(ENetEvent* event_); // This function will be called only by the host waiting for the connection. 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 */ 45 138 virtual inline void onDisconnect(ENetEvent* event_); 46 139 47 140 protected: 141 /** 142 * UDP port to use. 143 */ 48 144 unsigned short port; 145 146 /** 147 * This enum indicates the current role this class at as. 148 */ 49 149 dataIO_clusterENet_implementation::role currentRole; 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 */ 50 155 static int activeENetInstances; 51 bool serverInitialized; // shows if the complete server is initialized. (After init()) 52 ENetAddress address; 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 */ 53 170 ENetHost* host; 54 std::vector<ENetPeer*> peerList; // client: only one peer to server, Server: multiple peers for all clients 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 */ 55 180 ENetEvent event; 181 182 /** 183 * Reference to the transportContainer class to store data into on receive and read from during sending. 184 */ 56 185 std::string& receivedTransportContainer; 57 186 };
Note: See TracChangeset
for help on using the changeset viewer.