libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmRtNet.h 4.8KB

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