#ifndef __SOCKUTIL_H__ #define __SOCKUTIL_H__ #pragma once #include "config.h" #include #include #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] = #if defined(_MSC_VER) { {(ULONG)n1, (char*)buf1}, {(ULONG)n2, (char*)buf2}, }; #else { { (char*)buf1, (ULONG)n1 }, { (char*)buf2, (ULONG)n2 }, }; #endif //_MSC_VER 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] = #if defined(_MSC_VER) { {(ULONG)n1, (char*)buf1}, {(ULONG)n2, (char*)buf2}, {(ULONG)n3, (char*)buf3}, }; #else { { (char*)buf1, (ULONG)n1 }, { (char*)buf2, (ULONG)n2 }, { (char*)buf3, (ULONG)n3 }, }; #endif //_MSC_VER 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] = #if defined(_MSC_VER) { {(ULONG)n1, (char*)buf1}, {(ULONG)n2, (char*)buf2}, }; #else { { (char*)buf1, (ULONG)n1 }, { (char*)buf2, (ULONG)n2 }, }; #endif 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] = #if defined(_MSC_VER) { {(ULONG)n1, (char*)buf1}, {(ULONG)n2, (char*)buf2}, {(ULONG)n3, (char*)buf3}, }; #else { { (char*)buf1, (ULONG)n1 }, { (char*)buf2, (ULONG)n2 }, { (char*)buf3, (ULONG)n3 }, }; #endif //_MSC_VER 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__