Dear Sir:<br><br>connect() in RTSPClient::openConnectionFromURL() is blocking. A practical application may not accept such a behavior. The following functions provide connect() with timeout setting on Windows(maybe work on Linux). Not very elegant but work. Please take them into consideration.<br>
<br>Boolean makeSocketBlocking(int sock) {<br>#if defined(__WIN32__) || defined(_WIN32) || defined(IMN_PIM)<br> unsigned long arg = 0;<br> return ioctlsocket(sock, FIONBIO, &arg) == 0;<br>#elif defined(VXWORKS)<br> int arg = 1;<br>
return ioctl(sock, FIONBIO, (int)&arg) == 0;<br>#else<br> int curFlags = fcntl(sock, F_GETFL, 0);<br> return fcntl(sock, F_SETFL, curFlags|O_NONBLOCK) >= 0;<br>#endif<br>}<br><br>int connect(SOCKET sock, const sockaddr *name, int namelen, int timeout)<br>
{<br> if(!makeSocketNonBlocking(sock))<br> {<br> fprintf("[connect()] fail to set socket non-blocking\n");<br> return -1;<br> }<br><br> struct timeval tv;<br> tv.tv_sec = timeout;<br>
tv.tv_usec = 0;<br> fd_set rfds, wfds;<br> FD_ZERO(&rfds);<br> int ret = connect(sock, name, namelen);<br><br> do<br> {<br> if(ret == SOCKET_ERROR)<br> {<br>#if defined(__WIN32__) || defined(_WIN32) || defined(IMN_PIM)<br>
if(WSAGetLastError() != WSAEWOULDBLOCK)<br> {<br> fprintf(stderr, "[connect()] WSAGetLastError() %d\n", WSAGetLastError());<br> ret = -1;<br> break;<br>
}<br> else // need select<br>#endif<br> {<br> FD_SET(sock, &rfds);<br> wfds = rfds;<br> ret = select(sock + 1, &rfds, &wfds, 0, &tv);<br>
if(ret == 0)<br> {<br> fprintf(stderr, "[connect()] timeout in %ds\n", timeout);<br> ret = -1;<br> break;<br> }<br>
<br> if(FD_ISSET(sock, &rfds) || FD_ISSET(sock, &wfds))<br> {<br> int error;<br> int size = sizeof(error);<br> if(getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&error, &size) != 0 || error != 0)<br>
{<br> fprintf(stderr, "[connect()] getsockopt() error %d\n", error);<br> ret = -1;<br> break;<br> }<br>
}<br> else<br> {<br> fprintf(stderr, "[connect()] unknown error\n");<br> ret = -1;<br> break;<br> }<br>
}<br> ret = 0;<br> } <br> else<br> ret = 0;<br> } while(false);<br><br> if(!makeSocketBlocking(sock))<br> {<br> fprintf("[connect()] fail to set socket blocking\n");<br>
ret = -1;<br> }<br> return ret;<br>}