jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.7KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2008 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.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifndef __JackNetInterface__
  17. #define __JackNetInterface__
  18. #include "JackNetTool.h"
  19. namespace Jack
  20. {
  21. /**
  22. \Brief This class describes the basic Net Interface, used by both master and slave
  23. */
  24. class SERVER_EXPORT JackNetInterface
  25. {
  26. protected:
  27. session_params_t fParams;
  28. JackNetSocket fSocket;
  29. char* fMulticastIP;
  30. uint fNSubProcess;
  31. //headers
  32. packet_header_t fTxHeader;
  33. packet_header_t fRxHeader;
  34. //network buffers
  35. char* fTxBuffer;
  36. char* fRxBuffer;
  37. char* fTxData;
  38. char* fRxData;
  39. //jack buffers
  40. NetMidiBuffer* fNetMidiCaptureBuffer;
  41. NetMidiBuffer* fNetMidiPlaybackBuffer;
  42. NetAudioBuffer* fNetAudioCaptureBuffer;
  43. NetAudioBuffer* fNetAudioPlaybackBuffer;
  44. //sizes
  45. int fAudioRxLen;
  46. int fAudioTxLen;
  47. int fPayloadSize;
  48. //utility methods
  49. void SetFramesPerPacket();
  50. int SetNetBufferSize();
  51. int GetNMidiPckt();
  52. bool IsNextPacket();
  53. //virtual methods : depends on the sub class master/slave
  54. virtual void SetParams();
  55. virtual bool Init() = 0;
  56. //transport
  57. virtual int EncodeTransportData() = 0;
  58. virtual int DecodeTransportData() = 0;
  59. //sync packet
  60. virtual int EncodeSyncPacket() = 0;
  61. virtual int DecodeSyncPacket() = 0;
  62. virtual int SyncRecv() = 0;
  63. virtual int SyncSend() = 0;
  64. virtual int DataRecv() = 0;
  65. virtual int DataSend() = 0;
  66. virtual int Send ( size_t size, int flags ) = 0;
  67. virtual int Recv ( size_t size, int flags ) = 0;
  68. JackNetInterface();
  69. JackNetInterface ( const char* multicast_ip, int port );
  70. JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
  71. public:
  72. virtual ~JackNetInterface();
  73. };
  74. /**
  75. \Brief This class describes the Net Interface for masters (NetMaster)
  76. */
  77. class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
  78. {
  79. protected:
  80. bool fRunning;
  81. int fCycleOffset;
  82. bool Init();
  83. int SetRxTimeout();
  84. void SetParams();
  85. void Exit();
  86. int SyncRecv();
  87. int SyncSend();
  88. int DataRecv();
  89. int DataSend();
  90. int Send ( size_t size, int flags );
  91. int Recv ( size_t size, int flags );
  92. bool IsSynched();
  93. public:
  94. JackNetMasterInterface() : JackNetInterface(), fRunning(false), fCycleOffset(0)
  95. {}
  96. JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
  97. : JackNetInterface ( params, socket, multicast_ip )
  98. {}
  99. ~JackNetMasterInterface()
  100. {}
  101. };
  102. /**
  103. \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
  104. */
  105. class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
  106. {
  107. protected:
  108. static uint fSlaveCounter;
  109. bool Init();
  110. net_status_t GetNetMaster();
  111. net_status_t SendStartToMaster();
  112. void SetParams();
  113. int SyncRecv();
  114. int SyncSend();
  115. int DataRecv();
  116. int DataSend();
  117. int Recv ( size_t size, int flags );
  118. int Send ( size_t size, int flags );
  119. public:
  120. JackNetSlaveInterface() : JackNetInterface()
  121. {
  122. //open Socket API with the first slave
  123. if ( fSlaveCounter++ == 0 )
  124. {
  125. if ( SocketAPIInit() < 0 )
  126. {
  127. jack_error ( "Can't init Socket API, exiting..." );
  128. throw -1;
  129. }
  130. }
  131. }
  132. JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
  133. {
  134. //open Socket API with the first slave
  135. if ( fSlaveCounter++ == 0 )
  136. {
  137. if ( SocketAPIInit() < 0 )
  138. {
  139. jack_error ( "Can't init Socket API, exiting..." );
  140. throw -1;
  141. }
  142. }
  143. }
  144. ~JackNetSlaveInterface()
  145. {
  146. //close Socket API with the last slave
  147. if ( --fSlaveCounter == 0 )
  148. SocketAPIEnd();
  149. }
  150. };
  151. }
  152. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  153. #define DEFAULT_PORT 19000
  154. #define SLAVE_SETUP_RETRY 5
  155. #define MASTER_INIT_TIMEOUT 1000000 // in usec
  156. #define SLAVE_INIT_TIMEOUT 2000000 // in usec
  157. #define MAX_LATENCY 6
  158. #endif