changeset 2351:b9a881c0967e libavformat

Add initialization and cleanup functions for Winsock
author ramiro
date Thu, 09 Aug 2007 23:39:05 +0000
parents a0fb5ef1ec7c
children 3dfa90f47d53
files network.h tcp.c udp.c
diffstat 3 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/network.h	Thu Aug 09 23:29:58 2007 +0000
+++ b/network.h	Thu Aug 09 23:39:05 2007 +0000
@@ -44,6 +44,23 @@
 
 int ff_socket_nonblock(int socket, int enable);
 
+static inline int ff_network_init(void)
+{
+#ifdef HAVE_WINSOCK2_H
+    WSADATA wsaData;
+    if (WSAStartup(MAKEWORD(1,1), &wsaData))
+        return 0;
+#endif
+    return 1;
+}
+
+static inline void ff_network_close(void)
+{
+#ifdef HAVE_WINSOCK2_H
+    WSACleanup();
+#endif
+}
+
 #if !defined(HAVE_INET_ATON)
 /* in os_support.c */
 int inet_aton (const char * str, struct in_addr * add);
--- a/tcp.c	Thu Aug 09 23:29:58 2007 +0000
+++ b/tcp.c	Thu Aug 09 23:39:05 2007 +0000
@@ -53,6 +53,9 @@
     if (port <= 0 || port >= 65536)
         goto fail;
 
+    if(!ff_network_init())
+        return AVERROR(EIO);
+
     dest_addr.sin_family = AF_INET;
     dest_addr.sin_port = htons(port);
     if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
@@ -174,6 +177,7 @@
 {
     TCPContext *s = h->priv_data;
     closesocket(s->fd);
+    ff_network_close();
     av_free(s);
     return 0;
 }
--- a/udp.c	Thu Aug 09 23:29:58 2007 +0000
+++ b/udp.c	Thu Aug 09 23:39:05 2007 +0000
@@ -321,6 +321,9 @@
         udp_set_remote_url(h, uri);
     }
 
+    if(!ff_network_init())
+        return AVERROR(EIO);
+
 #ifndef CONFIG_IPV6
     udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
     if (udp_fd < 0)
@@ -472,6 +475,7 @@
         udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
 #endif
     closesocket(s->udp_fd);
+    ff_network_close();
     av_free(s);
     return 0;
 }