1 | /* |
1 | /* |
2 | * libev win32 compatibility cruft (_not_ a backend) |
2 | * libev win32 compatibility cruft (_not_ a backend) |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008 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 modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
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- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
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- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
44 | |
44 | |
45 | /* 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 */ |
46 | /* 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 */ |
47 | /* my wild guess would be that using something floating-pointy is required */ |
48 | /* for the crt to do something about it */ |
48 | /* for the crt to do something about it */ |
49 | volatile double SIGFPE_REQ = 0.0f; |
49 | volatile double SIGFPE_REQ = 0.0f; |
50 | |
50 | |
51 | /* oh, the humanity! */ |
51 | /* oh, the humanity! */ |
52 | static int |
52 | static int |
53 | ev_pipe (int filedes [2]) |
53 | ev_pipe (int filedes [2]) |
54 | { |
54 | { |
… | |
… | |
57 | struct sockaddr_in adr2; |
57 | struct sockaddr_in adr2; |
58 | int adr2_size = sizeof (adr2); |
58 | int adr2_size = sizeof (adr2); |
59 | SOCKET listener; |
59 | SOCKET listener; |
60 | SOCKET sock [2] = { -1, -1 }; |
60 | SOCKET sock [2] = { -1, -1 }; |
61 | |
61 | |
62 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
62 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
63 | return -1; |
63 | return -1; |
64 | |
64 | |
65 | addr.sin_family = AF_INET; |
65 | addr.sin_family = AF_INET; |
66 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
66 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
67 | addr.sin_port = 0; |
67 | addr.sin_port = 0; |
… | |
… | |
73 | goto fail; |
73 | goto fail; |
74 | |
74 | |
75 | if (listen (listener, 1)) |
75 | if (listen (listener, 1)) |
76 | goto fail; |
76 | goto fail; |
77 | |
77 | |
78 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
78 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
79 | goto fail; |
79 | goto fail; |
80 | |
80 | |
81 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
81 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
82 | goto fail; |
82 | goto fail; |
83 | |
83 | |
… | |
… | |
109 | goto fail; |
109 | goto fail; |
110 | |
110 | |
111 | closesocket (listener); |
111 | closesocket (listener); |
112 | |
112 | |
113 | #if EV_SELECT_IS_WINSOCKET |
113 | #if EV_SELECT_IS_WINSOCKET |
114 | filedes [0] = _open_osfhandle (sock [0], 0); |
114 | filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]); |
115 | filedes [1] = _open_osfhandle (sock [1], 0); |
115 | filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]); |
116 | #else |
116 | #else |
117 | /* when select isn't winsocket, we also expect socket, connect, accept etc. |
117 | /* when select isn't winsocket, we also expect socket, connect, accept etc. |
118 | * to work on fds */ |
118 | * to work on fds */ |
119 | filedes [0] = sock [0]; |
119 | filedes [0] = sock [0]; |
120 | filedes [1] = sock [1]; |
120 | filedes [1] = sock [1]; |
… | |
… | |
131 | return -1; |
131 | return -1; |
132 | } |
132 | } |
133 | |
133 | |
134 | #undef pipe |
134 | #undef pipe |
135 | #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; |
136 | |
143 | |
137 | static int |
144 | GetSystemTimeAsFileTime (&ft); |
138 | ev_gettimeofday (struct timeval *tv, struct timezone *tz) |
145 | ui.u.LowPart = ft.dwLowDateTime; |
139 | { |
146 | ui.u.HighPart = ft.dwHighDateTime; |
140 | struct _timeb tb; |
|
|
141 | |
147 | |
142 | _ftime (&tb); |
148 | /* msvc cannot convert ulonglong to double... yes, it is that sucky */ |
143 | |
149 | return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7; |
144 | tv->tv_sec = (long)tb.time; |
|
|
145 | tv->tv_usec = ((long)tb.millitm) * 1000; |
|
|
146 | |
|
|
147 | return 0; |
|
|
148 | } |
150 | } |
149 | |
|
|
150 | #undef gettimeofday |
|
|
151 | #define gettimeofday(tv,tz) ev_gettimeofday (tv, tz) |
|
|
152 | |
151 | |
153 | #endif |
152 | #endif |
154 | |
153 | |