The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / libev / ev_win32.c @ 90fc8d36

History | View | Annotate | Download (4.92 KB)

1 c5183738 Ryan
/*
2
 * libev win32 compatibility cruft (_not_ a backend)
3
 *
4
 * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de>
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without modifica-
8
 * tion, are permitted provided that the following conditions are met:
9
 * 
10
 *   1.  Redistributions of source code must retain the above copyright notice,
11
 *       this list of conditions and the following disclaimer.
12
 * 
13
 *   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
 * 
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-
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
 *
28
 * 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
 */
39
40
#ifdef _WIN32
41
42
/* timeb.h is actually xsi legacy functionality */
43
#include <sys/timeb.h>
44
45
/* note: the comment below could not be substantiated, but what would I care */
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 */
49
volatile double SIGFPE_REQ = 0.0f; 
50
51
/* oh, the humanity! */
52
static int
53
ev_pipe (int filedes [2])
54
{
55
  struct sockaddr_in addr = { 0 };
56
  int addr_size = sizeof (addr);
57
  struct sockaddr_in adr2;
58
  int adr2_size = sizeof (adr2);
59
  SOCKET listener;
60
  SOCKET sock [2] = { -1, -1 };
61
62
  if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 
63
    return -1;
64
65
  addr.sin_family = AF_INET;
66
  addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
67
  addr.sin_port = 0;
68
69
  if (bind (listener, (struct sockaddr *)&addr, addr_size))
70
    goto fail;
71
72
  if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
73
    goto fail;
74
75
  if (listen (listener, 1))
76
    goto fail;
77
78
  if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) 
79
    goto fail;
80
81
  if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
82
    goto fail;
83
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)
109
    goto fail;
110
111
  closesocket (listener);
112
113
#if EV_SELECT_IS_WINSOCKET
114
  filedes [0] = _open_osfhandle (sock [0], 0);
115
  filedes [1] = _open_osfhandle (sock [1], 0);
116
#else
117
  /* when select isn't winsocket, we also expect socket, connect, accept etc.
118
   * to work on fds */
119
  filedes [0] = sock [0];
120
  filedes [1] = sock [1];
121
#endif
122
123
  return 0;
124
125
fail:
126
  closesocket (listener);
127
128
  if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
129
  if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
130
131
  return -1;
132
}
133
134
#undef pipe
135
#define pipe(filedes) ev_pipe (filedes)
136
137
static int
138
ev_gettimeofday (struct timeval *tv, struct timezone *tz)
139
{
140
  struct _timeb tb;
141
142
  _ftime (&tb);
143
144
  tv->tv_sec  = (long)tb.time;
145
  tv->tv_usec = ((long)tb.millitm) * 1000;
146
147
  return 0;
148
}
149
150
#undef gettimeofday
151
#define gettimeofday(tv,tz) ev_gettimeofday (tv, tz)
152
153
#endif