source: osgVisual/trunk/include/cluster/dataIO_clusterENet_implementation.h @ 252

Last change on this file since 252 was 221, checked in by Torben Dannhauer, 14 years ago

Updated copyright NOTICE
(No code changes)

File size: 8.2 KB
RevLine 
[65]1#pragma once
[221]2/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
[68]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
[94]19#include <osg/Referenced>
[65]20#include <enet/enet.h>
21#include <iostream>
22#include <string>
23#include <vector>
24
[66]25namespace 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]38class dataIO_clusterENet_implementation :       public osg::Referenced
[65]39{
[88]40        #include <leakDetection.h>
[65]41public:
[77]42        /**
43         * \brief Constructor: Constructs this class and starts the ENet subsystem if this instance is the first instantiated one in the program.
44         *
45         * @param receivedTransportContainer_ : TransportContainer to interact with. See also receivedTransportContainer
46         */ 
[69]47        dataIO_clusterENet_implementation(std::string& receivedTransportContainer_);
[77]48
49        /**
50         * \brief Destructor: Destructs this class. In the last program wide application it shuts the ENet subsystem down.
51         *
52         */ 
[66]53        ~dataIO_clusterENet_implementation();
[65]54
[77]55        /**
[80]56         * This enum contains valid roles of this class. In contrast to the dataIO_cluster implementation this class do NOT use MASTER/SLAVE,
57         * because this class implements an non-abstract DUP connection with SERVER and CLIENT. As soon as osgVisual uses only the convenience
58         * functions of clusterENet, the roles are called as usual MASTER/SLAVE.
[77]59         */ 
[65]60        enum role {SERVER, CLIENT};
61
[77]62        /**
[80]63         * \brief This function initializes this class and allow to use it's service. Call this function as SERVER as well as CLIENT prior to any usage.
[77]64         *
[80]65         * @param role_ : Role to act like (SERVER/CLIENT)
[77]66         * @param port_ : Port to use.
[80]67         * @param maxClients_ : Maximum of allowed connected clients. (Affects only the role SERVER)
68         * @param maxChannels_ : Maximum of channels per client. (Affects only the role SERVER)
[77]69         * @param maxInBandwidth_ : Maximal incoming bandwith in bytes per second [bps]. Set 0 to rely on ENets internal throtteling algorithm.
70         * @param maxOutBandwidth_ : Maximal outgoing bandwith in bytes per second [bps]. Set 0 to rely on ENets internal throtteling algorithm.
71         * @return : True on successful init.
72         */ 
73        bool init( dataIO_clusterENet_implementation::role role_, unsigned short port_, int maxClients_=10, int maxChannels_=2, int maxInBandwidth_=0, int maxOutBandwidth_=0 );       
74
75        /**
[80]76         * \brief This function connects to a ENet serverat the specified adress. The function affects only the CLIENT role.
[77]77         *
78         * @param remoteAddr_ : Address of the ENet server to connect to.
79         * @param connectTimeout_ms_ : Connect timeout in milliseconds [ms].
[80]80         * @param clientInfo_ : Client data to transfer to the SERVER during connect. Usually not required.
[77]81         * @param channelToAlloc_ : Max number of non-concurring channels to open.
82         * @return : True on successful connect.
83         */ 
84        bool connectTo( const char* remoteAddr_, int connectTimeout_ms_, int clientInfo_=0,  int channelToAlloc_=2 );
[65]85       
[77]86        /**
[114]87         * \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 SERVER to CLIENT and vice versa. This function takes ownership of the packet and will destroy it after (un-)successful transmission.
[77]88         *
89         * @param packet_ : Data packet to broadcast.
90         * @param channelID_ : ID on which channel the broadcast should be done.
91         * @param peerID_ : Peer ID to send the packet to.
[80]92         * @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 SERVER to CLIENT and vice versa.
[77]93         */ 
94        void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, unsigned int peerID_=0, bool autoFlush_=false );
95
96        /**
[114]97         * \brief This function send a packet to the peer with the specified peerName. This function works bidirectional from SERVER to CLIENT and vice versa. This function takes ownership of the packet and will destroy it after (un-)successful transmission.
[77]98         *
99         * @param channelID_ : ID on which channel the broadcast should be done.
100         * @param peerName_ : Peer name to send to data to.
101         * @param packet_ : Data packet to broadcast.
102         * @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.).
103         */ 
104        void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, std::string peerName_, bool autoFlush_=false );
105
106        /**
[80]107         * \brief : This function emulates a UDP broadcast by sending the packet manually to all connected peers. This function works only as SERVER. Calls as CLIENT are ignored.
[77]108         *
109         * @param channelID_ : ID on which channel the broadcast should be done.
110         * @param packet_ : Data packet to broadcast.
111         * @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.).
112         */ 
113        void broadcastPacket( enet_uint8 channelID_, ENetPacket* packet_, bool autoFlush_=false );
114
115        /**
116         * \brief : Call this function to process all pending events like connects, disconnects, receives or sends.
117         *
118         * @param timeout_ms_ :  Maximal time to wait for incomming events in milliseconds [ms].
119         */ 
120        void processEvents( int timeout_ms_ = 0 );
121       
122        /**
123         * \brief This function handles the receive of a data packet.
124         *
125         * @param event_ : Receive event.
126         */ 
[65]127        virtual inline void onReceivePacket(ENetEvent* event_);
[77]128
129        /**
130         * \brief This function handles the connect of a peer. This function will be called only by the host waiting for the connection.
131         *
132         * @param event_ : Connect event.
133         */ 
134        virtual inline void onConnect(ENetEvent* event_);
135       
136        /**
137         * \brief This function handles the disconnect of a peer.
138         *
139         * @param event_ : Disconnect event.
140         */ 
[65]141        virtual inline void onDisconnect(ENetEvent* event_);
142
143protected:
[77]144        /**
145         * UDP port to use.
146         */ 
[65]147        unsigned short port;
[77]148
149        /**
150         * This enum indicates the current role this class at as.
151         */ 
[66]152        dataIO_clusterENet_implementation::role currentRole;
[77]153
154        /**
155         * This variable indicates the number of programm wide used instances of this class. one the first class starts the ENet subsystem and only the
156         * last one shuts the ENet subsystem down.
157         */
[65]158        static int activeENetInstances;
[77]159
160        /**
161         * This flag indicates if the complete ENet wrapper class is initialized. (After init())
162         */ 
163        bool enetInitialized;
164
165        /**
166         * ENet adress structure, which contains information regarding the interface and address.
167         */ 
168        ENetAddress address; 
169
170        /**
171         * Host structure, which contains the own host role for the conmmunication.
172         */ 
[65]173    ENetHost* host;
[77]174
175        /**
[80]176         * Lisst of the ENet remote peers. This list contains only the server during acting as CLIENT (peerList[0]=server), and multiple slave entries during acting as SERVER (peerList[0-n]=client0-n).
[77]177         */ 
178        std::vector<ENetPeer*> peerList;
179
180        /**
181         * Eventclass which contains the data of the events dureing connect, resieve or send.
182         */ 
[65]183        ENetEvent event;
[77]184
185        /**
186         * Reference to the transportContainer class to store data into on receive and read from during sending.
187         */ 
[69]188        std::string& receivedTransportContainer;
[66]189};
190
191}       // END NAMESPACE
Note: See TracBrowser for help on using the repository browser.