From dd3546ee408dd7af71a78f0c59118d36a1007a46 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Mon, 17 Feb 2014 14:21:47 +0100 Subject: [PATCH] Resolve socket bug under Windows. --- lib/BasicsC/socket-utils.h | 11 +++++++++-- lib/GeneralServer/GeneralCommTask.h | 4 ++-- lib/GeneralServer/GeneralSslServer.h | 2 +- lib/Scheduler/SchedulerLibev.cpp | 5 ++++- lib/SimpleHttpClient/ClientConnection.cpp | 4 ++-- lib/SimpleHttpClient/SslClientConnection.cpp | 6 +++--- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/BasicsC/socket-utils.h b/lib/BasicsC/socket-utils.h index 0b62da3883..46682419f4 100644 --- a/lib/BasicsC/socket-utils.h +++ b/lib/BasicsC/socket-utils.h @@ -251,11 +251,18 @@ static inline void TRI_invalidatesocket (TRI_socket_t* socket) { } //////////////////////////////////////////////////////////////////////////////// -/// @brief get file descriptor +/// @brief get file descriptor or handle, depending on OS +/// +/// Note that this returns the fileHandle under Windows which is exactly +/// the right thing we need in all but one places. //////////////////////////////////////////////////////////////////////////////// -static inline int TRI_get_fd_of_socket (TRI_socket_t socket) { +static inline int TRI_get_fd_or_handle_of_socket (TRI_socket_t socket) { +#ifdef _WIN32 + return socket.fileHandle; +#else return socket.fileDescriptor; +#endif } //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/GeneralServer/GeneralCommTask.h b/lib/GeneralServer/GeneralCommTask.h index 5c16f1abf7..47651e6c5d 100644 --- a/lib/GeneralServer/GeneralCommTask.h +++ b/lib/GeneralServer/GeneralCommTask.h @@ -103,7 +103,7 @@ namespace triagens { _maximalHeaderSize(0), _maximalBodySize(0) { LOG_TRACE("connection established, client %d, server ip %s, server port %d, client ip %s, client port %d", - TRI_get_fd_of_socket(socket), + TRI_get_fd_or_handle_of_socket(socket), _connectionInfo.serverAddress.c_str(), (int) _connectionInfo.serverPort, _connectionInfo.clientAddress.c_str(), @@ -123,7 +123,7 @@ namespace triagens { protected: ~GeneralCommTask () { - LOG_TRACE("connection closed, client %d", (int) TRI_get_fd_of_socket(_commSocket)); + LOG_TRACE("connection closed, client %d", (int) TRI_get_fd_or_handle_of_socket(_commSocket)); // free write buffers for (deque::iterator i = _writeBuffers.begin(); i != _writeBuffers.end(); i++) { diff --git a/lib/GeneralServer/GeneralSslServer.h b/lib/GeneralServer/GeneralSslServer.h index 0d88a94a88..17decbeedb 100644 --- a/lib/GeneralServer/GeneralSslServer.h +++ b/lib/GeneralServer/GeneralSslServer.h @@ -276,7 +276,7 @@ namespace triagens { LOG_DEBUG("trying to establish secure connection"); // convert in a SSL BIO structure - BIO * sbio = BIO_new_socket(TRI_get_fd_of_socket(socket), BIO_NOCLOSE); + BIO * sbio = BIO_new_socket(TRI_get_fd_or_handle_of_socket(socket), BIO_NOCLOSE); if (sbio == 0) { LOG_WARNING("cannot build new SSL BIO: %s", triagens::basics::lastSSLError().c_str()); diff --git a/lib/Scheduler/SchedulerLibev.cpp b/lib/Scheduler/SchedulerLibev.cpp index acbd4add81..d83f501b83 100644 --- a/lib/Scheduler/SchedulerLibev.cpp +++ b/lib/Scheduler/SchedulerLibev.cpp @@ -579,7 +579,10 @@ EventToken SchedulerLibev::installSocketEvent (EventLoop loop, EventType type, T } ev_io* w = (ev_io*) watcher; - ev_io_init(w, socketCallback, TRI_get_fd_of_socket(socket), flags); + // Note that we do not use TRI_get_fd_or_handle_of_socket here because even + // under Windows we want get the entry fileDescriptor here because + // of the reason that is mentioned above! + ev_io_init(w, socketCallback, socket.fileDescriptor, flags); ev_io_start(watcher->loop, w); return watcher->token; diff --git a/lib/SimpleHttpClient/ClientConnection.cpp b/lib/SimpleHttpClient/ClientConnection.cpp index a9a8cb61e9..24183f7285 100644 --- a/lib/SimpleHttpClient/ClientConnection.cpp +++ b/lib/SimpleHttpClient/ClientConnection.cpp @@ -177,7 +177,7 @@ bool ClientConnection::prepare (const double timeout, const bool isWrite) const tv.tv_usec = ((long) (timeout * 1000000.0)) % 1000000; FD_ZERO(&fdset); - FD_SET(TRI_get_fd_of_socket(_socket), &fdset); + FD_SET(TRI_get_fd_or_handle_of_socket(_socket), &fdset); fd_set* readFds = NULL; fd_set* writeFds = NULL; @@ -189,7 +189,7 @@ bool ClientConnection::prepare (const double timeout, const bool isWrite) const readFds = &fdset; } - int sockn = (int) (TRI_get_fd_of_socket(_socket) + 1); + int sockn = (int) (TRI_get_fd_or_handle_of_socket(_socket) + 1); int res = select(sockn, readFds, writeFds, NULL, &tv); if (res > 0) { diff --git a/lib/SimpleHttpClient/SslClientConnection.cpp b/lib/SimpleHttpClient/SslClientConnection.cpp index 3073a717b9..8c440c094a 100644 --- a/lib/SimpleHttpClient/SslClientConnection.cpp +++ b/lib/SimpleHttpClient/SslClientConnection.cpp @@ -162,7 +162,7 @@ bool SslClientConnection::connectSocket () { return false; } - if (SSL_set_fd(_ssl, TRI_get_fd_of_socket(_socket)) != 1) { + if (SSL_set_fd(_ssl, TRI_get_fd_or_handle_of_socket(_socket)) != 1) { _endpoint->disconnect(); SSL_free(_ssl); _ssl = 0; @@ -210,7 +210,7 @@ bool SslClientConnection::prepare (const double timeout, const bool isWrite) con tv.tv_usec = ((long) (timeout * 1000000.0)) % 1000000; FD_ZERO(&fdset); - FD_SET(TRI_get_fd_of_socket(_socket), &fdset); + FD_SET(TRI_get_fd_or_handle_of_socket(_socket), &fdset); fd_set* readFds = NULL; fd_set* writeFds = NULL; @@ -222,7 +222,7 @@ bool SslClientConnection::prepare (const double timeout, const bool isWrite) con readFds = &fdset; } - int sockn = (int) (TRI_get_fd_of_socket(_socket) + 1); + int sockn = (int) (TRI_get_fd_or_handle_of_socket(_socket) + 1); if (select(sockn, readFds, writeFds, NULL, &tv) > 0) { return true; }