[xiph-commits] r3115 - in liboggplay/trunk: include/oggplay src/liboggplay

shans at svn.annodex.net shans at svn.annodex.net
Thu Jun 28 01:08:24 PDT 2007


Author: shans
Date: 2007-06-28 01:08:24 -0700 (Thu, 28 Jun 2007)
New Revision: 3115

Modified:
   liboggplay/trunk/include/oggplay/oggplay_tools.h
   liboggplay/trunk/src/liboggplay/Version_script.in
   liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
   liboggplay/trunk/src/liboggplay/oggplay_tools.c
Log:
factored out some of the TCP code into oggplay_tools.c



Modified: liboggplay/trunk/include/oggplay/oggplay_tools.h
===================================================================
--- liboggplay/trunk/include/oggplay/oggplay_tools.h	2007-06-28 06:26:14 UTC (rev 3114)
+++ liboggplay/trunk/include/oggplay/oggplay_tools.h	2007-06-28 08:08:24 UTC (rev 3115)
@@ -46,6 +46,22 @@
 extern "C" {
 #endif
 
+#ifdef WIN32
+#include <sys/time.h>
+#include <process.h>
+#include <io.h>
+#else
+#include <sys/types.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/fcntl.h>
+#include <sys/errno.h>
+#include <sys/time.h>
+#include <time.h>
+#include <netdb.h>
+#include <unistd.h>
+#endif
+
 /* structure holds pointers to y, u, v channels */
 typedef struct _OggPlayYUVChannels {
     unsigned char * ptry;
@@ -76,6 +92,45 @@
 void
 oggplay_millisleep(long ms);
 
+#ifndef WIN32
+typedef int SOCKET;
+#define INVALID_SOCKET  -1
+#endif
+
+int
+oggplay_set_socket_blocking_state(SOCKET socket, int is_blocking);
+
+SOCKET
+oggplay_create_socket();
+
+void
+oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+                                  char **host, int *port, char **path);
+
+OggPlayErrorCode
+oggplay_connect_to_host(SOCKET socket, struct sockaddr *addr);
+
+#define START_TIMEOUT(ref) \
+  (ref) = oggplay_sys_time_in_ms()
+
+#ifdef WIN32 
+#define CHECK_ERROR(error) \
+  (WSAGetLastError() == WSA##error)
+#else
+#define CHECK_ERROR(error) \
+  (errno == error)
+#endif
+
+#define RETURN_ON_TIMEOUT_OR_CONTINUE(ref)        \
+  if (oggplay_sys_time_in_ms() - (ref) > 500) {   \
+    return E_OGGPLAY_TIMEOUT;                     \
+  } else {                                        \
+    oggplay_millisleep(10);                       \
+    continue;                                     \
+  }
+
+
+
 #ifdef __cplusplus
 }
 #endif

Modified: liboggplay/trunk/src/liboggplay/Version_script.in
===================================================================
--- liboggplay/trunk/src/liboggplay/Version_script.in	2007-06-28 06:26:14 UTC (rev 3114)
+++ liboggplay/trunk/src/liboggplay/Version_script.in	2007-06-28 08:08:24 UTC (rev 3115)
@@ -58,6 +58,10 @@
 
                 oggplay_sys_time_in_ms;
                 oggplay_millisleep;
+                oggplay_set_blocking_state;
+                oggplay_create_socket;
+                oggplay_hostname_and_path;
+                oggplay_connect_to_host;
 
                 oggplay_seek;
                 oggplay_get_available;

Modified: liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-28 06:26:14 UTC (rev 3114)
+++ liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-28 08:08:24 UTC (rev 3115)
@@ -43,7 +43,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef _WIN32
+#ifdef WIN32
 #include <process.h>
 #include <io.h>
 
@@ -61,49 +61,11 @@
 
 #include <assert.h>
 
-#ifndef WIN32
-#define INVALID_SOCKET  -1
-#endif
-
 #define PRINT_BUFFER(s,m) \
     printf("%s: in_mem: %d size: %d pos: %d stored: %d\n", \
             s, m->amount_in_memory, m->buffer_size, \
             m->current_position, m->stored_offset);
 
-
-static int
-set_socket_blocking_state(OggPlayTCPReader *me, int is_blocking) {
-#ifdef WIN32
-  u_long  io_mode = !is_blocking;
-  if (ioctlsocket(me->socket, FIONBIO, &io_mode) == SOCKET_ERROR) {
-#else
-  if (fcntl(me->socket, F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
-#endif
-    return 0;
-  }
-  return 1;
-}
-
-
-#define START_TIMEOUT(ref) \
-  (ref) = oggplay_sys_time_in_ms()
-
-#ifdef WIN32 
-#define CHECK_ERROR(error) \
-  (WSAGetLastError() == WSA##error)
-#else
-#define CHECK_ERROR(error) \
-  (errno == error)
-#endif
-
-#define RETURN_ON_TIMEOUT_OR_CONTINUE(ref)        \
-  if (oggplay_sys_time_in_ms() - (ref) > 500) {   \
-    return E_OGGPLAY_TIMEOUT;                     \
-  } else {                                        \
-    oggplay_millisleep(10);                       \
-    continue;                                     \
-  }
-
 OggPlayErrorCode
 oggplay_tcp_reader_initialise(OggPlayReader * opr, int block) {
 
@@ -112,20 +74,16 @@
   struct sockaddr_in    addr;
   char                * host;
   char                * path;
-  char                * colon;
-  char                * pos;
   int                   port;
-  int                   len;
   int                   nbytes;
-  int                   has_http;
   int                   remaining;
   char                  http_request_header[1024];
   ogg_int64_t           time_ref;
-#ifdef WIN32	
-  WORD                  wVersionRequested;
-  WSADATA               wsaData;
-#endif
+  int                   r;
 
+  char                * pos;
+  int                   len;
+
   if (me == NULL) {
     return E_OGGPLAY_BAD_READER;
   }
@@ -138,29 +96,9 @@
    */
   if (me->state == OTRS_UNINITIALISED) {
     assert(me->socket == INVALID_SOCKET);
-#ifdef WIN32	
-#ifdef HAVE_WINSOCK2        
-    wVersionRequested = MAKEWORD(2,2);
-#else
-    wVersionRequested = MAKEWORD(1,1);
-#endif    
-    if (WSAStartup(wVersionRequested, &wsaData) == -1) {
-      printf("Socket open error\n");
-      return E_OGGPLAY_SOCKET_ERROR;
-    }
-    if (wsaData.wVersion != wVersionRequested) {
-      printf("Incorrect winsock version [%d]\n", wVersionRequested);
-      WSACleanup();
-      return E_OGGPLAY_SOCKET_ERROR;
-    }
-#endif
 
-    me->socket = socket(PF_INET, SOCK_STREAM, 0);
+    me->socket = oggplay_create_socket();
     if (me->socket == INVALID_SOCKET) {
-      printf("Could not create socket\n");
-#ifdef WIN32
-      WSACleanup();
-#endif
       return E_OGGPLAY_SOCKET_ERROR;
     }
 
@@ -172,7 +110,7 @@
    * timeout and return control to the caller.
    */
   if (!block) {
-    if (!set_socket_blocking_state(me, 0)) {
+    if (!oggplay_set_socket_blocking_state(me->socket, 0)) {
       return E_OGGPLAY_SOCKET_ERROR;
     }
   }
@@ -180,63 +118,10 @@
   /*
    * Extract the host name and the path from the location.
    */
+  oggplay_hostname_and_path(me->location, me->proxy, me->proxy_port,
+                              &host, &port, &path);
 
-  if (me->proxy == NULL) {
-    char *_location;
-    len = strlen(me->location);
-    _location = (char*)malloc(len + 2);
-    strcpy(_location, me->location);
 
-    if (strncmp(_location, "http://", 7) == 0) {
-      path = strchr(_location + 7, '/');
-      colon = strchr(_location + 7, ':');
-      has_http = 1;
-    } else {
-      path = strchr(_location, '/');
-      colon = strchr(_location, ':');
-      has_http = 0;
-    }
-
-    port = 80;
-    if (colon != NULL) {
-      if (path == NULL || colon < path) {
-        port = (int)strtol(colon+1, NULL, 10);
-        if (path != NULL) {
-          strcpy(colon, me->location + (path - _location));
-          path = colon;
-        }
-        else {
-          *colon = '\0';
-          path = NULL;
-        }
-      }
-    }
-        
-
-    if (path == NULL) {
-      _location[len] = '/';
-      _location[len + 1] = '\0';
-      path = _location + len;
-    }
-
-    if (has_http) {
-      host = (char*)malloc((path - _location) - 6);
-      strncpy(host, _location + 7, (path - _location) - 7);
-      host[(path - _location) - 7] = '\0';
-    } else {
-      host = (char*)malloc((path - _location) + 1);
-      strncpy(host, _location, path - _location);
-      host[path - _location] = '\0';
-    }
-    free(_location);
-  }
-  else
-  {
-    host = me->proxy;
-    port = me->proxy_port;
-    path = me->location;
-  }
-
   /*
    * Prepare the HTTP request header now, so we can free all our
    * allocated memory before returning on any errors.
@@ -249,9 +134,8 @@
     "Connection: Keep-Alive\n\n", path, host);
 
   he = gethostbyname(host);
-  if (me->proxy == NULL) {
-    free(host);
-  }
+  free(host);
+  free(path);
   
   if (he == NULL) {
     printf("Host not found\n");
@@ -265,21 +149,11 @@
    * Connect to the host.
    */
   if (me->state == OTRS_SOCKET_CREATED) {
-    START_TIMEOUT(time_ref);
-    while (connect(me->socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-    if (CHECK_ERROR(EINPROGRESS) || CHECK_ERROR(EALREADY)
-#ifdef WIN32
-          /* see http://msdn2.microsoft.com/en-us/library/ms737625.aspx */
-          || CHECK_ERROR(EWOULDBLOCK) || CHECK_ERROR(EINVAL)
-#endif      
-      ) {
-        RETURN_ON_TIMEOUT_OR_CONTINUE(time_ref);
-      } else if CHECK_ERROR(EISCONN) {
-        break;
-      }
-      printf("Could not connect to host; error code is %d\n", errno);
-      return CHECK_ERROR(ETIMEDOUT) ? E_OGGPLAY_TIMEOUT : E_OGGPLAY_SOCKET_ERROR;
+    r = oggplay_connect_to_host(me->socket, (struct sockaddr *)&addr);
+    if (r != E_OGGPLAY_OK) {
+      return r;
     }
+
     me->state = OTRS_CONNECTED;
   }
 
@@ -439,7 +313,7 @@
   /*
    * Set the socket back to blocking mode.
    */
-  if (!set_socket_blocking_state(me, 1)) {
+  if (!oggplay_set_socket_blocking_state(me->socket, 1)) {
     return E_OGGPLAY_SOCKET_ERROR;
   }
 

Modified: liboggplay/trunk/src/liboggplay/oggplay_tools.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_tools.c	2007-06-28 06:26:14 UTC (rev 3114)
+++ liboggplay/trunk/src/liboggplay/oggplay_tools.c	2007-06-28 08:08:24 UTC (rev 3115)
@@ -39,13 +39,8 @@
 
 
 #include "oggplay_private.h"
+#include <string.h>
 
-#ifndef WIN32
-#include <sys/time.h>
-#endif
-
-#include <time.h>
-
 ogg_int64_t
 oggplay_sys_time_in_ms(void) {
 #ifdef WIN32
@@ -68,3 +63,153 @@
   nanosleep(&ts, NULL);
 #endif
 }
+
+#ifdef WIN32
+int
+oggplay_set_socket_blocking_state(SOCKET socket, int is_blocking) {
+  u_long  io_mode = !is_blocking;
+  if (ioctlsocket(socket, FIONBIO, &io_mode) == SOCKET_ERROR) {
+     return 0;
+  }
+  return 1;
+}
+#else
+int
+oggplay_set_socket_blocking_state(SOCKET socket, int is_blocking) {
+ if (fcntl(socket, F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
+    return 0;
+  }
+  return 1;
+}
+#endif
+
+SOCKET
+oggplay_create_socket() {
+  SOCKET sock;
+
+#ifdef WIN32	
+  WORD                  wVersionRequested;
+  WSADATA               wsaData;
+#ifdef HAVE_WINSOCK2        
+  wVersionRequested = MAKEWORD(2,2);
+#else
+  wVersionRequested = MAKEWORD(1,1);
+#endif    
+  if (WSAStartup(wVersionRequested, &wsaData) == -1) {
+    printf("Socket open error\n");
+    return INVALID_SOCKET;
+  }
+  if (wsaData.wVersion != wVersionRequested) {
+    printf("Incorrect winsock version [%d]\n", wVersionRequested);
+    WSACleanup();
+    return INVALID_SOCKET;
+  }
+#endif
+
+  sock = socket(PF_INET, SOCK_STREAM, 0);
+  if (sock == INVALID_SOCKET) {
+    printf("Could not create socket\n");
+#ifdef WIN32
+    WSACleanup();
+#endif
+    return INVALID_SOCKET;
+  }
+
+  return sock;
+}
+
+void
+oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+                                  char **host, int *port, char **path) {
+
+  char                * colon;
+  int                   len;
+  int                   has_http;
+
+  if (proxy == NULL) {
+    char *_location;
+    len = strlen(location);
+    _location = (char*)malloc(len + 2);
+    strcpy(_location, location);
+
+    if (strncmp(_location, "http://", 7) == 0) {
+      *path = strchr(_location + 7, '/');
+      colon = strchr(_location + 7, ':');
+      has_http = 1;
+    } else {
+      *path = strchr(_location, '/');
+      colon = strchr(_location, ':');
+      has_http = 0;
+    }
+
+    *port = 80;
+    if (colon != NULL) {
+      if (*path == NULL || colon < *path) {
+        *port = (int)strtol(colon+1, NULL, 10);
+        if (*path != NULL) {
+          strcpy(colon, location + (*path - _location));
+          *path = strdup(colon);
+        }
+        else {
+          *colon = '\0';
+          *path = NULL;
+        }
+      }
+    }
+        
+
+    if (*path == NULL) {
+      _location[len] = '/';
+      _location[len + 1] = '\0';
+      *path = strdup(_location + len);
+    }
+
+    if (has_http) {
+      *host = (char*)malloc((*path - _location) - 6);
+      strncpy(*host, _location + 7, (*path - _location) - 7);
+      (*host)[(*path - _location) - 7] = '\0';
+    } else {
+      *host = (char*)malloc((*path - _location) + 1);
+      strncpy(*host, _location, *path - _location);
+      (*host)[*path - _location] = '\0';
+    }
+    free(_location);
+  }
+  else
+  {
+    *host = strdup(proxy);
+    *port = proxy_port;
+    *path = strdup(location);
+  }
+
+}
+
+OggPlayErrorCode
+oggplay_connect_to_host(SOCKET socket, struct sockaddr *addr) {
+
+  ogg_int64_t           time_ref;
+
+  START_TIMEOUT(time_ref);
+
+  while (connect(socket, addr, sizeof(struct sockaddr_in)) < 0) {
+    if (
+        CHECK_ERROR(EINPROGRESS) || CHECK_ERROR(EALREADY)
+#ifdef WIN32
+          /* see http://msdn2.microsoft.com/en-us/library/ms737625.aspx */
+        || CHECK_ERROR(EWOULDBLOCK) || CHECK_ERROR(EINVAL)
+#endif      
+    ) {
+      RETURN_ON_TIMEOUT_OR_CONTINUE(time_ref);
+    } 
+    else if CHECK_ERROR(EISCONN) 
+    {
+      break;
+    }
+    printf("Could not connect to host; error code is %d\n", errno);
+    return CHECK_ERROR(ETIMEDOUT) ? E_OGGPLAY_TIMEOUT : 
+                                              E_OGGPLAY_SOCKET_ERROR;
+  }
+
+  return E_OGGPLAY_OK;
+
+}



More information about the commits mailing list