root/jack2/branches/libjacknet/common/JackNetInterface.h

Revision 3955, 6.3 kB (checked in by sletz, 6 months ago)

Improve master mode.

Line 
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2008 Romain Moret at Grame
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __JackNetInterface__
22 #define __JackNetInterface__
23
24 #include "JackNetTool.h"
25
26 namespace Jack
27 {
28     /**
29     \Brief This class describes the basic Net Interface, used by both master and slave
30     */
31
32     class SERVER_EXPORT JackNetInterface
33     {
34         private:
35        
36             void Initialize();
37            
38         protected:
39        
40             session_params_t fParams;
41             JackNetSocket fSocket;
42             char fMulticastIP[32];
43        
44             //headers
45             packet_header_t fTxHeader;
46             packet_header_t fRxHeader;
47            
48             // transport
49             net_transport_data_t fSendTransportData;
50             net_transport_data_t fReturnTransportData;
51
52             //network buffers
53             char* fTxBuffer;
54             char* fRxBuffer;
55             char* fTxData;
56             char* fRxData;
57
58             //jack buffers
59             NetMidiBuffer* fNetMidiCaptureBuffer;
60             NetMidiBuffer* fNetMidiPlaybackBuffer;
61             NetAudioBuffer* fNetAudioCaptureBuffer;
62             NetAudioBuffer* fNetAudioPlaybackBuffer;
63
64             //utility methods
65             int SetNetBufferSize();
66             void FreeNetworkBuffers();
67      
68             //virtual methods : depends on the sub class master/slave
69             virtual bool SetParams();
70             virtual bool Init() = 0;
71
72             //transport
73             virtual void EncodeTransportData() = 0;
74             virtual void DecodeTransportData() = 0;
75
76             //sync packet
77             virtual void EncodeSyncPacket() = 0;
78             virtual void DecodeSyncPacket() = 0;
79
80             virtual int SyncRecv() = 0;
81             virtual int SyncSend() = 0;
82             virtual int DataRecv() = 0;
83             virtual int DataSend() = 0;
84
85             virtual int Send ( size_t size, int flags ) = 0;
86             virtual int Recv ( size_t size, int flags ) = 0;
87
88             JackNetInterface();
89             JackNetInterface ( const char* multicast_ip, int port );
90             JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
91
92         public:
93             virtual ~JackNetInterface();
94     };
95
96     /**
97     \Brief This class describes the Net Interface for masters (NetMaster)
98     */
99
100     class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
101     {
102         protected:
103             bool fRunning;
104             int fCycleOffset;
105             int fLastfCycleOffset;
106
107             bool Init();
108             int SetRxTimeout();
109             bool SetParams();
110            
111             void Exit();
112            
113             int SyncRecv();
114             int SyncSend();
115            
116             int DataRecv();
117             int DataSend();
118            
119              //sync packet
120             void EncodeSyncPacket();
121             void DecodeSyncPacket();
122
123             int Send ( size_t size, int flags );
124             int Recv ( size_t size, int flags );
125            
126             bool IsSynched();
127
128         public:
129             JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0), fLastfCycleOffset(0)
130             {}
131             JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
132                     : JackNetInterface ( params, socket, multicast_ip )
133             {}
134             ~JackNetMasterInterface()
135             {}
136     };
137
138     /**
139     \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
140     */
141
142     class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
143     {
144         protected:
145        
146             static uint fSlaveCounter;
147        
148             bool Init();
149             bool InitConnection(int time_out);
150             bool InitRendering();
151            
152             net_status_t SendAvailableToMaster(long count = LONG_MAX);  // long here (and not int...)
153             net_status_t SendStartToMaster();
154            
155             bool SetParams();
156            
157             int SyncRecv();
158             int SyncSend();
159            
160             int DataRecv();
161             int DataSend();
162            
163             //sync packet
164             void EncodeSyncPacket();
165             void DecodeSyncPacket();
166
167             int Recv ( size_t size, int flags );
168             int Send ( size_t size, int flags );
169
170         public:
171             JackNetSlaveInterface() : JackNetInterface()
172             {
173                 //open Socket API with the first slave
174                 if ( fSlaveCounter++ == 0 )
175                 {
176                     if ( SocketAPIInit() < 0 )
177                     {
178                         jack_error ( "Can't init Socket API, exiting..." );
179                         throw -1;
180                     }
181                 }
182             }
183             JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
184             {
185                 //open Socket API with the first slave
186                 if ( fSlaveCounter++ == 0 )
187                 {
188                     if ( SocketAPIInit() < 0 )
189                     {
190                         jack_error ( "Can't init Socket API, exiting..." );
191                         throw -1;
192                     }
193                 }
194             }
195             ~JackNetSlaveInterface()
196             {
197                 //close Socket API with the last slave
198                 if ( --fSlaveCounter == 0 )
199                     SocketAPIEnd();
200             }
201     };
202 }
203
204 #define DEFAULT_MULTICAST_IP "225.3.19.154"
205 #define DEFAULT_PORT 19000
206 #define DEFAULT_MTU 1500
207
208 #define SLAVE_SETUP_RETRY 5
209
210 #define MASTER_INIT_TIMEOUT 1000000     // in usec
211 #define SLAVE_INIT_TIMEOUT 1000000      // in usec
212
213 #define CYCLE_OFFSET_FAST   0
214 #define CYCLE_OFFSET_NORMAL 1
215 #define CYCLE_OFFSET_SLOW   30
216 #define MAX_LATENCY CYCLE_OFFSET_SLOW * 4
217
218 #endif
Note: See TracBrowser for help on using the browser.