Added methos used to connect a socket asynchronously.

git-svn-id: http://voip.null.ro/svn/yate@5489 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2013-04-25 14:19:31 +00:00
parent dd67de4b42
commit bde8329cb6
2 changed files with 57 additions and 0 deletions

View File

@ -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;

View File

@ -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