1 | #pragma once |
---|
2 | #if _MSC_VER > 1000 |
---|
3 | #pragma warning(disable:4146) |
---|
4 | #endif // _MSC_VER > 1000 |
---|
5 | |
---|
6 | #include <osg/referenced> |
---|
7 | #include <enet/enet.h> |
---|
8 | #include <iostream> |
---|
9 | #include <string> |
---|
10 | #include <vector> |
---|
11 | |
---|
12 | namespace osgVisual |
---|
13 | { |
---|
14 | |
---|
15 | class dataIO_clusterENet_implementation : public osg::Referenced |
---|
16 | { |
---|
17 | public: |
---|
18 | dataIO_clusterENet_implementation(); |
---|
19 | ~dataIO_clusterENet_implementation(); |
---|
20 | |
---|
21 | enum role {SERVER, CLIENT}; |
---|
22 | |
---|
23 | 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 |
---|
24 | bool connectTo( const char* remoteAddr_, int connectTimeout_ms_, int clientInfo_=0, int channelToAlloc_=2 ); // only as client // Done |
---|
25 | void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, unsigned int peerID_=0, bool autoFlush_=false ); // by PeerID // Done |
---|
26 | void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, std::string peerName_, bool autoFlush_=false ); // by peer name // Done |
---|
27 | void broadcastPacket( enet_uint8 channelID_, ENetPacket* packet_, bool autoFlush_=false ); // only as server. // Done |
---|
28 | void processEvents( int timeout_ms_ = 0 ); // Done |
---|
29 | |
---|
30 | virtual inline void onReceivePacket(ENetEvent* event_); |
---|
31 | virtual inline void onConnect(ENetEvent* event_); // This function will be called only by the host waiting for the connection. |
---|
32 | virtual inline void onDisconnect(ENetEvent* event_); |
---|
33 | |
---|
34 | protected: |
---|
35 | unsigned short port; |
---|
36 | dataIO_clusterENet_implementation::role currentRole; |
---|
37 | static int activeENetInstances; |
---|
38 | bool serverInitialized; // shows if the complete server is initialized. (After init()) |
---|
39 | ENetAddress address; |
---|
40 | ENetHost* host; |
---|
41 | std::vector<ENetPeer*> peerList; // client: only one peer to server, Server: multiple peers for all clients |
---|
42 | ENetEvent event; |
---|
43 | }; |
---|
44 | |
---|
45 | } // END NAMESPACE |
---|