~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/enet/win32.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** 
 
2
 @file  win32.c
 
3
 @brief ENet Win32 system specific functions
 
4
*/
 
5
#ifdef WIN32
 
6
 
 
7
#include <time.h>
 
8
#define ENET_BUILDING_LIB 1
 
9
#include "enet/enet.h"
 
10
 
 
11
static enet_uint32 timeBase = 0;
 
12
 
 
13
int
 
14
enet_initialize (void)
 
15
{
 
16
    WORD versionRequested = MAKEWORD (1, 1);
 
17
    WSADATA wsaData;
 
18
   
 
19
    if (WSAStartup (versionRequested, & wsaData))
 
20
       return -1;
 
21
 
 
22
    if (LOBYTE (wsaData.wVersion) != 1||
 
23
        HIBYTE (wsaData.wVersion) != 1)
 
24
    {
 
25
       WSACleanup ();
 
26
       
 
27
       return -1;
 
28
    }
 
29
 
 
30
    timeBeginPeriod (1);
 
31
 
 
32
    return 0;
 
33
}
 
34
 
 
35
void
 
36
enet_deinitialize (void)
 
37
{
 
38
    timeEndPeriod (1);
 
39
 
 
40
    WSACleanup ();
 
41
}
 
42
 
 
43
enet_uint32
 
44
enet_time_get (void)
 
45
{
 
46
    return (enet_uint32) timeGetTime () - timeBase;
 
47
}
 
48
 
 
49
void
 
50
enet_time_set (enet_uint32 newTimeBase)
 
51
{
 
52
    timeBase = (enet_uint32) timeGetTime () - newTimeBase;
 
53
}
 
54
 
 
55
int
 
56
enet_address_set_host (ENetAddress * address, const char * name)
 
57
{
 
58
    struct hostent * hostEntry;
 
59
 
 
60
    hostEntry = gethostbyname (name);
 
61
    if (hostEntry == NULL ||
 
62
        hostEntry -> h_addrtype != AF_INET)
 
63
    {
 
64
        unsigned long host = inet_addr (name);
 
65
        if (host == INADDR_NONE)
 
66
            return -1;
 
67
        address -> host = host;
 
68
        return 0;
 
69
    }
 
70
 
 
71
    address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
 
72
 
 
73
    return 0;
 
74
}
 
75
 
 
76
int
 
77
enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
 
78
{
 
79
    char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
 
80
    if (addr == NULL)
 
81
        return -1;
 
82
    strncpy (name, addr, nameLength);
 
83
    return 0;
 
84
}
 
85
 
 
86
int
 
87
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
 
88
{
 
89
    struct in_addr in;
 
90
    struct hostent * hostEntry;
 
91
    
 
92
    in.s_addr = address -> host;
 
93
    
 
94
    hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
 
95
    if (hostEntry == NULL)
 
96
      return enet_address_get_host_ip (address, name, nameLength);
 
97
 
 
98
    strncpy (name, hostEntry -> h_name, nameLength);
 
99
 
 
100
    return 0;
 
101
}
 
102
 
 
103
int
 
104
enet_socket_bind (ENetSocket socket, const ENetAddress * address)
 
105
{
 
106
    struct sockaddr_in sin;
 
107
 
 
108
    memset (& sin, 0, sizeof (struct sockaddr_in));
 
109
 
 
110
    sin.sin_family = AF_INET;
 
111
 
 
112
    if (address != NULL)
 
113
    {
 
114
       sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
 
115
       sin.sin_addr.s_addr = address -> host;
 
116
    }
 
117
    else
 
118
    {
 
119
       sin.sin_port = 0;
 
120
       sin.sin_addr.s_addr = INADDR_ANY;
 
121
    }
 
122
 
 
123
    return bind (socket,
 
124
                 (struct sockaddr *) & sin,
 
125
                 sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
 
126
}
 
127
 
 
128
int
 
129
enet_socket_listen (ENetSocket socket, int backlog)
 
130
{
 
131
    return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0;
 
132
}
 
133
 
 
134
ENetSocket
 
135
enet_socket_create (ENetSocketType type)
 
136
{
 
137
    return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
 
138
}
 
139
 
 
140
int
 
141
enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
 
142
{
 
143
    int result = SOCKET_ERROR;
 
144
    switch (option)
 
145
    {
 
146
        case ENET_SOCKOPT_NONBLOCK:
 
147
        {
 
148
            u_long nonBlocking = (u_long) value;
 
149
            result = ioctlsocket (socket, FIONBIO, & nonBlocking);
 
150
            break;
 
151
        }
 
152
 
 
153
        case ENET_SOCKOPT_BROADCAST:
 
154
            result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
 
155
            break;
 
156
 
 
157
        case ENET_SOCKOPT_REUSEADDR:
 
158
            result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
 
159
            break;
 
160
 
 
161
        case ENET_SOCKOPT_RCVBUF:
 
162
            result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
 
163
            break;
 
164
 
 
165
        case ENET_SOCKOPT_SNDBUF:
 
166
            result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
 
167
            break;
 
168
 
 
169
        case ENET_SOCKOPT_RCVTIMEO:
 
170
            result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
 
171
            break;
 
172
 
 
173
        case ENET_SOCKOPT_SNDTIMEO:
 
174
            result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
 
175
            break;
 
176
 
 
177
        default:
 
178
            break;
 
179
    }
 
180
    return result == SOCKET_ERROR ? -1 : 0;
 
181
}
 
182
 
 
183
int
 
184
enet_socket_connect (ENetSocket socket, const ENetAddress * address)
 
185
{
 
186
    struct sockaddr_in sin;
 
187
 
 
188
    memset (& sin, 0, sizeof (struct sockaddr_in));
 
189
 
 
190
    sin.sin_family = AF_INET;
 
191
    sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
 
192
    sin.sin_addr.s_addr = address -> host;
 
193
 
 
194
    return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
 
195
}
 
196
 
 
197
ENetSocket
 
198
enet_socket_accept (ENetSocket socket, ENetAddress * address)
 
199
{
 
200
    SOCKET result;
 
201
    struct sockaddr_in sin;
 
202
    int sinLength = sizeof (struct sockaddr_in);
 
203
 
 
204
    result = accept (socket, 
 
205
                     address != NULL ? (struct sockaddr *) & sin : NULL, 
 
206
                     address != NULL ? & sinLength : NULL);
 
207
 
 
208
    if (result == INVALID_SOCKET)
 
209
      return ENET_SOCKET_NULL;
 
210
 
 
211
    if (address != NULL)
 
212
    {
 
213
        address -> host = (enet_uint32) sin.sin_addr.s_addr;
 
214
        address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
 
215
    }
 
216
 
 
217
    return result;
 
218
}
 
219
 
 
220
void
 
221
enet_socket_destroy (ENetSocket socket)
 
222
{
 
223
    closesocket (socket);
 
224
}
 
225
 
 
226
int
 
227
enet_socket_send (ENetSocket socket,
 
228
                  const ENetAddress * address,
 
229
                  const ENetBuffer * buffers,
 
230
                  size_t bufferCount)
 
231
{
 
232
    struct sockaddr_in sin;
 
233
    DWORD sentLength;
 
234
 
 
235
    if (address != NULL)
 
236
    {
 
237
        memset (& sin, 0, sizeof (struct sockaddr_in));
 
238
 
 
239
        sin.sin_family = AF_INET;
 
240
        sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
 
241
        sin.sin_addr.s_addr = address -> host;
 
242
    }
 
243
 
 
244
    if (WSASendTo (socket, 
 
245
                   (LPWSABUF) buffers,
 
246
                   (DWORD) bufferCount,
 
247
                   & sentLength,
 
248
                   0,
 
249
                   address != NULL ? (struct sockaddr *) & sin : NULL,
 
250
                   address != NULL ? sizeof (struct sockaddr_in) : 0,
 
251
                   NULL,
 
252
                   NULL) == SOCKET_ERROR)
 
253
    {
 
254
       if (WSAGetLastError () == WSAEWOULDBLOCK)
 
255
         return 0;
 
256
 
 
257
       return -1;
 
258
    }
 
259
 
 
260
    return (int) sentLength;
 
261
}
 
262
 
 
263
int
 
264
enet_socket_receive (ENetSocket socket,
 
265
                     ENetAddress * address,
 
266
                     ENetBuffer * buffers,
 
267
                     size_t bufferCount)
 
268
{
 
269
    INT sinLength = sizeof (struct sockaddr_in);
 
270
    DWORD flags = 0,
 
271
          recvLength;
 
272
    struct sockaddr_in sin;
 
273
 
 
274
    if (WSARecvFrom (socket,
 
275
                     (LPWSABUF) buffers,
 
276
                     (DWORD) bufferCount,
 
277
                     & recvLength,
 
278
                     & flags,
 
279
                     address != NULL ? (struct sockaddr *) & sin : NULL,
 
280
                     address != NULL ? & sinLength : NULL,
 
281
                     NULL,
 
282
                     NULL) == SOCKET_ERROR)
 
283
    {
 
284
       switch (WSAGetLastError ())
 
285
       {
 
286
       case WSAEWOULDBLOCK:
 
287
       case WSAECONNRESET:
 
288
          return 0;
 
289
       }
 
290
 
 
291
       return -1;
 
292
    }
 
293
 
 
294
    if (flags & MSG_PARTIAL)
 
295
      return -1;
 
296
 
 
297
    if (address != NULL)
 
298
    {
 
299
        address -> host = (enet_uint32) sin.sin_addr.s_addr;
 
300
        address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
 
301
    }
 
302
 
 
303
    return (int) recvLength;
 
304
}
 
305
 
 
306
int
 
307
enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
 
308
{
 
309
    struct timeval timeVal;
 
310
 
 
311
    timeVal.tv_sec = timeout / 1000;
 
312
    timeVal.tv_usec = (timeout % 1000) * 1000;
 
313
 
 
314
    return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
 
315
}
 
316
 
 
317
int
 
318
enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
 
319
{
 
320
    fd_set readSet, writeSet;
 
321
    struct timeval timeVal;
 
322
    int selectCount;
 
323
    
 
324
    timeVal.tv_sec = timeout / 1000;
 
325
    timeVal.tv_usec = (timeout % 1000) * 1000;
 
326
    
 
327
    FD_ZERO (& readSet);
 
328
    FD_ZERO (& writeSet);
 
329
 
 
330
    if (* condition & ENET_SOCKET_WAIT_SEND)
 
331
      FD_SET (socket, & writeSet);
 
332
 
 
333
    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
 
334
      FD_SET (socket, & readSet);
 
335
 
 
336
    selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
 
337
 
 
338
    if (selectCount < 0)
 
339
      return -1;
 
340
 
 
341
    * condition = ENET_SOCKET_WAIT_NONE;
 
342
 
 
343
    if (selectCount == 0)
 
344
      return 0;
 
345
 
 
346
    if (FD_ISSET (socket, & writeSet))
 
347
      * condition |= ENET_SOCKET_WAIT_SEND;
 
348
    
 
349
    if (FD_ISSET (socket, & readSet))
 
350
      * condition |= ENET_SOCKET_WAIT_RECEIVE;
 
351
 
 
352
    return 0;
 
353
 
354
 
 
355
#endif
 
356