From bde8329cb611703b29d28adb8921ee52d870e443 Mon Sep 17 00:00:00 2001 From: marian Date: Thu, 25 Apr 2013 14:19:31 +0000 Subject: [PATCH] Added methos used to connect a socket asynchronously. git-svn-id: http://voip.null.ro/svn/yate@5489 acf43c95-373e-0410-b603-e72c3f656dc1 --- engine/Socket.cpp | 33 +++++++++++++++++++++++++++++++++ yateclass.h | 24 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/engine/Socket.cpp b/engine/Socket.cpp index 5936d77e..6575fde7 100644 --- a/engine/Socket.cpp +++ b/engine/Socket.cpp @@ -1405,6 +1405,39 @@ bool Socket::connect(struct sockaddr* addr, socklen_t addrlen) return checkError(::connect(m_handle,addr,addrlen)); } +// Asynchronously connects the socket to a remote address +bool Socket::connectAsync(struct sockaddr* addr, socklen_t addrlen, unsigned int toutUs, bool* timeout) +{ + if (!canSelect()) + return false; + if (connect(addr,addrlen)) + return true; + if (!inProgress()) + return false; + unsigned int intervals = toutUs / Thread::idleUsec(); + // Make sure we wait for at least 1 timeout interval + if (!intervals) + intervals = 1; + clearError(); + while (intervals) { + bool done = false; + bool event = false; + if (!select(0,&done,&event,Thread::idleUsec())) { + if (!error() && (done || event)) + updateError(); + if (!error()) + return true; + return false; + } + if (Thread::check(false)) + return false; + intervals--; + } + if (timeout) + *timeout = true; + return false; +} + bool Socket::shutdown(bool stopReads, bool stopWrites) { int how; diff --git a/yateclass.h b/yateclass.h index 469c3191..23a87416 100644 --- a/yateclass.h +++ b/yateclass.h @@ -5964,6 +5964,30 @@ public: inline bool connect(const SocketAddr& addr) { return connect(addr.address(), addr.length()); } + /** + * Asynchronously connects the socket to a remote address. + * The socket must be selectable and in non-blocking operation mode + * @param addr Address to connect to + * @param addrlen Length of the address structure + * @param toutUs Timeout interval in microseconds + * @param timeout Optional boolean flag to signal timeout + * @return True on success + */ + virtual bool connectAsync(struct sockaddr* addr, socklen_t addrlen, unsigned int toutUs, + bool* timeout = 0); + + /** + * Asynchronously connects the socket to a remote address. + * The socket must be selectable and in non-blocking operation mode + * @param addr Socket address to connect to + * @param toutUs Timeout interval in microseconds + * @param timeout Optional boolean flag to signal timeout + * @return True on success + */ + inline bool connectAsync(const SocketAddr& addr, unsigned int toutUs, + bool* timeout = 0) + { return connectAsync(addr.address(),addr.length(),toutUs,timeout); } + /** * Shut down one or both directions of a full-duplex socket. * @param stopReads Request to shut down the read side of the socket