Static Public Member Functions

XmlRpc::XmlRpcSocket Class Reference

A platform-independent socket API. More...

#include <XmlRpcSocket.h>

List of all members.

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.

Detailed Description

A platform-independent socket API.

Definition at line 31 of file XmlRpcSocket.h.


Member Function Documentation

int XmlRpcSocket::accept ( int  socket  )  [static]

Accept a client connection request.

Definition at line 142 of file XmlRpcSocket.cc.

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

{
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);

  return (int) ::accept(fd, (struct sockaddr*)&addr, &addrlen);
}

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().

{
  struct sockaddr_in saddr;
  memset(&saddr, 0, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  saddr.sin_port = htons((u_short) port);
  return (::bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == 0);
}

void XmlRpcSocket::close ( int  socket  )  [static]

Closes a socket.

Definition at line 86 of file XmlRpcSocket.cc.

{
  XmlRpcUtil::log(4, "XmlRpcSocket::close: fd %d.", fd);
#if defined(_WINDOWS)
  closesocket(fd);
#else
  ::close(fd);
#endif // _WINDOWS
}

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().

{
  struct sockaddr_in saddr;
  memset(&saddr, 0, sizeof(saddr));
  saddr.sin_family = AF_INET;

  struct hostent *hp = gethostbyname(host.c_str());
  if (hp == 0) return false;

  saddr.sin_family = hp->h_addrtype;
  memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
  saddr.sin_port = htons((u_short) port);

  // For asynch operation, this will return EWOULDBLOCK (windows) or
  // EINPROGRESS (linux) and we just need to wait for the socket to be writable...
  int result = ::connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
  return result == 0 || nonFatalError();
}

int XmlRpcSocket::getError (  )  [static]

Returns last errno.

Definition at line 272 of file XmlRpcSocket.cc.

Referenced by getErrorMsg(), and nonFatalError().

{
#if defined(_WINDOWS)
  return WSAGetLastError();
#else
  return errno;
#endif
}

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

Returns message corresponding to error.

Definition at line 291 of file XmlRpcSocket.cc.

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

std::string XmlRpcSocket::getErrorMsg (  )  [static]
int XmlRpcSocket::getPort ( int  socket  )  [static]

Get the port of a bound socket.

Definition at line 253 of file XmlRpcSocket.cc.

{
  struct sockaddr_in saddr;
  socklen_t saddr_len = sizeof(saddr);
  int port;

  int result = ::getsockname(socket, (sockaddr*) &saddr, &saddr_len);

  if (result != 0) {
    port = -1;
  } else {
    port = ntohs(saddr.sin_port);
  }
  return port;
}

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().

{
  initWinSock();
  return (int) ::socket(AF_INET, SOCK_STREAM, 0);
}

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().

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

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().

{
  const int READ_SIZE = 4096;   // Number of bytes to attempt to read at a time
  char readBuf[READ_SIZE];

  bool wouldBlock = false;
  *eof = false;

  while ( ! wouldBlock && ! *eof) {
#if defined(_WINDOWS)
    int n = recv(fd, readBuf, READ_SIZE-1, 0);
#else
    int n = 0;
    if (ssl != (SSL *) NULL) {
#ifdef USE_SSL
      n = SSL_read(ssl, readBuf, READ_SIZE-1);
#endif
    } else {
      n = read(fd, readBuf, READ_SIZE-1);
    }
#endif
    XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n);

    if (n > 0) {
      readBuf[n] = 0;
      s.append(readBuf, n);
    } else if (n == 0) {
      *eof = true;
    } else if (nonFatalError()) {
      wouldBlock = true;
    } else {
      return false;   // Error
    }
  }
  return true;
}

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().

{
  int nToWrite = int(s.length()) - *bytesSoFar;
  char *sp = const_cast<char*>(s.c_str()) + *bytesSoFar;
  bool wouldBlock = false;

  while ( nToWrite > 0 && ! wouldBlock ) {
#if defined(_WINDOWS)
    int n = send(fd, sp, nToWrite, 0);
#else
    int n = 0;
    if (ssl != (SSL *) NULL) {
#ifdef USE_SSL
      n = SSL_write(ssl, sp, nToWrite);
#endif
    } else {
      n = write(fd, sp, nToWrite);
    }
#endif
    XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n);

    if (n > 0) {
      sp += n;
      *bytesSoFar += n;
      nToWrite -= n;
    } else if (nonFatalError()) {
      wouldBlock = true;
    } else {
      return false;   // Error
    }
  }
  return true;
}

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().

{
  int err = XmlRpcSocket::getError();
  return (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK || err == EINTR);
}

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().

{
#if defined(_WINDOWS)
  unsigned long flag = 1;
  return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0);
#else
  return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
#endif // _WINDOWS
}

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().

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


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