libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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