sockutil.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef __SOCKUTIL_H__
  2. #define __SOCKUTIL_H__
  3. #pragma once
  4. #include "config.h"
  5. #include <winpr/wtypes.h>
  6. #include <winpr/winsock.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. TOOLKIT_API int winsock_init();
  11. TOOLKIT_API int winsock_term();
  12. TOOLKIT_API char* get_local_ip(char *buf, int n);
  13. // < 0 : error
  14. // < n : timeout
  15. // ==n : ok
  16. TOOLKIT_API int tsend_n(int fd, const char *buf, int n, long timeout/* = -1 */);
  17. TOOLKIT_API int trecv_n(int fd, char *buf, size_t n, long timeout/* = -1*/);
  18. // > 0 && header_len == 0, recv bytes, but not encounter delimer
  19. // > 0 && header_len > 0, ok
  20. // < 0 : error
  21. TOOLKIT_API int trecv_until(int fd, char *buf, int n, const char *delimer, int *header_len, long timeout);
  22. TOOLKIT_API int tsend_until(int fd, const char *buf, int n, const char *delimer, long timeout);
  23. TOOLKIT_API int tsend_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout);
  24. TOOLKIT_API int trecv_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout);
  25. static int tsend_n_v2(int fd,
  26. const char *buf1, int n1,
  27. const char *buf2, int n2,
  28. long timeout)
  29. {
  30. WSABUF wbuf[2] =
  31. #if defined(_MSC_VER)
  32. {
  33. {(ULONG)n1, (char*)buf1},
  34. {(ULONG)n2, (char*)buf2},
  35. };
  36. #else
  37. {
  38. { (char*)buf1, (ULONG)n1 },
  39. { (char*)buf2, (ULONG)n2 },
  40. };
  41. #endif //_MSC_VER
  42. return tsend_n_v(fd, &wbuf[0], 2, timeout);
  43. }
  44. static int tsend_n_v3(int fd,
  45. const char *buf1, int n1,
  46. const char *buf2, int n2,
  47. const char *buf3, int n3,
  48. long timeout)
  49. {
  50. WSABUF wbuf[3] =
  51. #if defined(_MSC_VER)
  52. {
  53. {(ULONG)n1, (char*)buf1},
  54. {(ULONG)n2, (char*)buf2},
  55. {(ULONG)n3, (char*)buf3},
  56. };
  57. #else
  58. {
  59. { (char*)buf1, (ULONG)n1 },
  60. { (char*)buf2, (ULONG)n2 },
  61. { (char*)buf3, (ULONG)n3 },
  62. };
  63. #endif //_MSC_VER
  64. return tsend_n_v(fd, &wbuf[0], 3, timeout);
  65. }
  66. static int trecv_n_v2(int fd,
  67. char *buf1, int n1,
  68. char *buf2, int n2,
  69. long timeout)
  70. {
  71. WSABUF wbuf[2] =
  72. #if defined(_MSC_VER)
  73. {
  74. {(ULONG)n1, (char*)buf1},
  75. {(ULONG)n2, (char*)buf2},
  76. };
  77. #else
  78. {
  79. { (char*)buf1, (ULONG)n1 },
  80. { (char*)buf2, (ULONG)n2 },
  81. };
  82. #endif
  83. return trecv_n_v(fd, &wbuf[0], 2, timeout);
  84. }
  85. static int trecv_n_v3(int fd,
  86. char *buf1, int n1,
  87. char *buf2, int n2,
  88. char *buf3, int n3,
  89. long timeout)
  90. {
  91. WSABUF wbuf[3] =
  92. #if defined(_MSC_VER)
  93. {
  94. {(ULONG)n1, (char*)buf1},
  95. {(ULONG)n2, (char*)buf2},
  96. {(ULONG)n3, (char*)buf3},
  97. };
  98. #else
  99. {
  100. { (char*)buf1, (ULONG)n1 },
  101. { (char*)buf2, (ULONG)n2 },
  102. { (char*)buf3, (ULONG)n3 },
  103. };
  104. #endif //_MSC_VER
  105. return trecv_n_v(fd, &wbuf[0], 3, timeout);
  106. }
  107. static int nonblock_sock(SOCKET sock)
  108. {
  109. unsigned long v = 1;
  110. return _ioctlsocket(sock, FIONBIO, &v);
  111. }
  112. static int reuse_addr(SOCKET sock)
  113. {
  114. BOOL reuseaddr = 1;
  115. return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr, sizeof(reuseaddr));
  116. }
  117. TOOLKIT_API int disable_udp_connreset(SOCKET sock);
  118. TOOLKIT_API int tsendto(int fd, const char *buf, int n, const struct sockaddr* to, int tolen, long timeout);
  119. TOOLKIT_API int trecvfrom(int fd, char *buf, int n, struct sockaddr* fromaddr, int *fromlen, long timeout);
  120. #ifdef __cplusplus
  121. } // extern "C" {
  122. #endif
  123. #endif //__SOCKUTIL_H__