1 |
/* |
2 |
* libev win32 compatibility cruft (_not_ a backend) |
3 |
* |
4 |
* Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
* All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or without modifica- |
8 |
* tion, are permitted provided that the following conditions are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright notice, |
11 |
* this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 |
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 |
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
25 |
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
* |
28 |
* Alternatively, the contents of this file may be used under the terms of |
29 |
* the GNU General Public License ("GPL") version 2 or any later version, |
30 |
* in which case the provisions of the GPL are applicable instead of |
31 |
* the above. If you wish to allow the use of your version of this file |
32 |
* only under the terms of the GPL and not to allow others to use your |
33 |
* version of this file under the BSD license, indicate your decision |
34 |
* by deleting the provisions above and replace them with the notice |
35 |
* and other provisions required by the GPL. If you do not delete the |
36 |
* provisions above, a recipient may use your version of this file under |
37 |
* either the BSD or the GPL. |
38 |
*/ |
39 |
|
40 |
#ifdef _WIN32 |
41 |
|
42 |
#include <sys/timeb.h> |
43 |
|
44 |
/* note: the comment below could not be substantiated, but what would I care */ |
45 |
/* MSDN says this is required to handle SIGFPE */ |
46 |
volatile double SIGFPE_REQ = 0.0f; |
47 |
|
48 |
/* oh, the humanity! */ |
49 |
static int |
50 |
ev_pipe (int filedes [2]) |
51 |
{ |
52 |
struct sockaddr_in addr = { 0 }; |
53 |
int addr_size = sizeof (addr); |
54 |
SOCKET listener; |
55 |
SOCKET sock [2] = { -1, -1 }; |
56 |
|
57 |
if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
58 |
return -1; |
59 |
|
60 |
addr.sin_family = AF_INET; |
61 |
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
62 |
addr.sin_port = 0; |
63 |
|
64 |
if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
65 |
goto fail; |
66 |
|
67 |
if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
68 |
goto fail; |
69 |
|
70 |
if (listen (listener, 1)) |
71 |
goto fail; |
72 |
|
73 |
if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
74 |
goto fail; |
75 |
|
76 |
if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
77 |
goto fail; |
78 |
|
79 |
if ((sock[1] = accept (listener, 0, 0)) < 0) |
80 |
goto fail; |
81 |
|
82 |
closesocket (listener); |
83 |
|
84 |
#if EV_SELECT_IS_WINSOCKET |
85 |
filedes [0] = _open_osfhandle (sock [0], 0); |
86 |
filedes [1] = _open_osfhandle (sock [1], 0); |
87 |
#else |
88 |
/* when select isn't winsocket, we also expect socket, connect, accept etc. |
89 |
* to work on fds */ |
90 |
filedes [0] = sock [0]; |
91 |
filedes [1] = sock [1]; |
92 |
#endif |
93 |
|
94 |
return 0; |
95 |
|
96 |
fail: |
97 |
closesocket (listener); |
98 |
|
99 |
if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); |
100 |
if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); |
101 |
|
102 |
return -1; |
103 |
} |
104 |
|
105 |
#undef pipe |
106 |
#define pipe(filedes) ev_pipe (filedes) |
107 |
|
108 |
static int |
109 |
ev_gettimeofday (struct timeval *tv, struct timezone *tz) |
110 |
{ |
111 |
struct _timeb tb; |
112 |
|
113 |
_ftime (&tb); |
114 |
|
115 |
tv->tv_sec = (long)tb.time; |
116 |
tv->tv_usec = ((long)tb.millitm) * 1000; |
117 |
|
118 |
return 0; |
119 |
} |
120 |
|
121 |
#undef gettimeofday |
122 |
#define gettimeofday(tv,tz) ev_gettimeofday (tv, tz) |
123 |
|
124 |
#endif |
125 |
|