diff options
Diffstat (limited to 'winsup/cygwin/fhandler_socket.cc')
-rw-r--r-- | winsup/cygwin/fhandler_socket.cc | 89 |
1 files changed, 71 insertions, 18 deletions
diff --git a/winsup/cygwin/fhandler_socket.cc b/winsup/cygwin/fhandler_socket.cc index 33e9b39f3..c2af990df 100644 --- a/winsup/cygwin/fhandler_socket.cc +++ b/winsup/cygwin/fhandler_socket.cc @@ -31,6 +31,7 @@ #include "dtable.h" #include "cygheap.h" #include "sigproc.h" +#include "wsock_event.h" #define SECRET_EVENT_NAME "cygwin.local_socket.secret.%d.%08x-%08x-%08x-%08x" #define ENTROPY_SOURCE_NAME "/dev/urandom" @@ -251,38 +252,90 @@ fhandler_socket::fstat (struct stat *buf, path_conv *pc) return fh.fstat (buf, pc); } -extern "C" int cygwin_recv (int, void *, int, unsigned int); - -int __stdcall -fhandler_socket::read (void *ptr, size_t len) +int +fhandler_socket::recv (void *ptr, size_t len, unsigned int flags) { + int res = -1; + wsock_event wsock_evt; + LPWSAOVERLAPPED ovr; + sigframe thisframe (mainthread); - int res = cygwin_recv (get_fd (), (char *) ptr, len, 0); -#if 0 - if (res == SOCKET_ERROR) - set_winsock_errno (); -#endif + if (is_nonblocking () || !(ovr = wsock_evt.prepare ())) + { + debug_printf ("Fallback to winsock 1 recv call"); + if ((res = ::recv (get_socket (), (char *) ptr, len, flags)) + == SOCKET_ERROR) + { + set_winsock_errno (); + res = -1; + } + } + else + { + WSABUF wsabuf = { len, (char *) ptr }; + DWORD ret = 0; + if (WSARecv (get_socket (), &wsabuf, 1, &ret, (DWORD *)&flags, + ovr, NULL) != SOCKET_ERROR) + res = ret; + else if ((res = WSAGetLastError ()) != WSA_IO_PENDING) + { + set_winsock_errno (); + res = -1; + } + else if ((res = wsock_evt.wait (get_socket (), (DWORD *)&flags)) == -1) + set_winsock_errno (); + } return res; } -extern "C" int cygwin_send (int, const void *, int, unsigned int); +int __stdcall +fhandler_socket::read (void *ptr, size_t len) +{ + return recv (ptr, len, 0); +} int -fhandler_socket::write (const void *ptr, size_t len) +fhandler_socket::send (const void *ptr, size_t len, unsigned int flags) { + int res = -1; + wsock_event wsock_evt; + LPWSAOVERLAPPED ovr; + sigframe thisframe (mainthread); - int res = cygwin_send (get_fd (), (const char *) ptr, len, 0); -#if 0 - if (res == SOCKET_ERROR) + if (is_nonblocking () || !(ovr = wsock_evt.prepare ())) { - set_winsock_errno (); - if (get_errno () == ECONNABORTED || get_errno () == ECONNRESET) - _raise (SIGPIPE); + debug_printf ("Fallback to winsock 1 send call"); + if ((res = ::send (get_socket (), (const char *) ptr, len, flags)) + == SOCKET_ERROR) + { + set_winsock_errno (); + res = -1; + } + } + else + { + WSABUF wsabuf = { len, (char *) ptr }; + DWORD ret = 0; + if (WSASend (get_socket (), &wsabuf, 1, &ret, (DWORD)flags, + ovr, NULL) != SOCKET_ERROR) + res = ret; + else if ((res = WSAGetLastError ()) != WSA_IO_PENDING) + { + set_winsock_errno (); + res = -1; + } + else if ((res = wsock_evt.wait (get_socket (), (DWORD *)&flags)) == -1) + set_winsock_errno (); } -#endif return res; } +int +fhandler_socket::write (const void *ptr, size_t len) +{ + return send (ptr, len, 0); +} + /* Cygwin internal */ int fhandler_socket::close () |