ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_win32.c
Revision: 1.3
Committed: Sat Nov 17 05:26:09 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_0, rel-1_71, rel-1_8, rel-1_4, rel-1_72, rel-1_1, rel-1_3, rel-1_2, rel-1_5, rel-1_6, rel-1_81, rel-1_86, rel-1_85
Changes since 1.2: +7 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * libev win32 compatibility cruft
3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
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 */
31
32 #ifdef _WIN32
33
34 #include <sys/timeb.h>
35
36 /* note: the comment below could not be substantiated, but what would I care */
37 /* MSDN says this is required to handle SIGFPE */
38 volatile double SIGFPE_REQ = 0.0f;
39
40 /* oh, the humanity! */
41 static int
42 ev_pipe (int filedes [2])
43 {
44 struct sockaddr_in addr = { 0 };
45 int addr_size = sizeof (addr);
46 SOCKET listener;
47 SOCKET sock [2] = { -1, -1 };
48
49 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
50 return -1;
51
52 addr.sin_family = AF_INET;
53 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
54 addr.sin_port = 0;
55
56 if (bind (listener, (struct sockaddr *)&addr, addr_size))
57 goto fail;
58
59 if (getsockname(listener, (struct sockaddr *)&addr, &addr_size))
60 goto fail;
61
62 if (listen (listener, 1))
63 goto fail;
64
65 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
66 goto fail;
67
68 if (connect (sock[0], (struct sockaddr *)&addr, addr_size))
69 goto fail;
70
71 if ((sock[1] = accept (listener, 0, 0)) < 0)
72 goto fail;
73
74 closesocket (listener);
75
76 #if EV_SELECT_IS_WINSOCKET
77 filedes [0] = _open_osfhandle (sock [0], 0);
78 filedes [1] = _open_osfhandle (sock [1], 0);
79 #else
80 /* when select isn't winsocket, we also expect socket, connect, accept etc.
81 * to work on fds */
82 filedes [0] = sock [0];
83 filedes [1] = sock [1];
84 #endif
85
86 return 0;
87
88 fail:
89 closesocket (listener);
90
91 if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
92 if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
93
94 return -1;
95 }
96
97 #undef pipe
98 #define pipe(filedes) ev_pipe (filedes)
99
100 static int
101 ev_gettimeofday (struct timeval *tv, struct timezone *tz)
102 {
103 struct _timeb tb;
104
105 _ftime (&tb);
106
107 tv->tv_sec = (long)tb.time;
108 tv->tv_usec = ((long)tb.millitm) * 1000;
109
110 return 0;
111 }
112
113 #undef gettimeofday
114 #define gettimeofday(tv,tz) ev_gettimeofday (tv, tz)
115
116 #endif
117