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.

cmUdpNet.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmUdpNet_h
  4. #define cmUdpNet_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /*( { file_desc:"UDP based network object." kw:[network] }
  9. A cmUdpNet is a wrapper around a single cmUdpPort. This object
  10. maintains an array of remote nodes which map application defined
  11. node label/id's to IP address/port. This allows the application
  12. to communicate in terms of its own id scheme without having to
  13. consider the IP addr/port of the remote nodes.
  14. */
  15. enum
  16. {
  17. kOkUnRC = cmOkRC,
  18. kDuplicateNodeLabelUnRC,
  19. kDuplicateNodeIdUnRC,
  20. kUdpPortFailUnRC,
  21. kInvalidNodeLabelUnRC,
  22. kNodeNotFoundUnRC,
  23. kInvalidNodeAddrUnRC,
  24. kSendFailUnRC,
  25. kGetDataFailUnRC,
  26. kJsonFailUnRC
  27. };
  28. typedef cmRC_t cmUnRC_t;
  29. typedef cmHandle_t cmUdpNetH_t;
  30. extern cmUdpNetH_t cmUdpNetNullHandle;
  31. typedef void (*cmUdpNetCallback_t)( void* cbArg, cmUdpNetH_t h, const char* data, unsigned dataByteCnt, unsigned remoteNodeId );
  32. // Allocate a UDP net manager. To use the manager one of the
  33. // initialization functions must be used configure it.
  34. cmUnRC_t cmUdpNetAlloc( cmCtx_t* ctx, cmUdpNetH_t* hp );
  35. // Allocate and initialize a UDP network manager from
  36. // a JSON script. This function is a simple wrapper for
  37. // calls to cmUdpNetAlloc(), cmUdpNetInitJson(), and
  38. // cmUdpNetEnableListen(h,listenFl).
  39. enum { kListenUnFl=0x01, kNetOptionalUnFl=0x02 };
  40. cmUnRC_t cmUdpNetAllocJson(
  41. cmCtx_t* ctx,
  42. cmUdpNetH_t* hp,
  43. cmJsonH_t jsH,
  44. cmUdpNetCallback_t cbFunc,
  45. void* cbArg,
  46. unsigned flags);
  47. // Release a UDP network manager and any resources it may hold.
  48. cmUnRC_t cmUdpNetFree( cmUdpNetH_t* hp );
  49. // Initialize a UDP net using a previously allocated handle
  50. // The node information (nodeLabel,nodeId,nodeSocketPort) refers
  51. // to the local node. The callback information (cbFunc,cbArg)
  52. // are used during cmUdpNetReceive() to receive incoming
  53. // information from the local node.
  54. cmUnRC_t cmUdpNetInit(
  55. cmUdpNetH_t h,
  56. const cmChar_t* nodeLabel,
  57. unsigned nodeId,
  58. cmUdpPort_t nodeSocketPort,
  59. cmUdpNetCallback_t cbFunc,
  60. void* cbArg,
  61. unsigned recvBufByteCnt,
  62. unsigned socketRecvTimeOutMs );
  63. // Initialize a UDP net and register remote nodes using a JSON resource.
  64. cmUnRC_t cmUdpNetInitJson(
  65. cmUdpNetH_t h,
  66. cmJsonH_t jsH,
  67. cmUdpNetCallback_t cbFunc,
  68. void* cbArg );
  69. // Return true if the if the network has been initialized.
  70. bool cmUdpNetIsInitialized( cmUdpNetH_t h );
  71. // Finalize a UDP net. This releases any resources allocated
  72. // via one of the above 'init' functions.
  73. cmUnRC_t cmUdpNetFinal( cmUdpNetH_t h );
  74. // Enable/disable the networks listening port. While in
  75. // 'listening' mode the network internally queue's all arriving
  76. // messages. Messages are then forwarded to the client via
  77. // calls to cmUdpNetReceive().
  78. cmUnRC_t cmUdpNetEnableListen( cmUdpNetH_t h, bool enableFl );
  79. // Return true if the handle is valid.
  80. bool cmUdpNetIsValid( cmUdpNetH_t h );
  81. unsigned cmUdpNetLocalNodeId( cmUdpNetH_t h );
  82. const cmChar_t* cmUdpNetLocalNodeLabel( cmUdpNetH_t h );
  83. // Return the node id associated with a node label or 'cmInvalidId' if the
  84. // label is not found.
  85. unsigned cmUdpNetNodeLabelToId( cmUdpNetH_t h, const cmChar_t* label );
  86. // Return the node label associated with a node id or NULL if the id
  87. // is not found.
  88. const cmChar_t* cmUdpNetNodeIdToLabel( cmUdpNetH_t h, unsigned id );
  89. // Get the total count of nodes on the network. This count includes the local node.
  90. unsigned cmUdpNetNodeCount( cmUdpNetH_t h );
  91. // Return the node id of each network node.
  92. unsigned cmUdpNetNodeId( cmUdpNetH_t h, unsigned nodeIdx );
  93. // Register a remote node.
  94. cmUnRC_t cmUdpNetRegisterRemote(
  95. cmUdpNetH_t h,
  96. const cmChar_t* remoteNodeLabel,
  97. unsigned remoteNodeId,
  98. const char* remoteNodeSockAddr,
  99. cmUdpPort_t remoteNodePort );
  100. // Send a message to a remote network node.
  101. cmUnRC_t cmUdpNetSendById( cmUdpNetH_t h, unsigned remoteNodeId, const void* data, unsigned dataByteCnt );
  102. cmUnRC_t cmUdpNetSendByLabel( cmUdpNetH_t h, const cmChar_t* remoteNodeLabel, const void* data, unsigned dataByteCnt );
  103. // Transmit any waiting incoming messages to the client via the
  104. // cmUdpNetCallback_t callback function provided
  105. // cmUdpNetInit().
  106. // On input *msgCntPtr should hold the max. number of
  107. // messages to receive or NULL to receive all available.
  108. // On return *msgCntPtr is set to the actual number of
  109. // messages received.
  110. cmUnRC_t cmUdpNetReceive( cmUdpNetH_t h, unsigned* msgCntPtr );
  111. cmUnRC_t cmUdpNetPrintNodes( cmUdpNetH_t h, cmRpt_t* rpt );
  112. void cmUdpNetReport( cmUdpNetH_t h, cmRpt_t* rpt );
  113. cmRC_t cmUdpNetTest( cmCtx_t* ctx, int argc, const char* argv[] );
  114. //)
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif