#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. |
Definition at line 17 of file XmlRpcSocket.h.
int XmlRpcSocket::accept | ( | int | socket | ) | [static] |
Accept a client connection request.
Definition at line 132 of file XmlRpcSocket.cpp.
Referenced by XmlRpc::XmlRpcServer::acceptConnection().
00133 { 00134 struct sockaddr_in addr; 00135 #if defined(_WINDOWS) 00136 int 00137 #else 00138 socklen_t 00139 #endif 00140 addrlen = sizeof(addr); 00141 00142 return (int) ::accept(fd, (struct sockaddr*)&addr, &addrlen); 00143 }
bool XmlRpcSocket::bind | ( | int | socket, | |
int | port | |||
) | [static] |
Bind to a specified port.
Definition at line 112 of file XmlRpcSocket.cpp.
Referenced by XmlRpc::XmlRpcServer::bindAndListen().
00113 { 00114 struct sockaddr_in saddr; 00115 memset(&saddr, 0, sizeof(saddr)); 00116 saddr.sin_family = AF_INET; 00117 saddr.sin_addr.s_addr = htonl(INADDR_ANY); 00118 saddr.sin_port = htons((u_short) port); 00119 return (::bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == 0); 00120 }
void XmlRpcSocket::close | ( | int | socket | ) | [static] |
Closes a socket.
Definition at line 76 of file XmlRpcSocket.cpp.
References XmlRpc::XmlRpcUtil::log().
Referenced by XmlRpc::XmlRpcServer::acceptConnection(), and XmlRpc::XmlRpcSource::close().
00077 { 00078 XmlRpcUtil::log(4, "XmlRpcSocket::close: fd %d.", fd); 00079 #if defined(_WINDOWS) 00080 closesocket(fd); 00081 #else 00082 ::close(fd); 00083 #endif // _WINDOWS 00084 }
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.
References nonFatalError().
Referenced by XmlRpc::XmlRpcClient::doConnect().
00150 { 00151 struct sockaddr_in saddr; 00152 memset(&saddr, 0, sizeof(saddr)); 00153 saddr.sin_family = AF_INET; 00154 00155 struct hostent *hp = gethostbyname(host.c_str()); 00156 if (hp == 0) return false; 00157 00158 saddr.sin_family = hp->h_addrtype; 00159 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length); 00160 saddr.sin_port = htons((u_short) port); 00161 00162 // For asynch operation, this will return EWOULDBLOCK (windows) or 00163 // EINPROGRESS (linux) and we just need to wait for the socket to be writable... 00164 int result = ::connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); 00165 return result == 0 || nonFatalError(); 00166 }
int XmlRpcSocket::getError | ( | ) | [static] |
Returns last errno.
Definition at line 235 of file XmlRpcSocket.cpp.
Referenced by getErrorMsg().
00236 { 00237 #if defined(_WINDOWS) 00238 return WSAGetLastError(); 00239 #else 00240 return errno; 00241 #endif 00242 }
std::string XmlRpcSocket::getErrorMsg | ( | int | error | ) | [static] |
Returns message corresponding to error.
Definition at line 254 of file XmlRpcSocket.cpp.
References showlog::err, and deljobs::string.
00255 { 00256 char err[60]; 00257 snprintf(err,sizeof(err),"error %d", error); 00258 return std::string(err); 00259 }
std::string XmlRpcSocket::getErrorMsg | ( | ) | [static] |
Returns message corresponding to last error.
Definition at line 247 of file XmlRpcSocket.cpp.
References getError().
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().
00248 { 00249 return getErrorMsg(getError()); 00250 }
bool XmlRpcSocket::listen | ( | int | socket, | |
int | backlog | |||
) | [static] |
Set socket in listen mode.
Definition at line 125 of file XmlRpcSocket.cpp.
Referenced by XmlRpc::XmlRpcServer::bindAndListen().
00126 { 00127 return (::listen(fd, backlog) == 0); 00128 }
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.
References XmlRpc::XmlRpcUtil::log(), and nonFatalError().
Referenced by XmlRpc::XmlRpcServerConnection::readHeader(), XmlRpc::XmlRpcClient::readHeader(), XmlRpc::XmlRpcServerConnection::readRequest(), and XmlRpc::XmlRpcClient::readResponse().
00173 { 00174 const int READ_SIZE = 4096; // Number of bytes to attempt to read at a time 00175 char readBuf[READ_SIZE]; 00176 00177 bool wouldBlock = false; 00178 *eof = false; 00179 00180 while ( ! wouldBlock && ! *eof) { 00181 #if defined(_WINDOWS) 00182 int n = recv(fd, readBuf, READ_SIZE-1, 0); 00183 #else 00184 int n = read(fd, readBuf, READ_SIZE-1); 00185 #endif 00186 XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n); 00187 00188 if (n > 0) { 00189 readBuf[n] = 0; 00190 s.append(readBuf, n); 00191 } else if (n == 0) { 00192 *eof = true; 00193 } else if (nonFatalError()) { 00194 wouldBlock = true; 00195 } else { 00196 return false; // Error 00197 } 00198 } 00199 return true; 00200 }
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.
References XmlRpc::XmlRpcUtil::log(), and nonFatalError().
Referenced by XmlRpc::XmlRpcClient::writeRequest(), and XmlRpc::XmlRpcServerConnection::writeResponse().
00206 { 00207 int nToWrite = int(s.length()) - *bytesSoFar; 00208 char *sp = const_cast<char*>(s.c_str()) + *bytesSoFar; 00209 bool wouldBlock = false; 00210 00211 while ( nToWrite > 0 && ! wouldBlock ) { 00212 #if defined(_WINDOWS) 00213 int n = send(fd, sp, nToWrite, 0); 00214 #else 00215 int n = write(fd, sp, nToWrite); 00216 #endif 00217 XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n); 00218 00219 if (n > 0) { 00220 sp += n; 00221 *bytesSoFar += n; 00222 nToWrite -= n; 00223 } else if (nonFatalError()) { 00224 wouldBlock = true; 00225 } else { 00226 return false; // Error 00227 } 00228 } 00229 return true; 00230 }
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.
Referenced by XmlRpc::XmlRpcServer::acceptConnection(), XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::doConnect().
00091 { 00092 #if defined(_WINDOWS) 00093 unsigned long flag = 1; 00094 return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0); 00095 #else 00096 return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0); 00097 #endif // _WINDOWS 00098 }
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.
Referenced by XmlRpc::XmlRpcServer::bindAndListen().
00103 { 00104 // Allow this port to be re-bound immediately so server re-starts are not delayed 00105 int sflag = 1; 00106 return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&sflag, sizeof(sflag)) == 0); 00107 }
int XmlRpcSocket::socket | ( | ) | [static] |
Creates a stream (TCP) socket. Returns -1 on failure.
Definition at line 68 of file XmlRpcSocket.cpp.
References initWinSock.
Referenced by XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::doConnect().
00069 { 00070 initWinSock(); 00071 return (int) ::socket(AF_INET, SOCK_STREAM, 0); 00072 }