BOSS 8.0.0
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 101 of file XmlRpcSocket.cpp.

101 {
102 struct sockaddr_in addr;
103#if defined( _WINDOWS )
104 int
105#else
106 socklen_t
107#endif
108 addrlen = sizeof( addr );
109
110 return (int)::accept( fd, (struct sockaddr*)&addr, &addrlen );
111}
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 89 of file XmlRpcSocket.cpp.

89 {
90 struct sockaddr_in saddr;
91 memset( &saddr, 0, sizeof( saddr ) );
92 saddr.sin_family = AF_INET;
93 saddr.sin_addr.s_addr = htonl( INADDR_ANY );
94 saddr.sin_port = htons( (u_short)port );
95 return ( ::bind( fd, (struct sockaddr*)&saddr, sizeof( saddr ) ) == 0 );
96}
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 63 of file XmlRpcSocket.cpp.

63 {
64 XmlRpcUtil::log( 4, "XmlRpcSocket::close: fd %d.", fd );
65#if defined( _WINDOWS )
66 closesocket( fd );
67#else
68 ::close( fd );
69#endif // _WINDOWS
70}
static void close(int socket)
Closes a socket.
static void log(int level, const char *fmt,...)
Dump messages somewhere.

Referenced by XmlRpc::XmlRpcServer::acceptConnection(), close(), 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 114 of file XmlRpcSocket.cpp.

114 {
115 struct sockaddr_in saddr;
116 memset( &saddr, 0, sizeof( saddr ) );
117 saddr.sin_family = AF_INET;
118
119 struct hostent* hp = gethostbyname( host.c_str() );
120 if ( hp == 0 ) return false;
121
122 saddr.sin_family = hp->h_addrtype;
123 memcpy( &saddr.sin_addr, hp->h_addr, hp->h_length );
124 saddr.sin_port = htons( (u_short)port );
125
126 // For asynch operation, this will return EWOULDBLOCK (windows) or
127 // EINPROGRESS (linux) and we just need to wait for the socket to be writable...
128 int result = ::connect( fd, (struct sockaddr*)&saddr, sizeof( saddr ) );
129 return result == 0 || nonFatalError();
130}
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 195 of file XmlRpcSocket.cpp.

195 {
196#if defined( _WINDOWS )
197 return WSAGetLastError();
198#else
199 return errno;
200#endif
201}

Referenced by getErrorMsg().

◆ getErrorMsg() [1/2]

◆ getErrorMsg() [2/2]

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

Returns message corresponding to error.

Definition at line 207 of file XmlRpcSocket.cpp.

207 {
208 char err[60];
209 snprintf( err, sizeof( err ), "error %d", error );
210 return std::string( err );
211}

◆ listen()

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

Set socket in listen mode.

Definition at line 99 of file XmlRpcSocket.cpp.

99{ return ( ::listen( fd, backlog ) == 0 ); }
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 133 of file XmlRpcSocket.cpp.

133 {
134 const int READ_SIZE = 4096; // Number of bytes to attempt to read at a time
135 char readBuf[READ_SIZE];
136
137 bool wouldBlock = false;
138 *eof = false;
139
140 while ( !wouldBlock && !*eof )
141 {
142#if defined( _WINDOWS )
143 int n = recv( fd, readBuf, READ_SIZE - 1, 0 );
144#else
145 int n = read( fd, readBuf, READ_SIZE - 1 );
146#endif
147 XmlRpcUtil::log( 5, "XmlRpcSocket::nbRead: read/recv returned %d.", n );
148
149 if ( n > 0 )
150 {
151 readBuf[n] = 0;
152 s.append( readBuf, n );
153 }
154 else if ( n == 0 ) { *eof = true; }
155 else if ( nonFatalError() ) { wouldBlock = true; }
156 else
157 {
158 return false; // Error
159 }
160 }
161 return true;
162}
const Int_t n
XmlRpcServer s

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 165 of file XmlRpcSocket.cpp.

165 {
166 int nToWrite = int( s.length() ) - *bytesSoFar;
167 char* sp = const_cast<char*>( s.c_str() ) + *bytesSoFar;
168 bool wouldBlock = false;
169
170 while ( nToWrite > 0 && !wouldBlock )
171 {
172#if defined( _WINDOWS )
173 int n = send( fd, sp, nToWrite, 0 );
174#else
175 int n = write( fd, sp, nToWrite );
176#endif
177 XmlRpcUtil::log( 5, "XmlRpcSocket::nbWrite: send/write returned %d.", n );
178
179 if ( n > 0 )
180 {
181 sp += n;
182 *bytesSoFar += n;
183 nToWrite -= n;
184 }
185 else if ( nonFatalError() ) { wouldBlock = true; }
186 else
187 {
188 return false; // Error
189 }
190 }
191 return true;
192}

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 72 of file XmlRpcSocket.cpp.

72 {
73#if defined( _WINDOWS )
74 unsigned long flag = 1;
75 return ( ioctlsocket( (SOCKET)fd, FIONBIO, &flag ) == 0 );
76#else
77 return ( fcntl( fd, F_SETFL, O_NONBLOCK ) == 0 );
78#endif // _WINDOWS
79}

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 81 of file XmlRpcSocket.cpp.

81 {
82 // Allow this port to be re-bound immediately so server re-starts are not delayed
83 int sflag = 1;
84 return ( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&sflag, sizeof( sflag ) ) ==
85 0 );
86}

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

◆ socket()

int XmlRpcSocket::socket ( )
static

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

Definition at line 58 of file XmlRpcSocket.cpp.

58 {
60 return (int)::socket( AF_INET, SOCK_STREAM, 0 );
61}
#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: