libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmRtNet.h 8.9KB

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