[rtmpdump] [PATCH] Non-blocking Connection

Chris Larsen clarsen at euphoriaaudio.com
Fri Sep 3 21:49:37 CEST 2010


Hello, we had a problem where the connection was taking much too long to
timeout, so I configured the socket as non-blocking using the global RTMP
timeout settings so that we could ditch the connection quicker if a host
doesn't exist. I test it on Windows and Ubuntu so take a look and let me
know what ya'll think, thanks!

 

Index: rtmp.c

===================================================================

--- rtmp.c             (revision 548)

+++ rtmp.c          (working copy)

@@ -800,6 +800,7 @@

RTMP_Connect0(RTMP *r, struct sockaddr * service)

{

   int on = 1;

+  int sflags;

   r->m_sb.sb_timedout = FALSE;

   r->m_pausing = 0;

   r->m_fDuration = 0.0;

@@ -807,15 +808,44 @@

   r->m_sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

   if (r->m_sb.sb_socket != -1)

     {

-      if (connect(r->m_sb.sb_socket, service, sizeof(struct sockaddr)) < 0)

+                             // set non-blocking

+#ifdef WIN32

+             u_long nonbon = 1;

+             ioctlsocket(r->m_sb.sb_socket, FIONBIO, &nonbon);

+#else

+             sflags = fcntl(r->m_sb.sb_socket, F_GETFL, 0);              //
Get socket flags

+             fcntl(r->m_sb.sb_socket, F_SETFL, sflags | O_NONBLOCK);
// Add non-blocking flag

+#endif

+int ret;

+      if ((ret = connect(r->m_sb.sb_socket, service, sizeof(struct
sockaddr))) < 0)

               {

                 int err = GetSockError();

-                RTMP_Log(RTMP_LOGERROR, "%s, failed to connect socket. %d
(%s)",

-                    __FUNCTION__, err, strerror(err));

-                RTMP_Close(r);

-                return FALSE;

+               if (err != 10035 && err != 115){

+                               RTMP_Log(RTMP_LOGERROR, "%s, failed to
connect socket. %d (%s) %d",

+                                               __FUNCTION__, err,
strerror(err), ret);

+                               RTMP_Close(r);

+                               return FALSE;

+               }

               }

+             // wait for data

+             fd_set rset, wset;

+             struct timeval  tval;

+             FD_ZERO(&rset);

+    FD_SET(r->m_sb.sb_socket, &rset);

+                wset = rset;

+    tval.tv_sec = r->Link.timeout;

+    tval.tv_usec = 0;

+

+             int n;

+             if ( (n = select(r->m_sb.sb_socket+1, &rset, &wset, NULL,
&tval)) == 0) {

+                             close(r->m_sb.sb_socket);

+                               RTMP_Log(RTMP_LOGERROR, "%s, socket
connection timedout after %ds.", __FUNCTION__, r->Link.timeout);

+                   RTMP_Close(r);

+                   return FALSE;

+    }

+

+

       if (r->Link.socksport)

               {

                 RTMP_Log(RTMP_LOGDEBUG, "%s ... SOCKS negotiation",
__FUNCTION__);

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mplayerhq.hu/pipermail/rtmpdump/attachments/20100903/34d0f404/attachment.htm>


More information about the rtmpdump mailing list