Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

  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. #ifdef _WIN32
  32. #include <sys/timeb.h>
  33. /* note: the comment below could not be substantiated, but what would I care */
  34. /* MSDN says this is required to handle SIGFPE */
  35. volatile double SIGFPE_REQ = 0.0f;
  36. /* oh, the humanity! */
  37. static int
  38. ev_pipe (int filedes [2])
  39. {
  40. struct sockaddr_in addr = { 0 };
  41. int addr_size = sizeof (addr);
  42. SOCKET listener;
  43. SOCKET sock [2] = { -1, -1 };
  44. if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  45. return -1;
  46. addr.sin_family = AF_INET;
  47. addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  48. addr.sin_port = 0;
  49. if (bind (listener, (struct sockaddr *)&addr, addr_size))
  50. goto fail;
  51. if (getsockname(listener, (struct sockaddr *)&addr, &addr_size))
  52. goto fail;
  53. if (listen (listener, 1))
  54. goto fail;
  55. if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  56. goto fail;
  57. if (connect (sock[0], (struct sockaddr *)&addr, addr_size))
  58. goto fail;
  59. if ((sock[1] = accept (listener, 0, 0)) < 0)
  60. goto fail;
  61. closesocket (listener);
  62. #if EV_SELECT_IS_WINSOCKET
  63. filedes [0] = _open_osfhandle (sock [0], 0);
  64. filedes [1] = _open_osfhandle (sock [1], 0);
  65. #else
  66. /* when select isn't winsocket, we also expect socket, connect, accept etc.
  67. * to work on fds */
  68. filedes [0] = sock [0];
  69. filedes [1] = sock [1];
  70. #endif
  71. return 0;
  72. fail:
  73. closesocket (listener);
  74. if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
  75. if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
  76. return -1;
  77. }
  78. #undef pipe
  79. #define pipe(filedes) ev_pipe (filedes)
  80. static int
  81. ev_gettimeofday (struct timeval *tv, struct timezone *tz)
  82. {
  83. struct _timeb tb;
  84. _ftime (&tb);
  85. tv->tv_sec = (long)tb.time;
  86. tv->tv_usec = ((long)tb.millitm) * 1000;
  87. return 0;
  88. }
  89. #undef gettimeofday
  90. #define gettimeofday(tv,tz) ev_gettimeofday (tv, tz)
  91. #endif