2 Copyright (C) 2008-2011 Romain Moret at Grame
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.
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.
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.
20 #ifndef __JackNetInterface__
21 #define __JackNetInterface__
23 #include "JackNetTool.h"
29 #define DEFAULT_MULTICAST_IP "225.3.19.154"
30 #define DEFAULT_PORT 19000
31 #define DEFAULT_MTU 1500
33 #define SLAVE_SETUP_RETRY 5
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
40 #define NETWORK_MAX_LATENCY 20
43 \Brief This class describes the basic Net Interface, used by both master and slave.
46 class SERVER_EXPORT JackNetInterface
55 session_params_t fParams
;
56 JackNetSocket fSocket
;
57 char fMulticastIP
[32];
60 packet_header_t fTxHeader
;
61 packet_header_t fRxHeader
;
64 net_transport_data_t fSendTransportData
;
65 net_transport_data_t fReturnTransportData
;
74 NetMidiBuffer
* fNetMidiCaptureBuffer
;
75 NetMidiBuffer
* fNetMidiPlaybackBuffer
;
76 NetAudioBuffer
* fNetAudioCaptureBuffer
;
77 NetAudioBuffer
* fNetAudioPlaybackBuffer
;
80 int SetNetBufferSize();
81 void FreeNetworkBuffers();
83 // virtual methods : depends on the sub class master/slave
84 virtual bool SetParams();
85 virtual bool Init() = 0;
88 virtual void EncodeTransportData() = 0;
89 virtual void DecodeTransportData() = 0;
92 virtual void EncodeSyncPacket() = 0;
93 virtual void DecodeSyncPacket() = 0;
95 virtual int SyncRecv() = 0;
96 virtual int SyncSend() = 0;
97 virtual int DataRecv() = 0;
98 virtual int DataSend() = 0;
100 virtual int Send(size_t size
, int flags
) = 0;
101 virtual int Recv(size_t size
, int flags
) = 0;
103 virtual void FatalRecvError() = 0;
104 virtual void FatalSendError() = 0;
106 int MidiSend(NetMidiBuffer
* buffer
, int midi_channnels
, int audio_channels
);
107 int AudioSend(NetAudioBuffer
* buffer
, int audio_channels
);
109 int MidiRecv(packet_header_t
* rx_head
, NetMidiBuffer
* buffer
, uint
& recvd_midi_pckt
);
110 int AudioRecv(packet_header_t
* rx_head
, NetAudioBuffer
* buffer
);
112 int FinishRecv(NetAudioBuffer
* buffer
);
114 void SetRcvTimeOut();
116 NetAudioBuffer
* AudioBufferFactory(int nports
, char* buffer
);
121 JackNetInterface(const char* multicast_ip
, int port
);
122 JackNetInterface(session_params_t
& params
, JackNetSocket
& socket
, const char* multicast_ip
);
124 virtual ~JackNetInterface();
129 \Brief This class describes the Net Interface for masters (NetMaster)
132 class SERVER_EXPORT JackNetMasterInterface
: public JackNetInterface
138 int fCurrentCycleOffset
;
153 void EncodeSyncPacket();
154 void DecodeSyncPacket();
156 int Send(size_t size
, int flags
);
157 int Recv(size_t size
, int flags
);
161 void FatalRecvError();
162 void FatalSendError();
166 JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0)
168 JackNetMasterInterface(session_params_t
& params
, JackNetSocket
& socket
, const char* multicast_ip
)
169 : JackNetInterface(params
, socket
, multicast_ip
), fRunning(false), fCurrentCycleOffset(0), fMaxCycleOffset(0)
172 virtual~JackNetMasterInterface()
177 \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
180 class SERVER_EXPORT JackNetSlaveInterface
: public JackNetInterface
185 static uint fSlaveCounter
;
188 bool InitConnection(int time_out_sec
);
189 bool InitRendering();
191 net_status_t
SendAvailableToMaster(long count
= LONG_MAX
); // long here (and not int...)
192 net_status_t
SendStartToMaster();
203 void EncodeSyncPacket();
204 void DecodeSyncPacket();
206 int Recv(size_t size
, int flags
);
207 int Send(size_t size
, int flags
);
209 void FatalRecvError();
210 void FatalSendError();
216 JackNetSlaveInterface() : JackNetInterface()
221 JackNetSlaveInterface(const char* ip
, int port
) : JackNetInterface(ip
, port
)
226 virtual ~JackNetSlaveInterface()
228 // close Socket API with the last slave
229 if (--fSlaveCounter
== 0) {