libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmRtNet.h 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef cmNet_h
  2. #define cmNet_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /*
  7. Nodes and Endpoints:
  8. ---------------------
  9. A node corresponds to a process and owns a socket. It also has a label which is
  10. unique among all other nodes on the network. A node also has a set of application
  11. defined 'endpoints'. Each endpoint has a label and id that is unique among all
  12. other endpoints on the same node. Endpoints on different nodes however may share
  13. use the same label and id. Endpoints are used by remote senders to identify
  14. a particular receiver which is sharing the node with other receivers. Endpoints
  15. are therefore analogous to port numbers on sockets.
  16. See gt/doc/notes.txt for more discussion of cmRtNet.
  17. */
  18. enum
  19. {
  20. kOkNetRC = cmOkRC,
  21. kUdpPortFailNetRC,
  22. kInvalidLabelNetRC,
  23. kDuplLabelNetRC,
  24. kDuplEndNetRC,
  25. kDuplLocalNetRC,
  26. kThreadFailNetRC,
  27. kBufToSmallNetRC,
  28. kNodeNotFoundNetRC,
  29. kEndNotFoundNetRC,
  30. kLocalNodeNetRC,
  31. kInvalidArgNetRC,
  32. kSyncFailNetRC,
  33. kNodeEndCntErrNetRC
  34. };
  35. typedef cmRC_t cmRtNetRC_t;
  36. typedef cmHandle_t cmRtNetH_t;
  37. typedef cmHandle_t cmRtNetEndptH_t;
  38. extern cmRtNetH_t cmRtNetNullHandle;
  39. extern cmRtNetEndptH_t cmRtNetEndptNullHandle;
  40. // selector id's for cmRtNetSyncMsg_t.selId.
  41. typedef enum
  42. {
  43. kHelloSelNetId, // broadcast msg (label=node label, id=endpt cnt)
  44. kNodeSelNetId, // define remote node (label=remote node label, id=endpt cnt)
  45. kEndpointSelNetId, // define remote endpt (label=remote endpt label, id=endpt id)
  46. kDoneSelNetId, // declare all endpts sent
  47. kInvalidSelNetId
  48. } cmRtNetSelId_t;
  49. // Network synchronization message format.
  50. // cmRtNetRC_t.hdr.selId == kNetSyncSelRtid.
  51. typedef struct
  52. {
  53. cmRtSysMsgHdr_t hdr; // standard cmRtSys msg header
  54. cmRtNetSelId_t selId; // message selector id (See kXXXSelNetId above)
  55. unsigned rtSubIdx; // cmInvalidIdx or rtSubIdx
  56. unsigned id; // endptCnt or endpoint id
  57. const cmChar_t* label; // node or endpoint label
  58. } cmRtNetSyncMsg_t;
  59. const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m );
  60. // NOTE: Messages passed between cmRtNet nodes during the synchronization
  61. // process use the cmRtNetSyncMsg_t format (w/ the body of label following
  62. // the record. All other messages use cmRtNetMsg_t (cmRtSysMsg.h) format.
  63. // 'cbFunc' will be called within the context of cmRtNetReceive() to receive
  64. // incoming network messages.
  65. // rtSubIdx is the rtSubIdx of the cmRtSys which owns this cmRtNet.
  66. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg );
  67. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp );
  68. bool cmRtNetIsValid( cmRtNetH_t h );
  69. // Get the local host name for this machine. This function
  70. // is synonomous with gethostname().
  71. const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h );
  72. // Initialize the local network node.
  73. // 'bcastAddr' is the network broadcast address (e.g. 192.168.15.255).
  74. // 'nodeLabel' is the local network node label
  75. // 'ipAddr' may be set to NULL to use any available IP address.
  76. // 'ipPort' refers to the socket port (which may need to be made available
  77. // by the machine firewall cfg.)
  78. cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
  79. bool cmRtNetIsInitialized( cmRtNetH_t h );
  80. // Register the local endpoints.
  81. // Endpoints may only be registered once the network is initialized via
  82. // cmRtNetInitialize().
  83. // Remote nodes will be able to send messages to these endpoints by
  84. // referring to (nodeLabel/endPtLabel)
  85. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
  86. // Delete all nodes and endpoints.
  87. cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h );
  88. // Broadcast the 'hello' to all machines listening on the
  89. // broadcast addresss. This starts the synchronization sequence
  90. cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h );
  91. // This function must be polled to receive incoming network messages
  92. // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc().
  93. // Note that all messages received via 'cbFunc' will be prefixed with
  94. // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
  95. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
  96. // Get a remote end point handle for use with cmRtNetSend.
  97. cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
  98. // Send a message to a remote endpoint.
  99. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
  100. // Send a message to a remote endpoint. This function is a composite
  101. // of cmRtNetEndpointHandle() and cmRtNetSend().
  102. cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
  103. cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt );
  104. // Enable/disable synchronization protocol reporting.
  105. // Return the previous state of the report sync. flag.
  106. bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl );
  107. bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h );
  108. // Query network configuration. Returns true on success or false if
  109. // {nodeIdx, epIdx} does not identify a valid endpoint.
  110. const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h );
  111. unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h );
  112. const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx );
  113. unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx );
  114. cmRtNetRC_t cmRtNetRemoteNodeEndPoint(
  115. cmRtNetH_t h,
  116. unsigned nodeIdx,
  117. unsigned epIdx,
  118. const cmChar_t** labelRef,
  119. unsigned* idRef,
  120. unsigned* rsiRef );
  121. void cmRtNetReport( cmRtNetH_t h );
  122. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
  123. /*
  124. Synchronization Protocol:
  125. Machine A Machine B
  126. ================================== ====================================
  127. broadcast 'hello' ------------------=-> create node-A w/ ei=0 -------+
  128. |
  129. +<-- create node-B w/ ei=0 <--------=-- send 'node' <----------------+
  130. |
  131. +--> switch(ei,m_t)
  132. | ei < en : send endpt[ei++] -=--> create endpt[] on node-A -->+
  133. | |
  134. | ei == en : ++ei,send 'done' -=------------------------------->+ |
  135. | |
  136. | m_t!='done': send 'done' -=------------------------------->+ |
  137. | |
  138. | (stop) : |
  139. | |
  140. | v
  141. | switch(ei,m_t)
  142. +<-- create endpt[] on node-B <---=----- send endpt[ei++] : ei < en
  143. |
  144. +<---------------------------------=----- send 'done',++ei : ei == en
  145. |
  146. +<---------------------------------=----- send 'done' : m_t!= 'done'
  147. : (stop)
  148. Notes:
  149. 1) 'ei' is the index of the next local end point to transmit.
  150. 2) 'en' is the count of local endpoints.
  151. 3) 'm_t' is the msg type (i.e.'hello','node','endpoint','done')
  152. of the incoming message.
  153. 4) The symbol -=- in the flow chart implies a network transmission.
  154. */
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif