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

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

memory leak in clusterENet fixed

File size: 8.2 KB
Line 
1#pragma once
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*/
17
18
19#include <osg/Referenced>
20#include <enet/enet.h>
21#include <iostream>
22#include <string>
23#include <vector>
24
25namespace osgVisual
26{
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 */ 
38class dataIO_clusterENet_implementation :       public osg::Referenced
39{
40        #include <leakDetection.h>
41public:
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         */ 
47        dataIO_clusterENet_implementation(std::string& receivedTransportContainer_);
48
49        /**
50         * \brief Destructor: Destructs this class. In the last program wide application it shuts the ENet subsystem down.
51         *
52         */ 
53        ~dataIO_clusterENet_implementation();
54
55        /**
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.
59         */ 
60        enum role {SERVER, CLIENT};
61
62        /**
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.
64         *
65         * @param role_ : Role to act like (SERVER/CLIENT)
66         * @param port_ : Port to use.
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)
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        /**
76         * \brief This function connects to a ENet serverat the specified adress. The function affects only the CLIENT role.
77         *
78         * @param remoteAddr_ : Address of the ENet server to connect to.
79         * @param connectTimeout_ms_ : Connect timeout in milliseconds [ms].
80         * @param clientInfo_ : Client data to transfer to the SERVER during connect. Usually not required.
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 );
85       
86        /**
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.
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.
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.
93         */ 
94        void sendPacket( ENetPacket* packet_, enet_uint8 channelID_, unsigned int peerID_=0, bool autoFlush_=false );
95
96        /**
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.
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        /**
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.
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         */ 
127        virtual inline void onReceivePacket(ENetEvent* event_);
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         */ 
141        virtual inline void onDisconnect(ENetEvent* event_);
142
143protected:
144        /**
145         * UDP port to use.
146         */ 
147        unsigned short port;
148
149        /**
150         * This enum indicates the current role this class at as.
151         */ 
152        dataIO_clusterENet_implementation::role currentRole;
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         */
158        static int activeENetInstances;
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         */ 
173    ENetHost* host;
174
175        /**
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).
177         */ 
178        std::vector<ENetPeer*> peerList;
179
180        /**
181         * Eventclass which contains the data of the events dureing connect, resieve or send.
182         */ 
183        ENetEvent event;
184
185        /**
186         * Reference to the transportContainer class to store data into on receive and read from during sending.
187         */ 
188        std::string& receivedTransportContainer;
189};
190
191}       // END NAMESPACE
Note: See TracBrowser for help on using the repository browser.