ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / VP_Com / win32 / vp_com_socket_utils.c
1 #ifdef _WIN32
2
3 #include <VP_Com/vp_com_socket.h>
4 #include <VP_Com/vp_com_error.h>
5
6 #include <VP_Os/vp_os_malloc.h>
7 #include <VP_Os/vp_os_print.h>
8 #include <VP_Os/vp_os_signal.h>
9
10 #include <fcntl.h>
11 #include <errno.h>
12
13
14 typedef int socklen_t;
15
16 int32_t vp_com_fill_read_fs(vp_com_socket_t* sockets, int32_t num_sockets, int32_t max, fd_set* read_fs )
17 {
18   while( num_sockets > 0 )
19   {
20     if( !sockets->is_disable )
21     {
22       int32_t s = (int32_t) sockets->priv;
23
24       FD_SET( s, read_fs); // add the socket
25
26       if( s > max )
27         max = s;
28     }
29
30     sockets ++;
31     num_sockets--;
32   }
33
34   return max;
35 }
36
37 void vp_com_close_client_sockets(vp_com_socket_t* client_sockets, int32_t num_client_sockets)
38 {
39   int32_t s;
40
41   // Select timed out - We close all sockets because it should mean we lost connection with client
42   while( num_client_sockets > 0 )
43   {
44     if( !client_sockets->is_disable )
45     {
46       s = (int32_t) client_sockets->priv;
47
48       DEBUG_PRINT_SDK("[VP_COM_SERVER] Closing socket %d\n", (int)s);
49
50       client_sockets->select( client_sockets->server,
51                               client_sockets,
52                               VP_COM_SOCKET_SELECT_DISABLE,
53                               (Write) vp_com_write_socket );
54
55       if( client_sockets->protocol == VP_COM_TCP )
56       {
57         closesocket( s );
58       }
59
60       vp_os_memset( client_sockets, 0, sizeof(vp_com_socket_t) );
61       client_sockets->is_disable = TRUE;
62     }
63
64     client_sockets++;
65     num_client_sockets--;
66   }
67 }
68
69 C_RESULT vp_com_client_open_socket(vp_com_socket_t* server_socket, vp_com_socket_t* client_socket)
70 {
71   C_RESULT res;
72   struct sockaddr_in raddr = { 0 }; // remote address
73
74   socklen_t l = sizeof(raddr);
75   int32_t s = (int32_t) server_socket->priv;
76
77   Write write = (Write) (server_socket->protocol == VP_COM_TCP ? vp_com_write_socket : vp_com_write_udp_socket);
78
79   vp_os_memcpy( client_socket, server_socket, sizeof(vp_com_socket_t) );
80
81   res = server_socket->select( server_socket, client_socket, VP_COM_SOCKET_SELECT_ENABLE, write );
82
83   if( VP_SUCCEEDED(res) )
84   {
85     if( server_socket->protocol == VP_COM_TCP )
86     {
87       client_socket->priv = (void*)accept( s, (struct sockaddr*)&raddr, &l );
88     }
89
90     DEBUG_PRINT_SDK("[VP_COM_SERVER] Opening socket for server %d\n", (int)s);
91
92     client_socket->server  = server_socket;
93   }
94   else
95   {
96     DEBUG_PRINT_SDK("[VP_COM_SERVER] Failed to open socket for server %d\n", (int)s);
97     vp_os_memset( client_socket, 0, sizeof(vp_com_socket_t) );
98   }
99
100   return res;
101 }
102
103 void vp_com_client_receive( vp_com_socket_t *client_socket )
104 {
105   static int8_t local_buffer[VP_COM_THREAD_LOCAL_BUFFER_MAX_SIZE];
106   struct sockaddr from;
107   socklen_t fromlen;
108   int32_t s, received;
109
110   s = (int32_t) client_socket->priv;
111
112   fromlen = sizeof(from);
113   received = recvfrom(s, (char*)local_buffer, sizeof(local_buffer)/*VP_COM_THREAD_LOCAL_BUFFER_MAX_SIZE*/, 0, &from, &fromlen);
114
115   if( received == 0 )
116   {
117     client_socket->select( client_socket->server, client_socket, VP_COM_SOCKET_SELECT_DISABLE, (Write) vp_com_write_socket );
118     closesocket( s );
119     vp_os_memset( client_socket, 0, sizeof(vp_com_socket_t) );
120     client_socket->is_disable = TRUE;
121   }
122   else if( client_socket->read != NULL )
123   {
124     client_socket->read( (void*) client_socket, local_buffer, &received, ((struct sockaddr_in*)&from)->sin_addr.s_addr );
125   }
126 }
127
128
129 #endif