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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef cmNet_h
  2. #define cmNet_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. enum
  7. {
  8. kOkNetRC = cmOkRC,
  9. kUdpPortFailNetRC,
  10. kInvalidLabelNetRC,
  11. kDuplLabelNetRC,
  12. kDuplLocalNetRC,
  13. kDuplEndNetRC,
  14. kThreadFailNetRC,
  15. kBufToSmallNetRC,
  16. kNodeNotFoundNetRC,
  17. kNodeStateErrNetRC,
  18. kTimeOutErrNetRC,
  19. kLocalNodeNetRC,
  20. };
  21. typedef cmRC_t cmRtNetRC_t;
  22. typedef cmHandle_t cmRtNetH_t;
  23. extern cmRtNetH_t cmRtNetNullHandle;
  24. // 'cbFunc' will be called within the context of cmRtNetReceive() to receive
  25. // incoming network messages.
  26. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg );
  27. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp );
  28. bool cmRtNetIsValid( cmRtNetH_t h );
  29. const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h );
  30. // Create a network node.
  31. // The 'nodeLabel' refers to a network device cfg. (see cmDevCfg).
  32. // Set 'ipAddr' to NULL if this is the local node.
  33. // During sync mode this node will attempt to sync with all
  34. // nodes in the node list.
  35. cmRtNetRC_t cmRtNetCreateNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
  36. // Register the local endpoints.
  37. // Remote nodes will be able to send messages to these endpoints by
  38. // referring to (nodeLabel/endPtLabel)
  39. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
  40. // Delete all nodes and endpoints.
  41. cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h );
  42. // Go into 'sync' node.
  43. // When a node enters sync mode it systematically transmits all of it's
  44. // local endpoint information to each registered remote node. Prior to
  45. // entering sync mode a node must therefore have been setup with a list
  46. // of remote nodes (via cmRtNetCreateNode()) and a list of local endpoints
  47. // (cmRtNetRegisterEndpoint()). During sync mode a node sends it's local
  48. // endpoint list to each registered remote node. When a remote node receives
  49. // an endpoint it updates it's own remote node/endpoint
  50. // list.
  51. cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h );
  52. bool cmRtNetIsInSyncMode( cmRtNetH_t h );
  53. // When the network message recieve function (See cmRtNetAlloc() 'cbFunc')
  54. // receives a message with the cmRtSysMsgHdr_t.selId == kNetSyncSelRtId
  55. // it should call this function to update the current sync state of the
  56. // cmRtNet.
  57. cmRtNetRC_t cmRtNetSyncModeRecv( cmRtNetH_t h, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr );
  58. // When in the network is in sync mode (cmRtNetIsSync()==true)
  59. // the client system must poll this function to update the networks sync state.
  60. cmRtNetRC_t cmRtNetSyncModeSend( cmRtNetH_t h );
  61. // This function must be polled to receive incoming network messages
  62. // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc()
  63. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
  64. bool cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt );
  65. unsigned cmRtNetEndPointIndex( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endPtLabel );
  66. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned endPointIndex, const void* msg, unsigned msgByteCnt );
  67. void cmRtNetReport( cmRtNetH_t h );
  68. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
  69. /*
  70. Master:
  71. cmRtNetBeginSyncMode().
  72. while( cmRtNetIsSyncMode())
  73. {
  74. // Give the master an oppurtunity to advance it's sync mode state.
  75. // When the master is has sync'd with all remote nodes in it's
  76. // remote node list then it will automatically exit sync mode.
  77. cmRtNetSyncModeSend()
  78. }
  79. _myNetRecv(dataV,dataN,addr)
  80. {
  81. if( cmRtNetIsSyncModeMsg(dataV,dataN) )
  82. cmRtNetSyncModeRecv(dataV,dataN,addr)
  83. }
  84. The 'master' is the machine which cmRtNetBeginSyncMode() is called on.
  85. 1) 'master' sends local endpoints to all registered remote nodes.
  86. 2) When a 'slave' receives the kDoneSelNetId msg it transmits
  87. it's own local endpoints back to the master.
  88. a. Each node in the node list has a type id:
  89. 1. local
  90. 2. registered - remote node that was explicitely registered on a master
  91. 3. received - remote node that was received from a master
  92. b.
  93. 1. All nodes are created in the 'send-hello' state.
  94. 2. If a master machine is in 'sync-mode' then it systematically sends
  95. each of it's local endpoints to all 'registered' nodes.
  96. 3. When a slave machine recives a 'hello' it creates a
  97. 'received' node.
  98. 4. When a slave machine recieves a 'done' it enters sync mode
  99. and systematically sends each of its local endpoints to
  100. the 'done' source.
  101. Protocol:
  102. 1. A: broadcast - 'hello'
  103. 2. Bs: respond 'hello' ack
  104. 3. A: send local node and endpoints to each responder
  105. 4. A: send done
  106. 5. Bs: send local endpoints to A
  107. */
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif