1 | /* |
1 | /* |
2 | * libev win32 compatibility cruft (_not_ a backend) |
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 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 | * |
… | |
… | |
37 | * either the BSD or the GPL. |
37 | * either the BSD or the GPL. |
38 | */ |
38 | */ |
39 | |
39 | |
40 | #ifdef _WIN32 |
40 | #ifdef _WIN32 |
41 | |
41 | |
|
|
42 | /* timeb.h is actually xsi legacy functionality */ |
42 | #include <sys/timeb.h> |
43 | #include <sys/timeb.h> |
43 | |
44 | |
44 | /* 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 */ |
45 | /* 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 */ |
46 | volatile double SIGFPE_REQ = 0.0f; |
49 | volatile double SIGFPE_REQ = 0.0f; |
47 | |
50 | |
48 | /* oh, the humanity! */ |
51 | /* oh, the humanity! */ |
49 | static int |
52 | static int |
50 | ev_pipe (int filedes [2]) |
53 | ev_pipe (int filedes [2]) |
51 | { |
54 | { |
52 | struct sockaddr_in addr = { 0 }; |
55 | struct sockaddr_in addr = { 0 }; |
53 | int addr_size = sizeof (addr); |
56 | int addr_size = sizeof (addr); |
|
|
57 | struct sockaddr_in adr2; |
|
|
58 | int adr2_size = sizeof (adr2); |
54 | SOCKET listener; |
59 | SOCKET listener; |
55 | SOCKET sock [2] = { -1, -1 }; |
60 | SOCKET sock [2] = { -1, -1 }; |
56 | |
61 | |
57 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
62 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
58 | return -1; |
63 | return -1; |
… | |
… | |
62 | addr.sin_port = 0; |
67 | addr.sin_port = 0; |
63 | |
68 | |
64 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
69 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
65 | goto fail; |
70 | goto fail; |
66 | |
71 | |
67 | if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
72 | if (getsockname (listener, (struct sockaddr *)&addr, &addr_size)) |
68 | goto fail; |
73 | goto fail; |
69 | |
74 | |
70 | if (listen (listener, 1)) |
75 | if (listen (listener, 1)) |
71 | goto fail; |
76 | goto fail; |
72 | |
77 | |
73 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
78 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
74 | goto fail; |
79 | goto fail; |
75 | |
80 | |
76 | if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
81 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
77 | goto fail; |
82 | goto fail; |
78 | |
83 | |
79 | 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) |
80 | goto fail; |
109 | goto fail; |
81 | |
110 | |
82 | closesocket (listener); |
111 | closesocket (listener); |
83 | |
112 | |
84 | #if EV_SELECT_IS_WINSOCKET |
113 | #if EV_SELECT_IS_WINSOCKET |
… | |
… | |
102 | return -1; |
131 | return -1; |
103 | } |
132 | } |
104 | |
133 | |
105 | #undef pipe |
134 | #undef pipe |
106 | #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; |
107 | |
143 | |
108 | static int |
144 | GetSystemTimeAsFileTime (&ft); |
109 | ev_gettimeofday (struct timeval *tv, struct timezone *tz) |
145 | ui.u.LowPart = ft.dwLowDateTime; |
110 | { |
146 | ui.u.HighPart = ft.dwHighDateTime; |
111 | struct _timeb tb; |
|
|
112 | |
147 | |
113 | _ftime (&tb); |
148 | /* msvc cannot convert ulonglong to double... yes, it is that sucky */ |
114 | |
149 | return (__int64)(ui.QuadPart - 116444736000000000) * 1e-7; |
115 | tv->tv_sec = (long)tb.time; |
|
|
116 | tv->tv_usec = ((long)tb.millitm) * 1000; |
|
|
117 | |
|
|
118 | return 0; |
|
|
119 | } |
150 | } |
120 | |
|
|
121 | #undef gettimeofday |
|
|
122 | #define gettimeofday(tv,tz) ev_gettimeofday (tv, tz) |
|
|
123 | |
151 | |
124 | #endif |
152 | #endif |
125 | |
153 | |