17 # include <winsock2.h>
20 # define EINPROGRESS WSAEINPROGRESS
21 # define EWOULDBLOCK WSAEWOULDBLOCK
22 # define ETIMEDOUT WSAETIMEDOUT
24 typedef int socklen_t;
31 # include <sys/types.h>
32 # include <sys/socket.h>
33 # include <netinet/in.h>
43 using namespace XmlRpc;
51 static bool wsInit =
false;
54 WORD wVersionRequested = MAKEWORD( 2, 0 );
56 WSAStartup(wVersionRequested, &wsaData);
73 return (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK || err == EINTR);
81 return (
int) ::socket(AF_INET, SOCK_STREAM, 0);
102 #if defined(_WINDOWS)
103 unsigned long flag = 1;
104 return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0);
106 return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
116 return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
const char *)&sflag,
sizeof(sflag)) == 0);
124 struct sockaddr_in saddr;
125 memset(&saddr, 0,
sizeof(saddr));
126 saddr.sin_family = AF_INET;
127 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
128 saddr.sin_port = htons((u_short) port);
129 return (::
bind(fd, (
struct sockaddr *)&saddr,
sizeof(saddr)) == 0);
137 return (::
listen(fd, backlog) == 0);
144 struct sockaddr_in addr;
145 socklen_t addrlen =
sizeof(addr);
147 return (
int)
::accept(fd, (
struct sockaddr*)&addr, &addrlen);
156 struct sockaddr_in saddr;
157 memset(&saddr, 0,
sizeof(saddr));
158 saddr.sin_family = AF_INET;
160 struct hostent *hp = gethostbyname(host.c_str());
161 if (hp == 0)
return false;
163 saddr.sin_family = hp->h_addrtype;
164 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
165 saddr.sin_port = htons((u_short) port);
169 int result =
::connect(fd, (
struct sockaddr *)&saddr,
sizeof(saddr));
179 const int READ_SIZE = 4096;
180 char readBuf[READ_SIZE];
182 bool wouldBlock =
false;
185 while ( ! wouldBlock && ! *eof) {
186 #if defined(_WINDOWS)
187 int n = recv(fd, readBuf, READ_SIZE-1, 0);
190 if (ssl != (
SSL *) NULL) {
192 n =
SSL_read(ssl, readBuf, READ_SIZE-1);
195 n = read(fd, readBuf, READ_SIZE-1);
202 s.append(readBuf, n);
219 int nToWrite = int(s.length()) - *bytesSoFar;
220 char *sp =
const_cast<char*
>(s.c_str()) + *bytesSoFar;
221 bool wouldBlock =
false;
223 while ( nToWrite > 0 && ! wouldBlock ) {
224 #if defined(_WINDOWS)
225 int n = send(fd, sp, nToWrite, 0);
228 if (ssl != (
SSL *) NULL) {
233 n = write(fd, sp, nToWrite);
236 XmlRpcUtil::log(5,
"XmlRpcSocket::nbWrite: send/write returned %d.", n);
255 struct sockaddr_in saddr;
256 socklen_t saddr_len =
sizeof(saddr);
259 int result = ::getsockname(socket, (sockaddr*) &saddr, &saddr_len);
264 port = ntohs(saddr.sin_port);
274 #if defined(_WINDOWS)
275 return WSAGetLastError();
294 snprintf(err,
sizeof(err),
"error %d", error);
295 return std::string(err);