|
Packit Service |
24728e |
/*
|
|
Packit Service |
24728e |
* libev win32 compatibility cruft (_not_ a backend)
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de>
|
|
Packit Service |
24728e |
* All rights reserved.
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* Redistribution and use in source and binary forms, with or without modifica-
|
|
Packit Service |
24728e |
* tion, are permitted provided that the following conditions are met:
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
Packit Service |
24728e |
* this list of conditions and the following disclaimer.
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit Service |
24728e |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit Service |
24728e |
* documentation and/or other materials provided with the distribution.
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
Packit Service |
24728e |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
|
|
Packit Service |
24728e |
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
Packit Service |
24728e |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
|
|
Packit Service |
24728e |
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
Packit Service |
24728e |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
Packit Service |
24728e |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
Packit Service |
24728e |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
|
|
Packit Service |
24728e |
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
Packit Service |
24728e |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* Alternatively, the contents of this file may be used under the terms of
|
|
Packit Service |
24728e |
* the GNU General Public License ("GPL") version 2 or any later version,
|
|
Packit Service |
24728e |
* in which case the provisions of the GPL are applicable instead of
|
|
Packit Service |
24728e |
* the above. If you wish to allow the use of your version of this file
|
|
Packit Service |
24728e |
* only under the terms of the GPL and not to allow others to use your
|
|
Packit Service |
24728e |
* version of this file under the BSD license, indicate your decision
|
|
Packit Service |
24728e |
* by deleting the provisions above and replace them with the notice
|
|
Packit Service |
24728e |
* and other provisions required by the GPL. If you do not delete the
|
|
Packit Service |
24728e |
* provisions above, a recipient may use your version of this file under
|
|
Packit Service |
24728e |
* either the BSD or the GPL.
|
|
Packit Service |
24728e |
*/
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
#ifdef _WIN32
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
/* note: the comment below could not be substantiated, but what would I care */
|
|
Packit Service |
24728e |
/* MSDN says this is required to handle SIGFPE */
|
|
Packit Service |
24728e |
/* my wild guess would be that using something floating-pointy is required */
|
|
Packit Service |
24728e |
/* for the crt to do something about it */
|
|
Packit Service |
24728e |
volatile double SIGFPE_REQ = 0.0f;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
static SOCKET
|
|
Packit Service |
24728e |
ev_tcp_socket (void)
|
|
Packit Service |
24728e |
{
|
|
Packit Service |
24728e |
#if EV_USE_WSASOCKET
|
|
Packit Service |
24728e |
return WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0);
|
|
Packit Service |
24728e |
#else
|
|
Packit Service |
24728e |
return socket (AF_INET, SOCK_STREAM, 0);
|
|
Packit Service |
24728e |
#endif
|
|
Packit Service |
24728e |
}
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
/* oh, the humanity! */
|
|
Packit Service |
24728e |
static int
|
|
Packit Service |
24728e |
ev_pipe (int filedes [2])
|
|
Packit Service |
24728e |
{
|
|
Packit Service |
24728e |
struct sockaddr_in addr = { 0 };
|
|
Packit Service |
24728e |
int addr_size = sizeof (addr);
|
|
Packit Service |
24728e |
struct sockaddr_in adr2;
|
|
Packit Service |
24728e |
int adr2_size = sizeof (adr2);
|
|
Packit Service |
24728e |
SOCKET listener;
|
|
Packit Service |
24728e |
SOCKET sock [2] = { -1, -1 };
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if ((listener = ev_tcp_socket ()) == INVALID_SOCKET)
|
|
Packit Service |
24728e |
return -1;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
addr.sin_family = AF_INET;
|
|
Packit Service |
24728e |
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
|
|
Packit Service |
24728e |
addr.sin_port = 0;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if (bind (listener, (struct sockaddr *)&addr, addr_size))
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if (listen (listener, 1))
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET)
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
/* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */
|
|
Packit Service |
24728e |
/* when convenient, probably by just removing error checking altogether? */
|
|
Packit Service |
24728e |
if ((sock [1] = accept (listener, 0, 0)) < 0)
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
/* windows vista returns fantasy port numbers for sockets:
|
|
Packit Service |
24728e |
* example for two interconnected tcp sockets:
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364
|
|
Packit Service |
24728e |
* (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363
|
|
Packit Service |
24728e |
* (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363
|
|
Packit Service |
24728e |
* (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* wow! tridirectional sockets!
|
|
Packit Service |
24728e |
*
|
|
Packit Service |
24728e |
* this way of checking ports seems to work:
|
|
Packit Service |
24728e |
*/
|
|
Packit Service |
24728e |
if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size))
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size))
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
errno = WSAEINVAL;
|
|
Packit Service |
24728e |
if (addr_size != adr2_size
|
|
Packit Service |
24728e |
|| addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */
|
|
Packit Service |
24728e |
|| addr.sin_port != adr2.sin_port)
|
|
Packit Service |
24728e |
goto fail;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
closesocket (listener);
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
#if EV_SELECT_IS_WINSOCKET
|
|
Packit Service |
24728e |
filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]);
|
|
Packit Service |
24728e |
filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]);
|
|
Packit Service |
24728e |
#else
|
|
Packit Service |
24728e |
/* when select isn't winsocket, we also expect socket, connect, accept etc.
|
|
Packit Service |
24728e |
* to work on fds */
|
|
Packit Service |
24728e |
filedes [0] = sock [0];
|
|
Packit Service |
24728e |
filedes [1] = sock [1];
|
|
Packit Service |
24728e |
#endif
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
return 0;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
fail:
|
|
Packit Service |
24728e |
closesocket (listener);
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
|
|
Packit Service |
24728e |
if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
return -1;
|
|
Packit Service |
24728e |
}
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
#undef pipe
|
|
Packit Service |
24728e |
#define pipe(filedes) ev_pipe (filedes)
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
#define EV_HAVE_EV_TIME 1
|
|
Packit Service |
24728e |
ev_tstamp
|
|
Packit Service |
24728e |
ev_time (void)
|
|
Packit Service |
24728e |
{
|
|
Packit Service |
24728e |
FILETIME ft;
|
|
Packit Service |
24728e |
ULARGE_INTEGER ui;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
GetSystemTimeAsFileTime (&ft;;
|
|
Packit Service |
24728e |
ui.u.LowPart = ft.dwLowDateTime;
|
|
Packit Service |
24728e |
ui.u.HighPart = ft.dwHighDateTime;
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
/* msvc cannot convert ulonglong to double... yes, it is that sucky */
|
|
Packit Service |
24728e |
return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7;
|
|
Packit Service |
24728e |
}
|
|
Packit Service |
24728e |
|
|
Packit Service |
24728e |
#endif
|
|
Packit Service |
24728e |
|