sockutil.h 3.4 KB

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