BOSS 7.0.8
BESIII Offline Software System
Loading...
Searching...
No Matches
XmlRpc::XmlRpcSocket Class Reference

A platform-independent socket API. More...

#include <XmlRpcSocket.h>

Static Public Member Functions

static int socket ()
 Creates a stream (TCP) socket. Returns -1 on failure.
 
static void close (int socket)
 Closes a socket.
 
static bool setNonBlocking (int socket)
 Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
 
static bool nbRead (int socket, std::string &s, bool *eof)
 Read text from the specified socket. Returns false on error.
 
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 bind (int socket, int port)
 Bind to a specified port.
 
static bool listen (int socket, int backlog)
 Set socket in listen mode.
 
static int accept (int socket)
 Accept a client connection request.
 
static bool connect (int socket, std::string &host, int port)
 Connect a socket to a server (from a client)
 
static int getError ()
 Returns last errno.
 
static std::string getErrorMsg ()
 Returns message corresponding to last error.
 
static std::string getErrorMsg (int error)
 Returns message corresponding to error.
 

Detailed Description

A platform-independent socket API.

Definition at line 17 of file XmlRpcSocket.h.

Member Function Documentation

◆ accept()

int XmlRpcSocket::accept ( int  socket)
static

Accept a client connection request.

Definition at line 132 of file XmlRpcSocket.cpp.

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}
static int accept(int socket)
Accept a client connection request.

Referenced by accept(), and XmlRpc::XmlRpcServer::acceptConnection().

◆ bind()

bool XmlRpcSocket::bind ( int  socket,
int  port 
)
static

Bind to a specified port.

Definition at line 112 of file XmlRpcSocket.cpp.

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}
static bool bind(int socket, int port)
Bind to a specified port.

Referenced by bind(), and XmlRpc::XmlRpcServer::bindAndListen().

◆ close()

void XmlRpcSocket::close ( int  socket)
static

Closes a socket.

Definition at line 76 of file XmlRpcSocket.cpp.

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}
static void log(int level, const char *fmt,...)
Dump messages somewhere.
Definition: XmlRpcUtil.cpp:71
file close()

Referenced by XmlRpc::XmlRpcServer::acceptConnection(), and XmlRpc::XmlRpcSource::close().

◆ connect()

bool XmlRpcSocket::connect ( int  socket,
std::string &  host,
int  port 
)
static

Connect a socket to a server (from a client)

Definition at line 149 of file XmlRpcSocket.cpp.

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}
static bool connect(int socket, std::string &host, int port)
Connect a socket to a server (from a client)

Referenced by connect(), and XmlRpc::XmlRpcClient::doConnect().

◆ getError()

int XmlRpcSocket::getError ( )
static

Returns last errno.

Definition at line 235 of file XmlRpcSocket.cpp.

236{
237#if defined(_WINDOWS)
238 return WSAGetLastError();
239#else
240 return errno;
241#endif
242}

Referenced by getErrorMsg().

◆ getErrorMsg() [1/2]

◆ getErrorMsg() [2/2]

std::string XmlRpcSocket::getErrorMsg ( int  error)
static

Returns message corresponding to error.

Definition at line 254 of file XmlRpcSocket.cpp.

255{
256 char err[60];
257 snprintf(err,sizeof(err),"error %d", error);
258 return std::string(err);
259}

◆ listen()

bool XmlRpcSocket::listen ( int  socket,
int  backlog 
)
static

Set socket in listen mode.

Definition at line 125 of file XmlRpcSocket.cpp.

126{
127 return (::listen(fd, backlog) == 0);
128}
static bool listen(int socket, int backlog)
Set socket in listen mode.

Referenced by XmlRpc::XmlRpcServer::bindAndListen(), and listen().

◆ nbRead()

bool XmlRpcSocket::nbRead ( int  socket,
std::string &  s,
bool eof 
)
static

Read text from the specified socket. Returns false on error.

Definition at line 172 of file XmlRpcSocket.cpp.

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}
const Int_t n
XmlRpcServer s
Definition: HelloServer.cpp:11

Referenced by XmlRpc::XmlRpcClient::readHeader(), XmlRpc::XmlRpcServerConnection::readHeader(), XmlRpc::XmlRpcServerConnection::readRequest(), and XmlRpc::XmlRpcClient::readResponse().

◆ nbWrite()

bool XmlRpcSocket::nbWrite ( int  socket,
std::string &  s,
int *  bytesSoFar 
)
static

Write text to the specified socket. Returns false on error.

Definition at line 205 of file XmlRpcSocket.cpp.

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}

Referenced by XmlRpc::XmlRpcClient::writeRequest(), and XmlRpc::XmlRpcServerConnection::writeResponse().

◆ setNonBlocking()

bool XmlRpcSocket::setNonBlocking ( int  socket)
static

Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.

Definition at line 90 of file XmlRpcSocket.cpp.

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}

Referenced by XmlRpc::XmlRpcServer::acceptConnection(), XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::doConnect().

◆ setReuseAddr()

bool XmlRpcSocket::setReuseAddr ( int  socket)
static

Allow the port the specified socket is bound to to be re-bound immediately so server re-starts are not delayed. Returns false on failure.

Definition at line 102 of file XmlRpcSocket.cpp.

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}

Referenced by XmlRpc::XmlRpcServer::bindAndListen().

◆ socket()

int XmlRpcSocket::socket ( )
static

Creates a stream (TCP) socket. Returns -1 on failure.

Definition at line 68 of file XmlRpcSocket.cpp.

69{
71 return (int) ::socket(AF_INET, SOCK_STREAM, 0);
72}
#define initWinSock()
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.

Referenced by XmlRpc::XmlRpcServer::bindAndListen(), XmlRpc::XmlRpcClient::doConnect(), and socket().


The documentation for this class was generated from the following files: