1 | /* |
1 | /* |
2 | * libev win32 compatibility cruft |
2 | * libev win32 compatibility cruft (_not_ a backend) |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * modification, are permitted provided that the following conditions are |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * 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. |
10 | * |
27 | * |
11 | * * Redistributions of source code must retain the above copyright |
28 | * Alternatively, the contents of this file may be used under the terms of |
12 | * notice, this list of conditions and the following disclaimer. |
29 | * the GNU General Public License ("GPL") version 2 or any later version, |
13 | * |
30 | * in which case the provisions of the GPL are applicable instead of |
14 | * * Redistributions in binary form must reproduce the above |
31 | * the above. If you wish to allow the use of your version of this file |
15 | * copyright notice, this list of conditions and the following |
32 | * only under the terms of the GPL and not to allow others to use your |
16 | * disclaimer in the documentation and/or other materials provided |
33 | * version of this file under the BSD license, indicate your decision |
17 | * with the distribution. |
34 | * by deleting the provisions above and replace them with the notice |
18 | * |
35 | * and other provisions required by the GPL. If you do not delete the |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | * provisions above, a recipient may use your version of this file under |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | * either the BSD or the GPL. |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
30 | */ |
38 | */ |
31 | |
39 | |
32 | #ifdef _WIN32 |
40 | #ifdef _WIN32 |
33 | |
41 | |
|
|
42 | /* timeb.h is actually xsi legacy functionality */ |
34 | #include <sys/timeb.h> |
43 | #include <sys/timeb.h> |
35 | |
44 | |
36 | /* note: the comment below could not be substantiated, but what would I care */ |
45 | /* note: the comment below could not be substantiated, but what would I care */ |
37 | /* MSDN says this is required to handle SIGFPE */ |
46 | /* MSDN says this is required to handle SIGFPE */ |
|
|
47 | /* my wild guess would be that using something floating-pointy is required */ |
|
|
48 | /* for the crt to do something about it */ |
38 | volatile double SIGFPE_REQ = 0.0f; |
49 | volatile double SIGFPE_REQ = 0.0f; |
39 | |
50 | |
40 | /* oh, the humanity! */ |
51 | /* oh, the humanity! */ |
41 | static int |
52 | static int |
42 | ev_pipe (int filedes [2]) |
53 | ev_pipe (int filedes [2]) |
43 | { |
54 | { |
44 | struct sockaddr_in addr = { 0 }; |
55 | struct sockaddr_in addr = { 0 }; |
45 | int addr_size = sizeof (addr); |
56 | int addr_size = sizeof (addr); |
|
|
57 | struct sockaddr_in adr2; |
|
|
58 | int adr2_size = sizeof (adr2); |
46 | SOCKET listener; |
59 | SOCKET listener; |
47 | SOCKET sock [2] = { -1, -1 }; |
60 | SOCKET sock [2] = { -1, -1 }; |
48 | |
61 | |
49 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
62 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
50 | return -1; |
63 | return -1; |
… | |
… | |
54 | addr.sin_port = 0; |
67 | addr.sin_port = 0; |
55 | |
68 | |
56 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
69 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
57 | goto fail; |
70 | goto fail; |
58 | |
71 | |
59 | if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
72 | if (getsockname (listener, (struct sockaddr *)&addr, &addr_size)) |
60 | goto fail; |
73 | goto fail; |
61 | |
74 | |
62 | if (listen (listener, 1)) |
75 | if (listen (listener, 1)) |
63 | goto fail; |
76 | goto fail; |
64 | |
77 | |
65 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
78 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
66 | goto fail; |
79 | goto fail; |
67 | |
80 | |
68 | if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
81 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
69 | goto fail; |
82 | goto fail; |
70 | |
83 | |
71 | if ((sock[1] = accept (listener, 0, 0)) < 0) |
84 | if ((sock [1] = accept (listener, 0, 0)) < 0) |
|
|
85 | goto fail; |
|
|
86 | |
|
|
87 | /* windows vista returns fantasy port numbers for sockets: |
|
|
88 | * example for two interconnected tcp sockets: |
|
|
89 | * |
|
|
90 | * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364 |
|
|
91 | * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363 |
|
|
92 | * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363 |
|
|
93 | * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365 |
|
|
94 | * |
|
|
95 | * wow! tridirectional sockets! |
|
|
96 | * |
|
|
97 | * this way of checking ports seems to work: |
|
|
98 | */ |
|
|
99 | if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size)) |
|
|
100 | goto fail; |
|
|
101 | |
|
|
102 | if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size)) |
|
|
103 | goto fail; |
|
|
104 | |
|
|
105 | errno = WSAEINVAL; |
|
|
106 | if (addr_size != adr2_size |
|
|
107 | || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */ |
|
|
108 | || addr.sin_port != adr2.sin_port) |
72 | goto fail; |
109 | goto fail; |
73 | |
110 | |
74 | closesocket (listener); |
111 | closesocket (listener); |
75 | |
112 | |
76 | #if EV_SELECT_IS_WINSOCKET |
113 | #if EV_SELECT_IS_WINSOCKET |
… | |
… | |
94 | return -1; |
131 | return -1; |
95 | } |
132 | } |
96 | |
133 | |
97 | #undef pipe |
134 | #undef pipe |
98 | #define pipe(filedes) ev_pipe (filedes) |
135 | #define pipe(filedes) ev_pipe (filedes) |
|
|
136 | |
|
|
137 | #define EV_HAVE_EV_TIME 1 |
|
|
138 | ev_tstamp |
|
|
139 | ev_time (void) |
|
|
140 | { |
|
|
141 | FILETIME ft; |
|
|
142 | ULARGE_INTEGER ui; |
99 | |
143 | |
100 | static int |
144 | GetSystemTimeAsFileTime (&ft); |
101 | ev_gettimeofday (struct timeval *tv, struct timezone *tz) |
145 | ui.u.LowPart = ft.dwLowDateTime; |
102 | { |
146 | ui.u.HighPart = ft.dwHighDateTime; |
103 | struct _timeb tb; |
|
|
104 | |
147 | |
105 | _ftime (&tb); |
148 | /* msvc cannot convert ulonglong to double... yes, it is that sucky */ |
106 | |
149 | return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7; |
107 | tv->tv_sec = (long)tb.time; |
|
|
108 | tv->tv_usec = ((long)tb.millitm) * 1000; |
|
|
109 | |
|
|
110 | return 0; |
|
|
111 | } |
150 | } |
112 | |
|
|
113 | #undef gettimeofday |
|
|
114 | #define gettimeofday(tv,tz) ev_gettimeofday (tv, tz) |
|
|
115 | |
151 | |
116 | #endif |
152 | #endif |
117 | |
153 | |