[xiph-commits] r3116 - liboggplay/trunk/src/liboggplay

shans at svn.annodex.net shans at svn.annodex.net
Thu Jun 28 01:27:15 PDT 2007


Author: shans
Date: 2007-06-28 01:27:15 -0700 (Thu, 28 Jun 2007)
New Revision: 3116

Modified:
   liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
Log:
Streamlined tcp reading code a little



Modified: liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-28 08:08:24 UTC (rev 3115)
+++ liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-28 08:27:15 UTC (rev 3116)
@@ -165,33 +165,17 @@
    * the best idea, so we may want to rework it at some time...
    */
   if (me->state == OTRS_CONNECTED) {
+    oggplay_set_socket_blocking_state(me->socket, 1);
     pos = http_request_header;
     len = strlen(http_request_header);
-    START_TIMEOUT(time_ref);
-    while (1) {
 #ifdef WIN32  
-      nbytes = send(me->socket, pos, len, 0);
+    nbytes = send(me->socket, pos, len, 0);
 #else  
-      nbytes = write(me->socket, pos, len);
+    nbytes = write(me->socket, pos, len);
 #endif
-      if (nbytes < 0) {
-#ifdef WIN32
-        /*
-         * INFO: based on the reference found at: http://tangentsoft.net/wskfaq/articles/bsd-compatibility.html
-         * but probably requires some testing
-         */
-        if CHECK_ERROR(EWOULDBLOCK) {
-#else
-        if CHECK_ERROR(EAGAIN) {
-#endif
-          RETURN_ON_TIMEOUT_OR_CONTINUE(time_ref);
-        }
-        return E_OGGPLAY_SOCKET_ERROR;
-      } else if (nbytes >= len) {
-        break;
-      }
-      pos += nbytes;
-      len -= nbytes;
+    assert(nbytes == len);
+    if (nbytes < 0) {
+      return E_OGGPLAY_SOCKET_ERROR;
     }
     me->state = OTRS_SENT_HEADER;
   }
@@ -201,6 +185,7 @@
    */    
   if (me->state == OTRS_SENT_HEADER) {
     int offset;
+    int found_http_response = 0;
 
     if (me->buffer == NULL) {
       me->buffer = (unsigned char*)malloc(TCP_READER_MAX_IN_MEMORY);
@@ -208,12 +193,10 @@
       me->amount_in_memory = 0;
     }
 
-    START_TIMEOUT(time_ref);
-    me->duration = -1;
     while (1) {
       char *dpos;
 
-      remaining = TCP_READER_MAX_IN_MEMORY - me->amount_in_memory;
+      remaining = TCP_READER_MAX_IN_MEMORY - me->amount_in_memory - 1;
 #ifdef WIN32
       nbytes = recv(me->socket, (char*)(me->buffer + me->amount_in_memory), 
               remaining - 1, 0);
@@ -222,13 +205,6 @@
               remaining - 1);
 #endif
       if (nbytes < 0) {
-#ifdef WIN32
-        if CHECK_ERROR(EWOULDBLOCK) {
-#else
-        if CHECK_ERROR(EAGAIN) {
-#endif
-          RETURN_ON_TIMEOUT_OR_CONTINUE(time_ref);
-        }
         return E_OGGPLAY_SOCKET_ERROR;
       } else if (nbytes == 0) {
         /*
@@ -237,8 +213,29 @@
          */
         return E_OGGPLAY_END_OF_FILE;
       }
-      me->buffer[me->amount_in_memory + nbytes] = '\0';
 
+      me->amount_in_memory += nbytes;
+      me->buffer[me->amount_in_memory] = '\0';
+
+      if (me->amount_in_memory < 15) {
+        continue;
+      }
+
+      if 
+      (
+        (!found_http_response)
+        &&
+        strncmp((char *)me->buffer, "HTTP/1.1 200 OK", 15) != 0 
+        &&
+        strncmp((char *)me->buffer, "HTTP/1.0 200 OK", 15) != 0
+      ) 
+      {
+        return E_OGGPLAY_BAD_INPUT;
+      } else {
+        found_http_response = 1;
+        printf("found HTTP response\n");
+      }
+
       dpos = strstr((char *)me->buffer, "X-Content-Duration:");
       if (dpos != NULL) {
         me->duration = (int)(strtod(dpos + 20, NULL) * 1000);
@@ -246,24 +243,14 @@
 
       pos = strstr((char *)(me->buffer), "OggS");
       if (pos != NULL) {
+        printf("found oggz\n");
         break;
       }
-      /* TODO: make this more robust - won't this break on a loop??? */
-      if (strncmp((char *)me->buffer, "HTTP/1.1 200 OK", 15) != 0 &&
-          strncmp((char *)me->buffer, "HTTP/1.0 200 OK", 15) != 0) {
-        return E_OGGPLAY_BAD_INPUT;
-      }
-
-      if (nbytes > 4) {
-        memmove(me->buffer, me->buffer + nbytes - 4, 4);
-        me->amount_in_memory = 4;
-      } else {
-        me->amount_in_memory = nbytes;
-      }
     }
+
     offset = pos - (char *)(me->buffer); 
-    memmove(me->buffer, pos, nbytes - offset);
-    me->amount_in_memory = nbytes - offset;
+    memmove(me->buffer, pos, me->amount_in_memory - offset);
+    me->amount_in_memory -= offset;
     me->backing_store = tmpfile();    
     fwrite(me->buffer, 1, me->amount_in_memory, me->backing_store);    
     me->current_position = 0;
@@ -276,6 +263,10 @@
   /*
    * Read in enough data to fill the buffer.
    */
+  if (!block) {
+    oggplay_set_socket_blocking_state(me->socket, 0);
+  }
+
   if (me->state == OTRS_HTTP_RESPONDED) {
     remaining = TCP_READER_MAX_IN_MEMORY - me->stored_offset;
     START_TIMEOUT(time_ref);



More information about the commits mailing list