A platform-independent socket API. More...
#include <XmlRpcSocket.h>
Static Public Member Functions | |
static int | getSocket () |
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, SSL *ssl) |
Read text from the specified socket. Returns false on error. | |
static bool | nbWrite (int socket, std::string &s, int *bytesSoFar, SSL *ssl) |
Write text to the specified socket. Returns false on error. | |
static bool | setReuseAddr (int socket) |
Allow the port the specified socket is bound to to be re-bound immediately so server re-starts are not delayed. | |
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 | getPort (int socket) |
Get the port of a bound socket. | |
static bool | nonFatalError () |
Returns true if the last error was not a fatal one (eg, EWOULDBLOCK). | |
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. |
A platform-independent socket API.
Definition at line 31 of file XmlRpcSocket.h.
int XmlRpcSocket::accept | ( | int | socket | ) | [static] |
Accept a client connection request.
Definition at line 142 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::acceptConnection().
00143 { 00144 struct sockaddr_in addr; 00145 socklen_t addrlen = sizeof(addr); 00146 00147 return (int) ::accept(fd, (struct sockaddr*)&addr, &addrlen); 00148 }
bool XmlRpcSocket::bind | ( | int | socket, | |
int | port | |||
) | [static] |
Bind to a specified port.
Definition at line 122 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::bindAndListen().
00123 { 00124 struct sockaddr_in saddr; 00125 memset(&saddr, 0, sizeof(saddr)); 00126 saddr.sin_family = AF_INET; 00127 saddr.sin_addr.s_addr = htonl(INADDR_ANY); 00128 saddr.sin_port = htons((u_short) port); 00129 return (::bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == 0); 00130 }
void XmlRpcSocket::close | ( | int | socket | ) | [static] |
Closes a socket.
Definition at line 86 of file XmlRpcSocket.cc.
00087 { 00088 XmlRpcUtil::log(4, "XmlRpcSocket::close: fd %d.", fd); 00089 #if defined(_WINDOWS) 00090 closesocket(fd); 00091 #else 00092 ::close(fd); 00093 #endif // _WINDOWS 00094 }
bool XmlRpcSocket::connect | ( | int | socket, | |
std::string & | host, | |||
int | port | |||
) | [static] |
Connect a socket to a server (from a client).
Definition at line 154 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcClient::doConnect().
00155 { 00156 struct sockaddr_in saddr; 00157 memset(&saddr, 0, sizeof(saddr)); 00158 saddr.sin_family = AF_INET; 00159 00160 struct hostent *hp = gethostbyname(host.c_str()); 00161 if (hp == 0) return false; 00162 00163 saddr.sin_family = hp->h_addrtype; 00164 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length); 00165 saddr.sin_port = htons((u_short) port); 00166 00167 // For asynch operation, this will return EWOULDBLOCK (windows) or 00168 // EINPROGRESS (linux) and we just need to wait for the socket to be writable... 00169 int result = ::connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); 00170 return result == 0 || nonFatalError(); 00171 }
int XmlRpcSocket::getError | ( | ) | [static] |
Returns last errno.
Definition at line 272 of file XmlRpcSocket.cc.
Referenced by getErrorMsg(), and nonFatalError().
std::string XmlRpcSocket::getErrorMsg | ( | int | error | ) | [static] |
Returns message corresponding to error.
Definition at line 291 of file XmlRpcSocket.cc.
00292 { 00293 char err[60]; 00294 snprintf(err,sizeof(err),"error %d", error); 00295 return std::string(err); 00296 }
std::string XmlRpcSocket::getErrorMsg | ( | ) | [static] |
Returns message corresponding to last error.
Definition at line 284 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::acceptConnection(), XmlRpc::XmlRpcServer::bindAndListen(), XmlRpc::XmlRpcClient::doConnect(), XmlRpc::XmlRpcClient::handleEvent(), XmlRpc::XmlRpcServerConnection::readHeader(), XmlRpc::XmlRpcClient::readHeader(), XmlRpc::XmlRpcServerConnection::readRequest(), XmlRpc::XmlRpcClient::readResponse(), XmlRpc::XmlRpcClient::writeRequest(), and XmlRpc::XmlRpcServerConnection::writeResponse().
00285 { 00286 return getErrorMsg(getError()); 00287 }
int XmlRpcSocket::getPort | ( | int | socket | ) | [static] |
Get the port of a bound socket.
Definition at line 253 of file XmlRpcSocket.cc.
00254 { 00255 struct sockaddr_in saddr; 00256 socklen_t saddr_len = sizeof(saddr); 00257 int port; 00258 00259 int result = ::getsockname(socket, (sockaddr*) &saddr, &saddr_len); 00260 00261 if (result != 0) { 00262 port = -1; 00263 } else { 00264 port = ntohs(saddr.sin_port); 00265 } 00266 return port; 00267 }
int XmlRpcSocket::getSocket | ( | ) | [static] |
Creates a stream (TCP) socket. Returns -1 on failure.
Definition at line 78 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::doConnect().
00079 { 00080 initWinSock(); 00081 return (int) ::socket(AF_INET, SOCK_STREAM, 0); 00082 }
bool XmlRpcSocket::listen | ( | int | socket, | |
int | backlog | |||
) | [static] |
Set socket in listen mode.
Definition at line 135 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::bindAndListen().
00136 { 00137 return (::listen(fd, backlog) == 0); 00138 }
bool XmlRpcSocket::nbRead | ( | int | socket, | |
std::string & | s, | |||
bool * | eof, | |||
SSL * | ssl | |||
) | [static] |
Read text from the specified socket. Returns false on error.
Definition at line 177 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServerConnection::readHeader(), XmlRpc::XmlRpcClient::readHeader(), XmlRpc::XmlRpcServerConnection::readRequest(), and XmlRpc::XmlRpcClient::readResponse().
00178 { 00179 const int READ_SIZE = 4096; // Number of bytes to attempt to read at a time 00180 char readBuf[READ_SIZE]; 00181 00182 bool wouldBlock = false; 00183 *eof = false; 00184 00185 while ( ! wouldBlock && ! *eof) { 00186 #if defined(_WINDOWS) 00187 int n = recv(fd, readBuf, READ_SIZE-1, 0); 00188 #else 00189 int n = 0; 00190 if (ssl != (SSL *) NULL) { 00191 #ifdef USE_SSL 00192 n = SSL_read(ssl, readBuf, READ_SIZE-1); 00193 #endif 00194 } else { 00195 n = read(fd, readBuf, READ_SIZE-1); 00196 } 00197 #endif 00198 XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n); 00199 00200 if (n > 0) { 00201 readBuf[n] = 0; 00202 s.append(readBuf, n); 00203 } else if (n == 0) { 00204 *eof = true; 00205 } else if (nonFatalError()) { 00206 wouldBlock = true; 00207 } else { 00208 return false; // Error 00209 } 00210 } 00211 return true; 00212 }
bool XmlRpcSocket::nbWrite | ( | int | socket, | |
std::string & | s, | |||
int * | bytesSoFar, | |||
SSL * | ssl | |||
) | [static] |
Write text to the specified socket. Returns false on error.
Definition at line 217 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcClient::writeRequest(), and XmlRpc::XmlRpcServerConnection::writeResponse().
00218 { 00219 int nToWrite = int(s.length()) - *bytesSoFar; 00220 char *sp = const_cast<char*>(s.c_str()) + *bytesSoFar; 00221 bool wouldBlock = false; 00222 00223 while ( nToWrite > 0 && ! wouldBlock ) { 00224 #if defined(_WINDOWS) 00225 int n = send(fd, sp, nToWrite, 0); 00226 #else 00227 int n = 0; 00228 if (ssl != (SSL *) NULL) { 00229 #ifdef USE_SSL 00230 n = SSL_write(ssl, sp, nToWrite); 00231 #endif 00232 } else { 00233 n = write(fd, sp, nToWrite); 00234 } 00235 #endif 00236 XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n); 00237 00238 if (n > 0) { 00239 sp += n; 00240 *bytesSoFar += n; 00241 nToWrite -= n; 00242 } else if (nonFatalError()) { 00243 wouldBlock = true; 00244 } else { 00245 return false; // Error 00246 } 00247 } 00248 return true; 00249 }
bool XmlRpcSocket::nonFatalError | ( | ) | [static] |
Returns true if the last error was not a fatal one (eg, EWOULDBLOCK).
Definition at line 70 of file XmlRpcSocket.cc.
Referenced by connect(), nbRead(), and nbWrite().
00071 { 00072 int err = XmlRpcSocket::getError(); 00073 return (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK || err == EINTR); 00074 }
bool XmlRpcSocket::setNonBlocking | ( | int | socket | ) | [static] |
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
Definition at line 100 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::acceptConnection(), XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::doConnect().
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 112 of file XmlRpcSocket.cc.
Referenced by XmlRpc::XmlRpcServer::bindAndListen().