In JackCoreAudioDriver, improve management of input/output channels: -1 is now used...
[jack2.git] / common / JackNetInterface.h
blobca90875f80fe342a2b04540220d3422e9f2d3eb5
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2008 Romain Moret at Grame
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.
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.
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.
21 #ifndef __JackNetInterface__
22 #define __JackNetInterface__
24 #include "JackNetTool.h"
26 namespace Jack
28 /**
29 \Brief This class describes the basic Net Interface, used by both master and slave
32 class SERVER_EXPORT JackNetInterface
34 protected:
35 session_params_t fParams;
36 JackNetSocket fSocket;
37 char fMulticastIP[32];
38 uint fNSubProcess;
40 //headers
41 packet_header_t fTxHeader;
42 packet_header_t fRxHeader;
44 // transport
45 net_transport_data_t fSendTransportData;
46 net_transport_data_t fReturnTransportData;
48 //network buffers
49 char* fTxBuffer;
50 char* fRxBuffer;
51 char* fTxData;
52 char* fRxData;
54 //jack buffers
55 NetMidiBuffer* fNetMidiCaptureBuffer;
56 NetMidiBuffer* fNetMidiPlaybackBuffer;
57 NetAudioBuffer* fNetAudioCaptureBuffer;
58 NetAudioBuffer* fNetAudioPlaybackBuffer;
60 //sizes
61 int fAudioRxLen;
62 int fAudioTxLen;
63 int fPayloadSize;
65 //utility methods
66 void SetFramesPerPacket();
67 int SetNetBufferSize();
68 int GetNMidiPckt();
69 bool IsNextPacket();
71 //virtual methods : depends on the sub class master/slave
72 virtual void SetParams();
73 virtual bool Init() = 0;
75 //transport
76 virtual void EncodeTransportData() = 0;
77 virtual void DecodeTransportData() = 0;
79 //sync packet
80 virtual void EncodeSyncPacket() = 0;
81 virtual void DecodeSyncPacket() = 0;
83 virtual int SyncRecv() = 0;
84 virtual int SyncSend() = 0;
85 virtual int DataRecv() = 0;
86 virtual int DataSend() = 0;
88 virtual int Send ( size_t size, int flags ) = 0;
89 virtual int Recv ( size_t size, int flags ) = 0;
91 JackNetInterface();
92 JackNetInterface ( const char* multicast_ip, int port );
93 JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
95 public:
96 virtual ~JackNetInterface();
99 /**
100 \Brief This class describes the Net Interface for masters (NetMaster)
103 class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
105 protected:
106 bool fRunning;
107 int fCycleOffset;
109 bool Init();
110 int SetRxTimeout();
111 void SetParams();
113 void Exit();
115 int SyncRecv();
116 int SyncSend();
118 int DataRecv();
119 int DataSend();
121 //sync packet
122 void EncodeSyncPacket();
123 void DecodeSyncPacket();
125 int Send ( size_t size, int flags );
126 int Recv ( size_t size, int flags );
128 bool IsSynched();
130 public:
131 JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0)
133 JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
134 : JackNetInterface ( params, socket, multicast_ip )
136 ~JackNetMasterInterface()
141 \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
144 class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
146 protected:
148 static uint fSlaveCounter;
150 bool Init();
151 bool InitConnection();
152 bool InitRendering();
154 net_status_t SendAvailableToMaster();
155 net_status_t SendStartToMaster();
157 void SetParams();
159 int SyncRecv();
160 int SyncSend();
162 int DataRecv();
163 int DataSend();
165 //sync packet
166 void EncodeSyncPacket();
167 void DecodeSyncPacket();
169 int Recv ( size_t size, int flags );
170 int Send ( size_t size, int flags );
172 public:
173 JackNetSlaveInterface() : JackNetInterface()
175 //open Socket API with the first slave
176 if ( fSlaveCounter++ == 0 )
178 if ( SocketAPIInit() < 0 )
180 jack_error ( "Can't init Socket API, exiting..." );
181 throw -1;
185 JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
187 //open Socket API with the first slave
188 if ( fSlaveCounter++ == 0 )
190 if ( SocketAPIInit() < 0 )
192 jack_error ( "Can't init Socket API, exiting..." );
193 throw -1;
197 ~JackNetSlaveInterface()
199 //close Socket API with the last slave
200 if ( --fSlaveCounter == 0 )
201 SocketAPIEnd();
206 #define DEFAULT_MULTICAST_IP "225.3.19.154"
207 #define DEFAULT_PORT 19000
208 #define DEFAULT_MTU 1500
210 #define SLAVE_SETUP_RETRY 5
212 #define MASTER_INIT_TIMEOUT 1000000 // in usec
213 #define SLAVE_INIT_TIMEOUT 2000000 // in usec
215 #define MAX_LATENCY 6
217 #endif