libcm is a C development framework with an emphasis on audio signal processing applications.
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.

cmRtNet.h 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef cmRtNet_h
  2. #define cmRtNet_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. 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, // 0 broadcast msg (label=node label, id=endpt cnt)
  44. kNodeSelNetId, // 1 define remote node (label=remote node label, id=endpt cnt)
  45. kEndpointSelNetId, // 2 define remote endpt (label=remote endpt label, id=endpt id)
  46. kDoneSelNetId, // 3 declare all endpts sent
  47. kInvalidSelNetId // 4
  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 hdrByteCnt; // size of the header record at transmission (used to locate the serialzed label)
  56. unsigned rtSubIdx; // cmInvalidIdx or rtSubIdx
  57. unsigned id; // endptCnt or endpoint id
  58. const cmChar_t* label; // node or endpoint label
  59. } cmRtNetSyncMsg_t;
  60. const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m );
  61. // NOTE: Messages passed between cmRtNet nodes during the synchronization
  62. // process use the cmRtNetSyncMsg_t format (w/ the body of label following
  63. // the record. All other messages use cmRtNetMsg_t (cmRtSysMsg.h) format.
  64. // 'cbFunc' will be called within the context of cmRtNetReceive() to receive
  65. // incoming network messages.
  66. // rtSubIdx is the rtSubIdx of the cmRtSys which owns this cmRtNet.
  67. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg );
  68. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp );
  69. bool cmRtNetIsValid( cmRtNetH_t h );
  70. // Get the local host name for this machine. This function
  71. // is synonomous with gethostname().
  72. const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h );
  73. // Initialize the local network node.
  74. // 'bcastAddr' is the network broadcast address (e.g. 192.168.15.255).
  75. // 'nodeLabel' is the local network node label
  76. // 'ipAddr' may be set to NULL to use any available IP address.
  77. // 'ipPort' refers to the socket port (which may need to be made available
  78. // by the machine firewall cfg.)
  79. cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
  80. bool cmRtNetIsInitialized( cmRtNetH_t h );
  81. // Register the local endpoints.
  82. // Endpoints may only be registered once the network is initialized via
  83. // cmRtNetInitialize().
  84. // Remote nodes will be able to send messages to these endpoints by
  85. // referring to (nodeLabel/endPtLabel)
  86. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
  87. // Delete all nodes and endpoints.
  88. cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h );
  89. // Broadcast the 'hello' to all machines listening on the
  90. // broadcast addresss. This starts the synchronization sequence
  91. cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h );
  92. // This function must be polled to receive incoming network messages
  93. // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc().
  94. // Note that all messages received via 'cbFunc' will be prefixed with
  95. // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
  96. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
  97. // Get a remote end point handle for use with cmRtNetSend.
  98. cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
  99. bool cmRtNetEndpointIsValid( cmRtNetEndptH_t endPtH );
  100. // Given an endpoint handle return the id/label of the associated endpoint.
  101. unsigned cmRtNetEndpointId( cmRtNetEndptH_t endPtH );
  102. const cmChar_t* cmRtNetEndpointLabel( cmRtNetEndptH_t endPtH );
  103. // Send a message to a remote endpoint.
  104. // Note that srcEndPtId is used only to inform the receiver of the endpoint
  105. // of the transmitter. It is not used in any part of the transmit or receive
  106. // process.
  107. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned srcEndPtId, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
  108. // Send a message to a remote endpoint. This function is a composite
  109. // of cmRtNetEndpointHandle() and cmRtNetSend().
  110. cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, unsigned srcEndPtId, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
  111. cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned srcEndPtId, unsigned dstNodeIdx, unsigned dstEndptIdx, const void* msg, unsigned msgByteCnt );
  112. // Enable/disable synchronization protocol reporting.
  113. // Return the previous state of the report sync. flag.
  114. bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl );
  115. bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h );
  116. // Query network configuration. Returns true on success or false if
  117. // {nodeIdx, epIdx} does not identify a valid endpoint.
  118. const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h );
  119. unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h );
  120. unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
  121. unsigned cmRtNetRemoteNodeIndex( cmRtNetH_t h, const cmChar_t* label );
  122. const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx );
  123. unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx );
  124. cmRtNetRC_t cmRtNetRemoteNodeEndPoint(
  125. cmRtNetH_t h,
  126. unsigned nodeIdx,
  127. unsigned epIdx,
  128. const cmChar_t** labelRef,
  129. unsigned* idRef,
  130. unsigned* rsiRef );
  131. void cmRtNetReport( cmRtNetH_t h );
  132. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
  133. /*
  134. Synchronization Protocol:
  135. Machine A Machine B
  136. ================================== ====================================
  137. broadcast 'hello' ------------------=-> create node-A w/ ei=0 -------+
  138. |
  139. +<-- create node-B w/ ei=0 <--------=-- send 'node' <----------------+
  140. |
  141. +--> switch(ei,m_t)
  142. | ei < en : send endpt[ei++] -=--> create endpt[] on node-A -->+
  143. | |
  144. | ei == en : ++ei,send 'done' -=------------------------------->+ |
  145. | |
  146. | m_t!='done': send 'done' -=------------------------------->+ |
  147. | |
  148. | (stop) : |
  149. | |
  150. | v
  151. | switch(ei,m_t)
  152. +<-- create endpt[] on node-B <---=----- send endpt[ei++] : ei < en
  153. |
  154. +<---------------------------------=----- send 'done',++ei : ei == en
  155. |
  156. +<---------------------------------=----- send 'done' : m_t!= 'done'
  157. : (stop)
  158. Notes:
  159. 1) 'ei' is the index of the next local end point to transmit.
  160. 2) 'en' is the count of local endpoints.
  161. 3) 'm_t' is the msg type (i.e.'hello','node','endpoint','done')
  162. of the incoming message.
  163. 4) The symbol -=- in the flow chart implies a network transmission.
  164. */
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif