ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_win32.c
Revision: 1.18
Committed: Thu Nov 12 07:02:37 2015 UTC (8 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: EV-rel-4_27, EV-rel-4_22, rel-4_23, rel-4_22, rel-4_27, rel-4_25, rel-4_24
Changes since 1.17: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2 root 1.4 * libev win32 compatibility cruft (_not_ a backend)
3 root 1.1 *
4 root 1.10 * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de>
5 root 1.1 * All rights reserved.
6     *
7 root 1.4 * Redistribution and use in source and binary forms, with or without modifica-
8     * tion, are permitted provided that the following conditions are met:
9 root 1.14 *
10 root 1.4 * 1. Redistributions of source code must retain the above copyright notice,
11     * this list of conditions and the following disclaimer.
12 root 1.14 *
13 root 1.4 * 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 root 1.14 *
17 root 1.4 * 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 root 1.1 *
28 root 1.4 * 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 root 1.1 */
39    
40 root 1.2 #ifdef _WIN32
41 root 1.1
42     /* note: the comment below could not be substantiated, but what would I care */
43     /* MSDN says this is required to handle SIGFPE */
44 root 1.6 /* my wild guess would be that using something floating-pointy is required */
45     /* for the crt to do something about it */
46 root 1.14 volatile double SIGFPE_REQ = 0.0f;
47 root 1.1
48 root 1.15 static SOCKET
49     ev_tcp_socket (void)
50     {
51     #if EV_USE_WSASOCKET
52     return WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0);
53     #else
54     return socket (AF_INET, SOCK_STREAM, 0);
55     #endif
56     }
57    
58 root 1.1 /* oh, the humanity! */
59     static int
60     ev_pipe (int filedes [2])
61     {
62     struct sockaddr_in addr = { 0 };
63     int addr_size = sizeof (addr);
64 root 1.7 struct sockaddr_in adr2;
65 root 1.9 int adr2_size = sizeof (adr2);
66 root 1.1 SOCKET listener;
67     SOCKET sock [2] = { -1, -1 };
68    
69 root 1.15 if ((listener = ev_tcp_socket ()) == INVALID_SOCKET)
70 root 1.1 return -1;
71    
72     addr.sin_family = AF_INET;
73     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
74     addr.sin_port = 0;
75    
76     if (bind (listener, (struct sockaddr *)&addr, addr_size))
77     goto fail;
78    
79 root 1.7 if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
80 root 1.1 goto fail;
81    
82     if (listen (listener, 1))
83     goto fail;
84    
85 root 1.15 if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET)
86 root 1.1 goto fail;
87    
88 root 1.7 if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
89 root 1.1 goto fail;
90    
91 root 1.18 /* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */
92     /* when convenient, probably by just removing error checking altogether? */
93 root 1.7 if ((sock [1] = accept (listener, 0, 0)) < 0)
94     goto fail;
95    
96 root 1.8 /* windows vista returns fantasy port numbers for sockets:
97 root 1.7 * example for two interconnected tcp sockets:
98     *
99     * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364
100     * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363
101     * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363
102     * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365
103     *
104     * wow! tridirectional sockets!
105     *
106     * this way of checking ports seems to work:
107     */
108     if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size))
109     goto fail;
110    
111     if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size))
112     goto fail;
113    
114     errno = WSAEINVAL;
115     if (addr_size != adr2_size
116     || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */
117     || addr.sin_port != adr2.sin_port)
118 root 1.1 goto fail;
119    
120     closesocket (listener);
121    
122 root 1.3 #if EV_SELECT_IS_WINSOCKET
123 root 1.13 filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]);
124     filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]);
125 root 1.3 #else
126     /* when select isn't winsocket, we also expect socket, connect, accept etc.
127     * to work on fds */
128 root 1.1 filedes [0] = sock [0];
129     filedes [1] = sock [1];
130 root 1.3 #endif
131 root 1.1
132     return 0;
133    
134     fail:
135     closesocket (listener);
136    
137     if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
138     if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
139    
140     return -1;
141     }
142    
143     #undef pipe
144     #define pipe(filedes) ev_pipe (filedes)
145 root 1.16
146 root 1.11 #define EV_HAVE_EV_TIME 1
147     ev_tstamp
148     ev_time (void)
149 root 1.1 {
150 root 1.11 FILETIME ft;
151     ULARGE_INTEGER ui;
152 root 1.1
153 root 1.11 GetSystemTimeAsFileTime (&ft);
154     ui.u.LowPart = ft.dwLowDateTime;
155     ui.u.HighPart = ft.dwHighDateTime;
156 root 1.1
157 root 1.11 /* msvc cannot convert ulonglong to double... yes, it is that sucky */
158 root 1.12 return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7;
159 root 1.1 }
160    
161     #endif
162 root 1.2