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.

194 lines
5.6KB

  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. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "JackNetTool.h"
  22. namespace Jack
  23. {
  24. /**
  25. \Brief This class describes the basic Net Interface, used by both master and slave
  26. */
  27. class SERVER_EXPORT JackNetInterface
  28. {
  29. protected:
  30. session_params_t fParams;
  31. JackNetSocket fSocket;
  32. char* fMulticastIP;
  33. uint fNSubProcess;
  34. //headers
  35. packet_header_t fTxHeader;
  36. packet_header_t fRxHeader;
  37. //network buffers
  38. char* fTxBuffer;
  39. char* fRxBuffer;
  40. char* fTxData;
  41. char* fRxData;
  42. //jack buffers
  43. NetMidiBuffer* fNetMidiCaptureBuffer;
  44. NetMidiBuffer* fNetMidiPlaybackBuffer;
  45. NetAudioBuffer* fNetAudioCaptureBuffer;
  46. NetAudioBuffer* fNetAudioPlaybackBuffer;
  47. //sizes
  48. int fAudioRxLen;
  49. int fAudioTxLen;
  50. int fPayloadSize;
  51. //utility methods
  52. jack_nframes_t SetFramesPerPacket();
  53. int SetNetBufferSize();
  54. int GetNMidiPckt();
  55. bool IsNextPacket();
  56. //virtual methods : depends on the sub class master/slave
  57. virtual void SetParams();
  58. virtual bool Init() = 0;
  59. //transport
  60. virtual int EncodeTransportData() = 0;
  61. virtual int DecodeTransportData() = 0;
  62. //sync packet
  63. virtual int EncodeSyncPacket() = 0;
  64. virtual int DecodeSyncPacket() = 0;
  65. virtual int SyncRecv() = 0;
  66. virtual int SyncSend() = 0;
  67. virtual int DataRecv() = 0;
  68. virtual int DataSend() = 0;
  69. virtual int Send ( size_t size, int flags ) = 0;
  70. virtual int Recv ( size_t size, int flags ) = 0;
  71. JackNetInterface();
  72. JackNetInterface ( const char* multicast_ip, int port );
  73. JackNetInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip );
  74. public:
  75. virtual ~JackNetInterface();
  76. };
  77. /**
  78. \Brief This class describes the Net Interface for masters (NetMaster)
  79. */
  80. class SERVER_EXPORT JackNetMasterInterface : public JackNetInterface
  81. {
  82. protected:
  83. bool fRunning;
  84. bool Init();
  85. int SetRxTimeout();
  86. void SetParams();
  87. void Exit();
  88. int SyncRecv();
  89. int SyncSend();
  90. int DataRecv();
  91. int DataSend();
  92. int Send ( size_t size, int flags );
  93. int Recv ( size_t size, int flags );
  94. public:
  95. JackNetMasterInterface() : JackNetInterface(), fRunning ( false )
  96. {}
  97. JackNetMasterInterface ( session_params_t& params, JackNetSocket& socket, const char* multicast_ip )
  98. : JackNetInterface ( params, socket, multicast_ip )
  99. {}
  100. ~JackNetMasterInterface()
  101. {}
  102. };
  103. /**
  104. \Brief This class describes the Net Interface for slaves (NetDriver and NetAdapter)
  105. */
  106. class SERVER_EXPORT JackNetSlaveInterface : public JackNetInterface
  107. {
  108. protected:
  109. static uint fSlaveCounter;
  110. bool Init();
  111. net_status_t GetNetMaster();
  112. net_status_t SendStartToMaster();
  113. void SetParams();
  114. int SyncRecv();
  115. int SyncSend();
  116. int DataRecv();
  117. int DataSend();
  118. int Recv ( size_t size, int flags );
  119. int Send ( size_t size, int flags );
  120. public:
  121. JackNetSlaveInterface() : JackNetInterface()
  122. {
  123. //open Socket API with the first slave
  124. if ( fSlaveCounter++ == 0 )
  125. {
  126. if ( SocketAPIInit() < 0 )
  127. {
  128. jack_error ( "Can't init Socket API, exiting..." );
  129. throw -1;
  130. }
  131. }
  132. }
  133. JackNetSlaveInterface ( const char* ip, int port ) : JackNetInterface ( ip, port )
  134. {
  135. //open Socket API with the first slave
  136. if ( fSlaveCounter++ == 0 )
  137. {
  138. if ( SocketAPIInit() < 0 )
  139. {
  140. jack_error ( "Can't init Socket API, exiting..." );
  141. throw -1;
  142. }
  143. }
  144. }
  145. ~JackNetSlaveInterface()
  146. {
  147. //close Socket API with the last slave
  148. if ( --fSlaveCounter == 0 )
  149. SocketAPIEnd();
  150. }
  151. };
  152. }
  153. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  154. #define DEFAULT_PORT 19000
  155. #define SLAVE_SETUP_RETRY 5
  156. #define MASTER_INIT_TIMEOUT 1000000 // in usec
  157. #define SLAVE_INIT_TIMEOUT 2000000 // in usec
  158. #endif