BOSS 7.0.7
BESIII Offline Software System
Loading...
Searching...
No Matches
XmlRpcSocket.cpp
Go to the documentation of this file.
1
2#include "XmlRpcSocket.h"
3#include "XmlRpcUtil.h"
4
5#ifndef MAKEDEPEND
6
7#if defined(_WINDOWS)
8# include <stdio.h>
9# include <winsock2.h>
10//# pragma lib(WS2_32.lib)
11
12# define EINPROGRESS WSAEINPROGRESS
13# define EWOULDBLOCK WSAEWOULDBLOCK
14# define ETIMEDOUT WSAETIMEDOUT
15#else
16extern "C" {
17# include <unistd.h>
18# include <stdio.h>
19# include <sys/types.h>
20# include <sys/socket.h>
21# include <netinet/in.h>
22# include <netdb.h>
23# include <errno.h>
24# include <fcntl.h>
25#include <cstring>
26}
27#endif // _WINDOWS
28
29#endif // MAKEDEPEND
30
31
32using namespace XmlRpc;
33
34
35
36#if defined(_WINDOWS)
37
38static void initWinSock()
39{
40 static bool wsInit = false;
41 if (! wsInit)
42 {
43 WORD wVersionRequested = MAKEWORD( 2, 0 );
44 WSADATA wsaData;
45 WSAStartup(wVersionRequested, &wsaData);
46 wsInit = true;
47 }
48}
49
50#else
51
52#define initWinSock()
53
54#endif // _WINDOWS
55
56
57// These errors are not considered fatal for an IO operation; the operation will be re-tried.
58static inline bool
59nonFatalError()
60{
61 int err = XmlRpcSocket::getError();
62 return (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK || err == EINTR);
63}
64
65
66
67int
69{
71 return (int) ::socket(AF_INET, SOCK_STREAM, 0);
72}
73
74
75void
77{
78 XmlRpcUtil::log(4, "XmlRpcSocket::close: fd %d.", fd);
79#if defined(_WINDOWS)
80 closesocket(fd);
81#else
82 ::close(fd);
83#endif // _WINDOWS
84}
85
86
87
88
89bool
91{
92#if defined(_WINDOWS)
93 unsigned long flag = 1;
94 return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0);
95#else
96 return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
97#endif // _WINDOWS
98}
99
100
101bool
103{
104 // Allow this port to be re-bound immediately so server re-starts are not delayed
105 int sflag = 1;
106 return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&sflag, sizeof(sflag)) == 0);
107}
108
109
110// Bind to a specified port
111bool
112XmlRpcSocket::bind(int fd, int port)
113{
114 struct sockaddr_in saddr;
115 memset(&saddr, 0, sizeof(saddr));
116 saddr.sin_family = AF_INET;
117 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
118 saddr.sin_port = htons((u_short) port);
119 return (::bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == 0);
120}
121
122
123// Set socket in listen mode
124bool
125XmlRpcSocket::listen(int fd, int backlog)
126{
127 return (::listen(fd, backlog) == 0);
128}
129
130
131int
133{
134 struct sockaddr_in addr;
135#if defined(_WINDOWS)
136 int
137#else
138 socklen_t
139#endif
140 addrlen = sizeof(addr);
141
142 return (int) ::accept(fd, (struct sockaddr*)&addr, &addrlen);
143}
144
145
146
147// Connect a socket to a server (from a client)
148bool
149XmlRpcSocket::connect(int fd, std::string& host, int port)
150{
151 struct sockaddr_in saddr;
152 memset(&saddr, 0, sizeof(saddr));
153 saddr.sin_family = AF_INET;
154
155 struct hostent *hp = gethostbyname(host.c_str());
156 if (hp == 0) return false;
157
158 saddr.sin_family = hp->h_addrtype;
159 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
160 saddr.sin_port = htons((u_short) port);
161
162 // For asynch operation, this will return EWOULDBLOCK (windows) or
163 // EINPROGRESS (linux) and we just need to wait for the socket to be writable...
164 int result = ::connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
165 return result == 0 || nonFatalError();
166}
167
168
169
170// Read available text from the specified socket. Returns false on error.
171bool
172XmlRpcSocket::nbRead(int fd, std::string& s, bool *eof)
173{
174 const int READ_SIZE = 4096; // Number of bytes to attempt to read at a time
175 char readBuf[READ_SIZE];
176
177 bool wouldBlock = false;
178 *eof = false;
179
180 while ( ! wouldBlock && ! *eof) {
181#if defined(_WINDOWS)
182 int n = recv(fd, readBuf, READ_SIZE-1, 0);
183#else
184 int n = read(fd, readBuf, READ_SIZE-1);
185#endif
186 XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n);
187
188 if (n > 0) {
189 readBuf[n] = 0;
190 s.append(readBuf, n);
191 } else if (n == 0) {
192 *eof = true;
193 } else if (nonFatalError()) {
194 wouldBlock = true;
195 } else {
196 return false; // Error
197 }
198 }
199 return true;
200}
201
202
203// Write text to the specified socket. Returns false on error.
204bool
205XmlRpcSocket::nbWrite(int fd, std::string& s, int *bytesSoFar)
206{
207 int nToWrite = int(s.length()) - *bytesSoFar;
208 char *sp = const_cast<char*>(s.c_str()) + *bytesSoFar;
209 bool wouldBlock = false;
210
211 while ( nToWrite > 0 && ! wouldBlock ) {
212#if defined(_WINDOWS)
213 int n = send(fd, sp, nToWrite, 0);
214#else
215 int n = write(fd, sp, nToWrite);
216#endif
217 XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n);
218
219 if (n > 0) {
220 sp += n;
221 *bytesSoFar += n;
222 nToWrite -= n;
223 } else if (nonFatalError()) {
224 wouldBlock = true;
225 } else {
226 return false; // Error
227 }
228 }
229 return true;
230}
231
232
233// Returns last errno
234int
236{
237#if defined(_WINDOWS)
238 return WSAGetLastError();
239#else
240 return errno;
241#endif
242}
243
244
245// Returns message corresponding to last errno
246std::string
248{
249 return getErrorMsg(getError());
250}
251
252// Returns message corresponding to errno... well, it should anyway
253std::string
255{
256 char err[60];
257 snprintf(err,sizeof(err),"error %d", error);
258 return std::string(err);
259}
260
261
XmlRpcServer s
Definition: HelloServer.cpp:11
#define initWinSock()
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
static bool listen(int socket, int backlog)
Set socket in listen mode.
static int getError()
Returns last errno.
static int accept(int socket)
Accept a client connection request.
static bool nbWrite(int socket, std::string &s, int *bytesSoFar)
Write text to the specified socket. Returns false on error.
static bool setReuseAddr(int socket)
static bool connect(int socket, std::string &host, int port)
Connect a socket to a server (from a client)
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
static void close(int socket)
Closes a socket.
static bool nbRead(int socket, std::string &s, bool *eof)
Read text from the specified socket. Returns false on error.
static bool bind(int socket, int port)
Bind to a specified port.
static std::string getErrorMsg()
Returns message corresponding to last error.
static void log(int level, const char *fmt,...)
Dump messages somewhere.
Definition: XmlRpcUtil.cpp:71
file close()
Definition: XmlRpc.h:35