]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Add esl_connect_timeout to specify custom connection timeouts
authorMathieu Rene <mrene@avgs.ca>
Tue, 20 Jul 2010 18:42:43 +0000 (14:42 -0400)
committerMathieu Rene <mrene@avgs.ca>
Tue, 20 Jul 2010 18:45:53 +0000 (14:45 -0400)
libs/esl/src/esl.c
libs/esl/src/include/esl.h

index abfe9eb1a86f3ab4924db7bac8f4799d28432d84..c63f2b1a00499d5b10fdda4d591f6e8a9d28a5d1 100644 (file)
@@ -34,6 +34,7 @@
 #include <esl.h>
 #ifndef WIN32
 #define closesocket(x) close(x)
+#include <fcntl.h>
 #else
 #include <Ws2tcpip.h>
 #endif
@@ -606,12 +607,13 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
 
 }
 
-ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password)
+ESL_DECLARE(esl_status_t) esl_connect_timeout(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password, long timeout)
 {
        char sendbuf[256];
        int rval = 0;
        const char *hval;
        struct addrinfo hints = { 0 }, *result;
+       int fd_flags;
 #ifdef WIN32
        WORD wVersionRequested = MAKEWORD(2, 0);
        WSADATA wsaData;
@@ -647,8 +649,58 @@ ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, es
        handle->sockaddr.sin_family = AF_INET;
        handle->sockaddr.sin_port = htons(port);
 
+       if (timeout != -1) {
+#ifdef WIN32
+               u_long arg = 1;
+               if (ioctlsocket(handle->sock, FIONBIO, &arg) == SOCKET_ERROR) {
+                       snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
+                       goto fail;
+               }
+#else
+               fd_flags = fcntl(handle->sock, F_GETFL, 0);
+               if (fcntl(handle->sock, F_SETFL, fd_flags | O_NONBLOCK)) {
+                       snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
+                       goto fail;
+               }
+#endif
+       }
+
        rval = connect(handle->sock, (struct sockaddr*)&handle->sockaddr, sizeof(handle->sockaddr));
        
+       if (timeout != -1) {
+               fd_set wfds;
+               struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
+               int r;
+
+               FD_ZERO(&wfds);
+        FD_SET(handle->sock, &wfds);
+
+        r = select(handle->sock + 1, NULL, &wfds, NULL, &tv);
+               
+               if (r <= 0) {
+                       snprintf(handle->err, sizeof(handle->err), "Connection timed out");
+                       goto fail;
+               }
+
+               if (!FD_ISSET(handle->sock, &wfds)) {
+                       snprintf(handle->err, sizeof(handle->err), "Connection timed out");
+                       goto fail;
+               }
+
+#ifdef WIN32
+               {
+                       u_long arg = 0;
+                       if (ioctlsocket(handle->sock, FIONBIO, &arg) == SOCKET_ERROR) {
+                               snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
+                               goto fail;
+                       }
+               }
+#else
+               fcntl(handle->sock, F_SETFL, fd_flags);
+#endif 
+               rval = 0;
+       }
+       
        freeaddrinfo(result);
        result = NULL;
        
index 7b98988accea48a4e656c3e50a6ebc24d6dfd3ca..d87f83f26bec7418a9be5c87045dbbbc7885f48d 100644 (file)
@@ -387,8 +387,11 @@ ESL_DECLARE(esl_status_t) esl_sendevent(esl_handle_t *handle, esl_event_t *event
     \param port Port to be connected
     \param password FreeSWITCH server username (optional)
     \param password FreeSWITCH server password
+       \param timeout Connection timeout, in miliseconds
 */
-ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password);
+ESL_DECLARE(esl_status_t) esl_connect_timeout(esl_handle_t *handle, const char *host, esl_port_t port, const char *user, const char *password, long timeout);
+#define esl_connect(_handle, _host, _port, _user, _password) esl_connect_timeout(_handle, _host, _port, _user, _password, -1)
+
 /*!
     \brief Disconnect a handle
     \param handle Handle to be disconnected