Jack2 1.9.6

JackNetInterface.h

00001 /*
00002 Copyright (C) 2001 Paul Davis
00003 Copyright (C) 2008 Romain Moret at Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #ifndef __JackNetInterface__
00022 #define __JackNetInterface__
00023 
00024 #include "JackNetTool.h"
00025 
00026 namespace Jack
00027 {
00032     class SERVER_EXPORT JackNetInterface
00033     {
00034         protected:
00035             session_params_t fParams;
00036             JackNetSocket fSocket;
00037             char fMulticastIP[32];
00038             uint fNSubProcess;
00039 
00040             //headers
00041             packet_header_t fTxHeader;
00042             packet_header_t fRxHeader;
00043             
00044             // transport
00045             net_transport_data_t fSendTransportData;
00046             net_transport_data_t fReturnTransportData;
00047 
00048             //network buffers
00049             char* fTxBuffer;
00050             char* fRxBuffer;
00051             char* fTxData;
00052             char* fRxData;
00053 
00054             //jack buffers
00055             NetMidiBuffer* fNetMidiCaptureBuffer;
00056             NetMidiBuffer* fNetMidiPlaybackBuffer;
00057             NetAudioBuffer* fNetAudioCaptureBuffer;
00058             NetAudioBuffer* fNetAudioPlaybackBuffer;
00059 
00060             //sizes
00061             int fAudioRxLen;
00062             int fAudioTxLen;
00063             int fPayloadSize;
00064 
00065             //utility methods
00066             void SetFramesPerPacket();
00067             int SetNetBufferSize();
00068             int GetNMidiPckt();
00069             bool IsNextPacket();
00070 
00071             //virtual methods : depends on the sub class master/slave
00072             virtual void SetParams();
00073             virtual bool Init() = 0;
00074 
00075             //transport
00076             virtual void EncodeTransportData() = 0;
00077             virtual void DecodeTransportData() = 0;
00078 
00079             //sync packet
00080             virtual void EncodeSyncPacket() = 0;
00081             virtual void DecodeSyncPacket() = 0;
00082 
00083             virtual int SyncRecv() = 0;
00084             virtual int SyncSend() = 0;
00085             virtual int DataRecv() = 0;
00086             virtual int DataSend() = 0;
00087 
00088             virtual int Send ( size_t size, int flags ) = 0;
00089             virtual int Recv ( size_t size, int flags ) = 0;
00090 
00091             JackNetInterface();
00092             JackNetInterface ( const char* multicast_ip, int port );
00093             JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
00094 
00095         public:
00096             virtual ~JackNetInterface();
00097     };
00098 
00103     class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
00104     {
00105         protected:
00106             bool fRunning;
00107             int fCycleOffset;
00108 
00109             bool Init();
00110             int SetRxTimeout();
00111             void SetParams();
00112             
00113             void Exit();
00114             
00115             int SyncRecv();
00116             int SyncSend();
00117             
00118             int DataRecv();
00119             int DataSend();
00120             
00121              //sync packet
00122             void EncodeSyncPacket();
00123             void DecodeSyncPacket();
00124 
00125             int Send ( size_t size, int flags );
00126             int Recv ( size_t size, int flags );
00127             
00128             bool IsSynched();
00129 
00130         public:
00131             JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0)
00132             {}
00133             JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
00134                     : JackNetInterface ( params, socket, multicast_ip )
00135             {}
00136             ~JackNetMasterInterface()
00137             {}
00138     };
00139 
00144     class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
00145     {
00146         protected:
00147         
00148             static uint fSlaveCounter;
00149        
00150             bool Init();
00151             bool InitConnection();
00152             bool InitRendering();
00153             
00154             net_status_t SendAvailableToMaster();
00155             net_status_t SendStartToMaster();
00156             
00157             void SetParams();
00158             
00159             int SyncRecv();
00160             int SyncSend();
00161             
00162             int DataRecv();
00163             int DataSend();
00164             
00165             //sync packet
00166             void EncodeSyncPacket();
00167             void DecodeSyncPacket();
00168 
00169             int Recv ( size_t size, int flags );
00170             int Send ( size_t size, int flags );
00171 
00172         public:
00173             JackNetSlaveInterface() : JackNetInterface()
00174             {
00175                 //open Socket API with the first slave
00176                 if ( fSlaveCounter++ == 0 )
00177                 {
00178                     if ( SocketAPIInit() < 0 )
00179                     {
00180                         jack_error ( "Can't init Socket API, exiting..." );
00181                         throw -1;
00182                     }
00183                 }
00184             }
00185             JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
00186             {
00187                 //open Socket API with the first slave
00188                 if ( fSlaveCounter++ == 0 )
00189                 {
00190                     if ( SocketAPIInit() < 0 )
00191                     {
00192                         jack_error ( "Can't init Socket API, exiting..." );
00193                         throw -1;
00194                     }
00195                 }
00196             }
00197             ~JackNetSlaveInterface()
00198             {
00199                 //close Socket API with the last slave
00200                 if ( --fSlaveCounter == 0 )
00201                     SocketAPIEnd();
00202             }
00203     };
00204 }
00205 
00206 #define DEFAULT_MULTICAST_IP "225.3.19.154"
00207 #define DEFAULT_PORT 19000
00208 #define DEFAULT_MTU 1500
00209 
00210 #define SLAVE_SETUP_RETRY 5
00211 
00212 #define MASTER_INIT_TIMEOUT 1000000     // in usec
00213 #define SLAVE_INIT_TIMEOUT 2000000      // in usec
00214 
00215 #define MAX_LATENCY 6
00216 
00217 #endif