123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #ifndef __SOCKUTIL_H__
- #define __SOCKUTIL_H__
- #pragma once
- #include "config.h"
- #include <winpr/wtypes.h>
- #include <winpr/winsock.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- TOOLKIT_API int winsock_init();
- TOOLKIT_API int winsock_term();
- TOOLKIT_API char* get_local_ip(char *buf, int n);
- // < 0 : error
- // < n : timeout
- // ==n : ok
- TOOLKIT_API int tsend_n(int fd, const char *buf, int n, long timeout/* = -1 */);
- TOOLKIT_API int trecv_n(int fd, char *buf, size_t n, long timeout/* = -1*/);
- // > 0 && header_len == 0, recv bytes, but not encounter delimer
- // > 0 && header_len > 0, ok
- // < 0 : error
- TOOLKIT_API int trecv_until(int fd, char *buf, int n, const char *delimer, int *header_len, long timeout);
- TOOLKIT_API int tsend_until(int fd, const char *buf, int n, const char *delimer, long timeout);
- TOOLKIT_API int tsend_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout);
- TOOLKIT_API int trecv_n_v(int fd, LPWSABUF lpBuffers, DWORD dwBufferCount, long timeout);
- static int tsend_n_v2(int fd,
- const char *buf1, int n1,
- const char *buf2, int n2,
- long timeout)
- {
- WSABUF wbuf[2] =
- {
- {(ULONG)n1, (char*)buf1},
- {(ULONG)n2, (char*)buf2},
- };
- return tsend_n_v(fd, &wbuf[0], 2, timeout);
- }
- static int tsend_n_v3(int fd,
- const char *buf1, int n1,
- const char *buf2, int n2,
- const char *buf3, int n3,
- long timeout)
- {
- WSABUF wbuf[3] =
- {
- {(ULONG)n1, (char*)buf1},
- {(ULONG)n2, (char*)buf2},
- {(ULONG)n3, (char*)buf3},
- };
- return tsend_n_v(fd, &wbuf[0], 3, timeout);
- }
- static int trecv_n_v2(int fd,
- char *buf1, int n1,
- char *buf2, int n2,
- long timeout)
- {
- WSABUF wbuf[2] =
- {
- {(ULONG)n1, (char*)buf1},
- {(ULONG)n2, (char*)buf2},
- };
- return trecv_n_v(fd, &wbuf[0], 2, timeout);
- }
- static int trecv_n_v3(int fd,
- char *buf1, int n1,
- char *buf2, int n2,
- char *buf3, int n3,
- long timeout)
- {
- WSABUF wbuf[3] =
- {
- {(ULONG)n1, (char*)buf1},
- {(ULONG)n2, (char*)buf2},
- {(ULONG)n3, (char*)buf3},
- };
- return trecv_n_v(fd, &wbuf[0], 3, timeout);
- }
- static int nonblock_sock(SOCKET sock)
- {
- unsigned long v = 1;
- return _ioctlsocket(sock, FIONBIO, &v);
- }
- static int reuse_addr(SOCKET sock)
- {
- BOOL reuseaddr = 1;
- return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr, sizeof(reuseaddr));
- }
- TOOLKIT_API int disable_udp_connreset(SOCKET sock);
- TOOLKIT_API int tsendto(int fd, const char *buf, int n, const struct sockaddr* to, int tolen, long timeout);
- TOOLKIT_API int trecvfrom(int fd, char *buf, int n, struct sockaddr* fromaddr, int *fromlen, long timeout);
- #ifdef __cplusplus
- } // extern "C" {
- #endif
- #endif //__SOCKUTIL_H__
|