Jack2  1.9.9
JackNetInterface.h
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #ifndef __JackNetInterface__
21 #define __JackNetInterface__
22 
23 #include "JackNetTool.h"
24 #include <limits.h>
25 
26 namespace Jack
27 {
28 
29 #define DEFAULT_MULTICAST_IP "225.3.19.154"
30 #define DEFAULT_PORT 19000
31 #define DEFAULT_MTU 1500
32 
33 #define SLAVE_SETUP_RETRY 5
34 
35 #define MANAGER_INIT_TIMEOUT 2000000 // in usec
36 #define MASTER_INIT_TIMEOUT 1000000 * 10 // in usec
37 #define SLAVE_INIT_TIMEOUT 1000000 * 10 // in usec
38 #define PACKET_TIMEOUT 500000 // in usec
39 
40 #define NETWORK_MAX_LATENCY 20
41 
46  class SERVER_EXPORT JackNetInterface
47  {
48 
49  protected:
50 
51  bool fSetTimeOut;
52 
53  void Initialize();
54 
55  session_params_t fParams;
56  JackNetSocket fSocket;
57  char fMulticastIP[32];
58 
59  // headers
60  packet_header_t fTxHeader;
61  packet_header_t fRxHeader;
62 
63  // transport
64  net_transport_data_t fSendTransportData;
65  net_transport_data_t fReturnTransportData;
66 
67  // network buffers
68  char* fTxBuffer;
69  char* fRxBuffer;
70  char* fTxData;
71  char* fRxData;
72 
73  // JACK buffers
74  NetMidiBuffer* fNetMidiCaptureBuffer;
75  NetMidiBuffer* fNetMidiPlaybackBuffer;
76  NetAudioBuffer* fNetAudioCaptureBuffer;
77  NetAudioBuffer* fNetAudioPlaybackBuffer;
78 
79  // utility methods
80  int SetNetBufferSize();
81  void FreeNetworkBuffers();
82 
83  // virtual methods : depends on the sub class master/slave
84  virtual bool SetParams();
85  virtual bool Init() = 0;
86 
87  // transport
88  virtual void EncodeTransportData() = 0;
89  virtual void DecodeTransportData() = 0;
90 
91  // sync packet
92  virtual void EncodeSyncPacket() = 0;
93  virtual void DecodeSyncPacket() = 0;
94 
95  virtual int SyncRecv() = 0;
96  virtual int SyncSend() = 0;
97  virtual int DataRecv() = 0;
98  virtual int DataSend() = 0;
99 
100  virtual int Send(size_t size, int flags) = 0;
101  virtual int Recv(size_t size, int flags) = 0;
102 
103  virtual void FatalRecvError() = 0;
104  virtual void FatalSendError() = 0;
105 
106  int MidiSend(NetMidiBuffer* buffer, int midi_channnels, int audio_channels);
107  int AudioSend(NetAudioBuffer* buffer, int audio_channels);
108 
109  int MidiRecv(packet_header_t* rx_head, NetMidiBuffer* buffer, uint& recvd_midi_pckt);
110  int AudioRecv(packet_header_t* rx_head, NetAudioBuffer* buffer);
111 
112  int FinishRecv(NetAudioBuffer* buffer);
113 
114  void SetRcvTimeOut();
115 
116  NetAudioBuffer* AudioBufferFactory(int nports, char* buffer);
117 
118  public:
119 
121  JackNetInterface(const char* multicast_ip, int port);
122  JackNetInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip);
123 
124  virtual ~JackNetInterface();
125 
126  };
127 
132  class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
133  {
134 
135  protected:
136 
137  bool fRunning;
138  int fCurrentCycleOffset;
139  int fMaxCycleOffset;
140 
141  bool Init();
142  bool SetParams();
143 
144  void Exit();
145 
146  int SyncRecv();
147  int SyncSend();
148 
149  int DataRecv();
150  int DataSend();
151 
152  // sync packet
153  void EncodeSyncPacket();
154  void DecodeSyncPacket();
155 
156  int Send(size_t size, int flags);
157  int Recv(size_t size, int flags);
158 
159  bool IsSynched();
160 
161  void FatalRecvError();
162  void FatalSendError();
163 
164  public:
165 
166  JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0)
167  {}
168  JackNetMasterInterface(session_params_t& params, JackNetSocket& socket, const char* multicast_ip)
169  : JackNetInterface(params, socket, multicast_ip), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0)
170  {}
171 
172  virtual~JackNetMasterInterface()
173  {}
174  };
175 
180  class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
181  {
182 
183  protected:
184 
185  static uint fSlaveCounter;
186 
187  bool Init();
188  bool InitConnection(int time_out_sec);
189  bool InitRendering();
190 
191  net_status_t SendAvailableToMaster(long count = LONG_MAX); // long here (and not int...)
192  net_status_t SendStartToMaster();
193 
194  bool SetParams();
195 
196  int SyncRecv();
197  int SyncSend();
198 
199  int DataRecv();
200  int DataSend();
201 
202  // sync packet
203  void EncodeSyncPacket();
204  void DecodeSyncPacket();
205 
206  int Recv(size_t size, int flags);
207  int Send(size_t size, int flags);
208 
209  void FatalRecvError();
210  void FatalSendError();
211 
212  void InitAPI();
213 
214  public:
215 
217  {
218  InitAPI();
219  }
220 
221  JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
222  {
223  InitAPI();
224  }
225 
226  virtual ~JackNetSlaveInterface()
227  {
228  // close Socket API with the last slave
229  if (--fSlaveCounter == 0) {
230  SocketAPIEnd();
231  }
232  }
233  };
234 }
235 
236 #endif
This structure containes master/slave connection parameters, it&#39;s used to setup the whole system...
Definition: JackNetTool.h:88