ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_win32.c
(Generate patch)

Comparing libev/ev_win32.c (file contents):
Revision 1.5 by root, Fri May 23 16:37:38 2008 UTC vs.
Revision 1.18 by root, Thu Nov 12 07:02:37 2015 UTC

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,
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#include <sys/timeb.h>
43
44/* note: the comment below could not be substantiated, but what would I care */ 42/* note: the comment below could not be substantiated, but what would I care */
45/* MSDN says this is required to handle SIGFPE */ 43/* MSDN says this is required to handle SIGFPE */
44/* my wild guess would be that using something floating-pointy is required */
45/* for the crt to do something about it */
46volatile double SIGFPE_REQ = 0.0f; 46volatile double SIGFPE_REQ = 0.0f;
47
48static SOCKET
49ev_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}
47 57
48/* oh, the humanity! */ 58/* oh, the humanity! */
49static int 59static int
50ev_pipe (int filedes [2]) 60ev_pipe (int filedes [2])
51{ 61{
52 struct sockaddr_in addr = { 0 }; 62 struct sockaddr_in addr = { 0 };
53 int addr_size = sizeof (addr); 63 int addr_size = sizeof (addr);
64 struct sockaddr_in adr2;
65 int adr2_size = sizeof (adr2);
54 SOCKET listener; 66 SOCKET listener;
55 SOCKET sock [2] = { -1, -1 }; 67 SOCKET sock [2] = { -1, -1 };
56 68
57 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 69 if ((listener = ev_tcp_socket ()) == INVALID_SOCKET)
58 return -1; 70 return -1;
59 71
60 addr.sin_family = AF_INET; 72 addr.sin_family = AF_INET;
61 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 73 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
62 addr.sin_port = 0; 74 addr.sin_port = 0;
63 75
64 if (bind (listener, (struct sockaddr *)&addr, addr_size)) 76 if (bind (listener, (struct sockaddr *)&addr, addr_size))
65 goto fail; 77 goto fail;
66 78
67 if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) 79 if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
68 goto fail; 80 goto fail;
69 81
70 if (listen (listener, 1)) 82 if (listen (listener, 1))
71 goto fail; 83 goto fail;
72 84
73 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 85 if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET)
74 goto fail; 86 goto fail;
75 87
76 if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) 88 if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
77 goto fail; 89 goto fail;
78 90
91 /* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */
92 /* when convenient, probably by just removing error checking altogether? */
79 if ((sock[1] = accept (listener, 0, 0)) < 0) 93 if ((sock [1] = accept (listener, 0, 0)) < 0)
94 goto fail;
95
96 /* windows vista returns fantasy port numbers for sockets:
97 * 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)
80 goto fail; 118 goto fail;
81 119
82 closesocket (listener); 120 closesocket (listener);
83 121
84#if EV_SELECT_IS_WINSOCKET 122#if EV_SELECT_IS_WINSOCKET
85 filedes [0] = _open_osfhandle (sock [0], 0); 123 filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]);
86 filedes [1] = _open_osfhandle (sock [1], 0); 124 filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]);
87#else 125#else
88 /* when select isn't winsocket, we also expect socket, connect, accept etc. 126 /* when select isn't winsocket, we also expect socket, connect, accept etc.
89 * to work on fds */ 127 * to work on fds */
90 filedes [0] = sock [0]; 128 filedes [0] = sock [0];
91 filedes [1] = sock [1]; 129 filedes [1] = sock [1];
103} 141}
104 142
105#undef pipe 143#undef pipe
106#define pipe(filedes) ev_pipe (filedes) 144#define pipe(filedes) ev_pipe (filedes)
107 145
108static int 146#define EV_HAVE_EV_TIME 1
109ev_gettimeofday (struct timeval *tv, struct timezone *tz) 147ev_tstamp
148ev_time (void)
110{ 149{
111 struct _timeb tb; 150 FILETIME ft;
151 ULARGE_INTEGER ui;
112 152
113 _ftime (&tb); 153 GetSystemTimeAsFileTime (&ft);
154 ui.u.LowPart = ft.dwLowDateTime;
155 ui.u.HighPart = ft.dwHighDateTime;
114 156
115 tv->tv_sec = (long)tb.time; 157 /* msvc cannot convert ulonglong to double... yes, it is that sucky */
116 tv->tv_usec = ((long)tb.millitm) * 1000; 158 return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7;
117
118 return 0;
119} 159}
120
121#undef gettimeofday
122#define gettimeofday(tv,tz) ev_gettimeofday (tv, tz)
123 160
124#endif 161#endif
125 162

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines