libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmUdpPort.h 5.0KB

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