Remove old Windows projects.
[jack2.git] / common / JackNetInterface.h
blobb20a4b899fa7c899f4ba53e022ee82bd44ee52c4
1 /*
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"
24 #include <limits.h>
26 namespace Jack
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
42 /**
43 \Brief This class describes the basic Net Interface, used by both master and slave.
46 class SERVER_EXPORT JackNetInterface
49 protected:
51 bool fSetTimeOut;
53 void Initialize();
55 session_params_t fParams;
56 JackNetSocket fSocket;
57 char fMulticastIP[32];
59 // headers
60 packet_header_t fTxHeader;
61 packet_header_t fRxHeader;
63 // transport
64 net_transport_data_t fSendTransportData;
65 net_transport_data_t fReturnTransportData;
67 // network buffers
68 char* fTxBuffer;
69 char* fRxBuffer;
70 char* fTxData;
71 char* fRxData;
73 // JACK buffers
74 NetMidiBuffer* fNetMidiCaptureBuffer;
75 NetMidiBuffer* fNetMidiPlaybackBuffer;
76 NetAudioBuffer* fNetAudioCaptureBuffer;
77 NetAudioBuffer* fNetAudioPlaybackBuffer;
79 // utility methods
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;
87 // transport
88 virtual void EncodeTransportData() = 0;
89 virtual void DecodeTransportData() = 0;
91 // sync packet
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);
118 public:
120 JackNetInterface();
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
135 protected:
137 bool fRunning;
138 int fCurrentCycleOffset;
139 int fMaxCycleOffset;
141 bool Init();
142 bool SetParams();
144 void Exit();
146 int SyncRecv();
147 int SyncSend();
149 int DataRecv();
150 int DataSend();
152 // sync packet
153 void EncodeSyncPacket();
154 void DecodeSyncPacket();
156 int Send(size_t size, int flags);
157 int Recv(size_t size, int flags);
159 bool IsSynched();
161 void FatalRecvError();
162 void FatalSendError();
164 public:
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
183 protected:
185 static uint fSlaveCounter;
187 bool Init();
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();
194 bool SetParams();
196 int SyncRecv();
197 int SyncSend();
199 int DataRecv();
200 int DataSend();
202 // sync packet
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();
212 void InitAPI();
214 public:
216 JackNetSlaveInterface() : JackNetInterface()
218 InitAPI();
221 JackNetSlaveInterface(const char* ip, int port) : JackNetInterface(ip, port)
223 InitAPI();
226 virtual ~JackNetSlaveInterface()
228 // close Socket API with the last slave
229 if (--fSlaveCounter == 0) {
230 SocketAPIEnd();
236 #endif