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.

cmUdpPort.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 cmUdpPort_h
  4. #define cmUdpPort_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"UDP socket interface class." kw:[network] }
  9. #include <netinet/in.h>
  10. enum
  11. {
  12. kOkUdpRC = cmOkRC,
  13. kSockCreateFailUdpRC,
  14. kSockCloseFailUdpRC,
  15. kSockBindFailUdpRC,
  16. kSockConnectFailUdpRC,
  17. kSockOptSetFailUdpRC,
  18. kSockSendFailUdpRC,
  19. kSockRecvFailUdpRC,
  20. kSockSelectFailUdpRC,
  21. kPtoNFailUdpRC,
  22. kNtoPFailUdpRC,
  23. kNotConnectedUdpRC,
  24. kThreadFailUdpRC,
  25. kQueueFailUdpRC,
  26. kRecvBufOverflowUdpRC,
  27. kBufTooSmallUdpRC,
  28. kHostNameFailUdpRC,
  29. kInvalidPortNumbUdpRC,
  30. kTimeOutUdpRC,
  31. kTestFailUdpRC
  32. };
  33. typedef cmRC_t cmUdpRC_t;
  34. typedef cmHandle_t cmUdpH_t;
  35. typedef unsigned short cmUdpPort_t;
  36. extern cmUdpH_t cmUdpNullHandle;
  37. typedef void (*cmUdpCallback_t)( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr );
  38. enum
  39. {
  40. kNonBlockingUdpFl = 0x00,
  41. kBlockingUdpFl = 0x01,
  42. kNoQueueUdpFl = 0x02,
  43. kBroadcastUdpFl = 0x04
  44. };
  45. enum
  46. {
  47. // port 0 is reserved by and is therefore a convenient invalid port number
  48. kInvalidUdpPortNumber = 0
  49. };
  50. cmUdpRC_t cmUdpAlloc( cmCtx_t* ctx, cmUdpH_t* hp );
  51. cmUdpRC_t cmUdpFree( cmUdpH_t* hp );
  52. cmUdpRC_t cmUdpInit(
  53. cmUdpH_t h,
  54. cmUdpPort_t port, // this sockets port
  55. unsigned flags, // see kXXXUdpFl
  56. cmUdpCallback_t cbFunc, // Callback for use w/ cmUdpGetAvailData()
  57. void* cbArg, // First arg to cbFunc().
  58. const char* remoteAddr, // Remote addr to bind this socket to (or NULL).
  59. cmUdpPort_t remotePort, // Remote port to use with remoteAddr.
  60. unsigned recvBufByteCnt, // Size of the internal receive buffer in bytes. Size of the internal queue and msg receive buffer. No single msg can exceed this size.
  61. unsigned timeOutMs ); // Receive time-out in milliseconds
  62. cmUdpRC_t cmUdpFinal( cmUdpH_t h );
  63. bool cmUdpIsValid( cmUdpH_t h );
  64. // This function may not return a useful value until the
  65. // socket has gone into 'listen' mode.
  66. const struct sockaddr_in* cmUdpLocalAddr( cmUdpH_t h );
  67. // Set a destination address for this socket. Once a destination address is set
  68. // the caller may use cmUdpSend() to communicate with the specified remote socket
  69. // without having to specify an destination address on each call.
  70. cmUdpRC_t cmUdpConnect( cmUdpH_t h, const char* remoteAddr, cmUdpPort_t remotePort );
  71. // Send a message to a remote UDP socket. Use the function cmUdpInitAddr() to setup
  72. // the 'sockaddr_in' arg. for cmUdpSendTo().
  73. cmUdpRC_t cmUdpSend( cmUdpH_t h, const char* data, unsigned dataByteCnt );
  74. cmUdpRC_t cmUdpSendTo( cmUdpH_t h, const char* data, unsigned dataByteCnt, const struct sockaddr_in* remoteAddr );
  75. cmUdpRC_t cmUdpSend2( cmUdpH_t h, const char* data, unsigned dataByteCnt, const char* remoteAddr, cmUdpPort_t remotePort );
  76. // Receive incoming messages by directly checking the internal
  77. // socket for waiting data. This function is used to receive
  78. // incoming data when the internal listening thread is not used.
  79. // Note that if kBlockingUdpFl was set
  80. // in cmUdpInit() that this call will block for available data
  81. // or for 'timeOutMs' milliseconds, whichever comes first.
  82. // If kNonBlockingUdpFl was set in cmUdpInit() then the function
  83. // will return immediately if no incoming messages are waiting.
  84. // If non-NULL *recvByteCntPtr is set to the length of the received
  85. // message or 0 if no msg was received.
  86. cmUdpRC_t cmUdpRecv( cmUdpH_t h, char* data, unsigned dataByteCnt, struct sockaddr_in* fromAddr, unsigned* recvByteCntPtr );
  87. // Start a listening thread. If the queue is enabled then incoming
  88. // messages are received as they arrive and stored in an internal
  89. // queue until the client requests them using cmUdpGetAvailData().
  90. // If the queue is disabled the messages are transmitted immediately
  91. // to the client in the context of the internal listening thread.
  92. cmUdpRC_t cmUdpEnableListen( cmUdpH_t h, bool enableFl );
  93. // Enable/disable the internal queue. If the queue is disabled then
  94. // the receive callback function will be called immediately upon reception
  95. // of the incoming message in the context of the internal listening thread.
  96. // If the queue is enabled then incoming
  97. // messages will be queued until they are transmitted by calling
  98. // cmUdpGetAvailData().
  99. bool cmUdpIsQueueEnabled( cmUdpH_t h );
  100. void cmUdpQueueEnable( cmUdpH_t h, bool enableFl );
  101. // Return the size of the next available message waiting in the
  102. // internal data queue.
  103. unsigned cmUdpAvailDataByteCount( cmUdpH_t h );
  104. // The Call this function to receieve any data waiting in the internal queue.
  105. // Set 'data' to NULL to receive the data via the callback provided
  106. // in cmUdpAlloc().
  107. // On input *dataByteCntPtr must be set to the number of bytes in data[].
  108. // On return *dataByteCntPtr is set to the actual number of bytes copied into data[].
  109. // If fromAddr is non-NULL it is set to the data source address.
  110. cmUdpRC_t cmUdpGetAvailData( cmUdpH_t h, char* data, unsigned* dataByteCntPtr, struct sockaddr_in* fromAddr );
  111. void cmUdpReport( cmUdpH_t h, cmRpt_t* rpt );
  112. // Prepare a struct sockadddr_in for use with cmUdpSendTo()
  113. cmUdpRC_t cmUdpInitAddr( cmUdpH_t h, const char* addrStr, cmUdpPort_t portNumber, struct sockaddr_in* retAddrPtr );
  114. const cmChar_t* cmUdpAddrToString( cmUdpH_t h, const struct sockaddr_in* addr );
  115. bool cmUdpAddrIsEqual( const struct sockaddr_in* a0, const struct sockaddr_in* a1 );
  116. const cmChar_t* cmUdpHostName( cmUdpH_t h );
  117. cmUdpRC_t cmUdpTest( cmCtx_t* ctx, const char* remoteIpAddr, cmUdpPort_t port );
  118. cmUdpRC_t cmUdpTestV( cmCtx_t* ctx, unsigned argc, const char* argv[]);
  119. //)
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif