Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

XmlRpc::XmlRpcSocket Class Reference

A platform-independent socket API. More...

#include <XmlRpcSocket.h>

List of all members.

Static Public Member Functions

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


Detailed Description

A platform-independent socket API.


Member Function Documentation

int XmlRpcSocket::accept int  socket  )  [static]
 

Accept a client connection request.

00132 {
00133   struct sockaddr_in addr;
00134 #if defined(_WINDOWS)
00135   int
00136 #else
00137   socklen_t
00138 #endif
00139     addrlen = sizeof(addr);
00140 
00141   return (int) ::accept(fd, (struct sockaddr*)&addr, &addrlen);
00142 }

bool XmlRpcSocket::bind int  socket,
int  port
[static]
 

Bind to a specified port.

00112 {
00113   struct sockaddr_in saddr;
00114   memset(&saddr, 0, sizeof(saddr));
00115   saddr.sin_family = AF_INET;
00116   saddr.sin_addr.s_addr = htonl(INADDR_ANY);
00117   saddr.sin_port = htons((u_short) port);
00118   return (::bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == 0);
00119 }

void XmlRpcSocket::close int  socket  )  [static]
 

Closes a socket.

00076 {
00077   XmlRpcUtil::log(4, "XmlRpcSocket::close: fd %d.", fd);
00078 #if defined(_WINDOWS)
00079   closesocket(fd);
00080 #else
00081   ::close(fd);
00082 #endif // _WINDOWS
00083 }

bool XmlRpcSocket::connect int  socket,
std::string &  host,
int  port
[static]
 

Connect a socket to a server (from a client).

00149 {
00150   struct sockaddr_in saddr;
00151   memset(&saddr, 0, sizeof(saddr));
00152   saddr.sin_family = AF_INET;
00153 
00154   struct hostent *hp = gethostbyname(host.c_str());
00155   if (hp == 0) return false;
00156 
00157   saddr.sin_family = hp->h_addrtype;
00158   memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
00159   saddr.sin_port = htons((u_short) port);
00160 
00161   // For asynch operation, this will return EWOULDBLOCK (windows) or
00162   // EINPROGRESS (linux) and we just need to wait for the socket to be writable...
00163   int result = ::connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
00164   return result == 0 || nonFatalError();
00165 }

int XmlRpcSocket::getError  )  [static]
 

Returns last errno.

00235 {
00236 #if defined(_WINDOWS)
00237   return WSAGetLastError();
00238 #else
00239   return errno;
00240 #endif
00241 }

std::string XmlRpcSocket::getErrorMsg int  error  )  [static]
 

Returns message corresponding to error.

00254 {
00255   char err[60];
00256   snprintf(err,sizeof(err),"error %d", error);
00257   return std::string(err);
00258 }

std::string XmlRpcSocket::getErrorMsg  )  [static]
 

Returns message corresponding to last error.

00247 {
00248   return getErrorMsg(getError());
00249 }

bool XmlRpcSocket::listen int  socket,
int  backlog
[static]
 

Set socket in listen mode.

00125 {
00126   return (::listen(fd, backlog) == 0);
00127 }

bool XmlRpcSocket::nbRead int  socket,
std::string &  s,
bool eof
[static]
 

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

00172 {
00173   const int READ_SIZE = 4096;   // Number of bytes to attempt to read at a time
00174   char readBuf[READ_SIZE];
00175 
00176   bool wouldBlock = false;
00177   *eof = false;
00178 
00179   while ( ! wouldBlock && ! *eof) {
00180 #if defined(_WINDOWS)
00181     int n = recv(fd, readBuf, READ_SIZE-1, 0);
00182 #else
00183     int n = read(fd, readBuf, READ_SIZE-1);
00184 #endif
00185     XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n);
00186 
00187     if (n > 0) {
00188       readBuf[n] = 0;
00189       s.append(readBuf, n);
00190     } else if (n == 0) {
00191       *eof = true;
00192     } else if (nonFatalError()) {
00193       wouldBlock = true;
00194     } else {
00195       return false;   // Error
00196     }
00197   }
00198   return true;
00199 }

bool XmlRpcSocket::nbWrite int  socket,
std::string &  s,
int *  bytesSoFar
[static]
 

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

00205 {
00206   int nToWrite = int(s.length()) - *bytesSoFar;
00207   char *sp = const_cast<char*>(s.c_str()) + *bytesSoFar;
00208   bool wouldBlock = false;
00209 
00210   while ( nToWrite > 0 && ! wouldBlock ) {
00211 #if defined(_WINDOWS)
00212     int n = send(fd, sp, nToWrite, 0);
00213 #else
00214     int n = write(fd, sp, nToWrite);
00215 #endif
00216     XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n);
00217 
00218     if (n > 0) {
00219       sp += n;
00220       *bytesSoFar += n;
00221       nToWrite -= n;
00222     } else if (nonFatalError()) {
00223       wouldBlock = true;
00224     } else {
00225       return false;   // Error
00226     }
00227   }
00228   return true;
00229 }

bool XmlRpcSocket::setNonBlocking int  socket  )  [static]
 

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

00090 {
00091 #if defined(_WINDOWS)
00092   unsigned long flag = 1;
00093   return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0);
00094 #else
00095   return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
00096 #endif // _WINDOWS
00097 }

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.

00102 {
00103   // Allow this port to be re-bound immediately so server re-starts are not delayed
00104   int sflag = 1;
00105   return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&sflag, sizeof(sflag)) == 0);
00106 }

int XmlRpcSocket::socket  )  [static]
 

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

00068 {
00069   initWinSock();
00070   return (int) ::socket(AF_INET, SOCK_STREAM, 0);
00071 }


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 19:25:35 2011 for BOSS6.5.5 by  doxygen 1.3.9.1