22 #pragma comment(lib, "ws2_32")
24 #include <ndb_global.h>
26 #define MY_SOCKET_FORMAT "%p"
27 #define MY_SOCKET_FORMAT_VALUE(x) (x.s)
29 typedef SOCKET ndb_native_socket_t;
32 static inline ndb_native_socket_t
40 return (s.s != INVALID_SOCKET);
52 my_socket_invalidate(&s);
63 return closesocket(s.s);
66 static inline int my_socket_errno()
68 return WSAGetLastError();
71 static inline void my_socket_set_errno(
int error)
73 WSASetLastError(error);
76 static inline ndb_socket_t my_socket_create(
int domain,
int type,
int protocol)
79 s.s= socket(domain, type, protocol);
86 int ret= recv(s.s, buf, (
int)len, flags);
87 if (ret == SOCKET_ERROR)
93 ssize_t my_send(
ndb_socket_t s,
const char* buf,
size_t len,
int flags)
95 int ret= send(s.s, buf, (
int)len, flags);
96 if (ret == SOCKET_ERROR)
101 static inline int my_socket_reuseaddr(
ndb_socket_t s,
int enable)
103 const int on = enable;
104 return setsockopt(s.s, SOL_SOCKET, SO_REUSEADDR,
105 (
const char*)&on,
sizeof(on));
108 static inline int my_socket_nonblock(
ndb_socket_t s,
int enable)
110 unsigned long ul = enable;
112 if(ioctlsocket(s.s, FIONBIO, &ul))
113 return my_socket_errno();
118 static inline int my_bind(
ndb_socket_t s,
const struct sockaddr *my_addr,
119 SOCKET_SIZE_TYPE len)
121 return bind(s.s, my_addr, len);
124 static inline int my_bind_inet(
ndb_socket_t s,
const struct sockaddr_in *my_addr)
126 return bind(s.s, (
const struct sockaddr*)my_addr,
sizeof(
struct sockaddr_in));
129 static inline int my_socket_get_port(
ndb_socket_t s,
unsigned short *port)
131 struct sockaddr_in servaddr;
132 SOCKET_SIZE_TYPE sock_len =
sizeof(servaddr);
133 if(getsockname(s.s, (
struct sockaddr*)&servaddr, &sock_len) < 0) {
137 *port= ntohs(servaddr.sin_port);
141 static inline int my_listen(
ndb_socket_t s,
int backlog)
143 return listen(s.s, backlog);
148 SOCKET_SIZE_TYPE *addrlen)
151 r.s= accept(s.s, addr, addrlen);
155 static inline int my_connect_inet(
ndb_socket_t s,
const struct sockaddr_in *addr)
157 return connect(s.s, (
const struct sockaddr*) addr,
158 sizeof(
struct sockaddr_in));
163 void *optval, SOCKET_SIZE_TYPE *optlen)
165 return getsockopt(s.s, level, optname, (
char*)optval, optlen);
170 void *optval, SOCKET_SIZE_TYPE optlen)
172 return setsockopt(s.s, level, optname, (
char*)optval, optlen);
175 static inline int my_socket_connect_address(
ndb_socket_t s,
struct in_addr *a)
177 struct sockaddr_in addr;
178 SOCKET_SIZE_TYPE addrlen=
sizeof(addr);
179 if(getpeername(s.s, (
struct sockaddr*)&addr, &addrlen)==SOCKET_ERROR)
180 return my_socket_errno();
186 static inline int my_getpeername(
ndb_socket_t s,
struct sockaddr *a,
187 SOCKET_SIZE_TYPE *addrlen)
189 if(getpeername(s.s, a, addrlen))
190 return my_socket_errno();
197 return shutdown(s.s, how);
219 if (WSARecv(s.s,(LPWSABUF)iov,iovcnt,&rv,0,0,0) == SOCKET_ERROR)
228 if (WSASend(s.s,(LPWSABUF)iov,iovcnt,&rv,0,0,0) == SOCKET_ERROR)