/* fhandler_socket */
fhandler_socket::fhandler_socket ()
- : fhandler_base (), sun_path (NULL)
+ : fhandler_base (), sun_path (NULL), has_been_closed (0)
{
set_need_fork_fixup ();
prot_info_ptr = (LPWSAPROTOCOL_INFOA) cmalloc (HEAP_BUF,
{
WSABUF wsabuf = { len, (char *) ptr };
- if (is_nonblocking ())
+ if (is_nonblocking () || has_been_closed)
res = WSARecvFrom (get_socket (), &wsabuf, 1, &ret, (DWORD *) &flags,
from, fromlen,
NULL, NULL);
else
{
wsock_event wsock_evt;
- res = WSARecvFrom (get_socket (), &wsabuf, 1, &ret, (DWORD *) &flags,
- from, fromlen,
- wsock_evt.prepare (), NULL);
-
- if (res == SOCKET_ERROR && WSAGetLastError () == WSA_IO_PENDING)
- ret = res = wsock_evt.wait (get_socket (), (DWORD *) &flags);
+ long evt = (FD_CLOSE | ((flags & MSG_OOB) ? FD_OOB : FD_READ));
+ if (wsock_evt.prepare (get_socket (), evt))
+ {
+ do
+ {
+ if (!(res = wsock_evt.wait (get_socket (), has_been_closed)))
+ res = WSARecvFrom (get_socket (), &wsabuf, 1, &ret,
+ (DWORD *) &flags, from, fromlen,
+ NULL, NULL);
+ }
+ while (res == SOCKET_ERROR
+ && WSAGetLastError () == WSAEWOULDBLOCK
+ && !has_been_closed);
+ wsock_evt.release (get_socket ());
+ }
}
}
DWORD ret;
- if (is_nonblocking ())
+ if (is_nonblocking () || has_been_closed)
res = WSARecvFrom (get_socket (),
wsabuf, iovcnt, &ret, (DWORD *) &flags,
from, fromlen,
else
{
wsock_event wsock_evt;
- res = WSARecvFrom (get_socket (),
- wsabuf, iovcnt, &ret, (DWORD *) &flags,
- from, fromlen,
- wsock_evt.prepare (), NULL);
-
- if (res == SOCKET_ERROR && WSAGetLastError () == WSA_IO_PENDING)
- ret = res = wsock_evt.wait (get_socket (), (DWORD *) &flags);
+ long evt = (FD_CLOSE | ((flags & MSG_OOB) ? FD_OOB : FD_READ));
+ if (wsock_evt.prepare (get_socket (), evt))
+ {
+ do
+ {
+ if (!(res = wsock_evt.wait (get_socket (), has_been_closed)))
+ res = WSARecvFrom (get_socket (), wsabuf, iovcnt, &ret,
+ (DWORD *) &flags, from, fromlen,
+ NULL, NULL);
+ }
+ while (res == SOCKET_ERROR
+ && WSAGetLastError () == WSAEWOULDBLOCK
+ && !has_been_closed);
+ wsock_evt.release (get_socket ());
+ }
}
if (res == SOCKET_ERROR)
{
WSABUF wsabuf = { len, (char *) ptr };
- if (is_nonblocking ())
+ if (is_nonblocking () || has_been_closed)
res = WSASendTo (get_socket (), &wsabuf, 1, &ret,
flags & MSG_WINMASK,
(to ? (const struct sockaddr *) &sin : NULL), tolen,
else
{
wsock_event wsock_evt;
- res = WSASendTo (get_socket (), &wsabuf, 1, &ret,
- flags & MSG_WINMASK,
- (to ? (const struct sockaddr *) &sin : NULL), tolen,
- wsock_evt.prepare (), NULL);
-
- if (res == SOCKET_ERROR && WSAGetLastError () == WSA_IO_PENDING)
- ret = res = wsock_evt.wait (get_socket (), (DWORD *) &flags);
+ if (wsock_evt.prepare (get_socket (), FD_CLOSE | FD_WRITE))
+ {
+ do
+ {
+ res = WSASendTo (get_socket (), &wsabuf, 1, &ret,
+ flags & MSG_WINMASK,
+ (to ? (const struct sockaddr *) &sin : NULL),
+ tolen, NULL, NULL);
+ if (res != SOCKET_ERROR
+ || WSAGetLastError () != WSAEWOULDBLOCK)
+ break;
+ if (ret > 0)
+ {
+ res = 0;
+ break;
+ }
+ }
+ while (!(res = wsock_evt.wait (get_socket (), has_been_closed)));
+ wsock_evt.release (get_socket ());
+ }
}
}
DWORD ret;
- if (is_nonblocking ())
+ if (is_nonblocking () || has_been_closed)
res = WSASendTo (get_socket (), wsabuf, iovcnt, &ret, flags,
(struct sockaddr *) msg->msg_name,
msg->msg_namelen,
else
{
wsock_event wsock_evt;
- res = WSASendTo (get_socket (), wsabuf, iovcnt, &ret, flags,
- (struct sockaddr *) msg->msg_name,
- msg->msg_namelen,
- wsock_evt.prepare (), NULL);
-
- if (res == SOCKET_ERROR && WSAGetLastError () == WSA_IO_PENDING)
- ret = res = wsock_evt.wait (get_socket (), (DWORD *) &flags);
+ if (wsock_evt.prepare (get_socket (), FD_CLOSE | FD_WRITE))
+ {
+ do
+ {
+ res = WSASendTo (get_socket (), wsabuf, iovcnt, &ret,
+ flags, (struct sockaddr *) msg->msg_name,
+ msg->msg_namelen, NULL, NULL);
+ if (res != SOCKET_ERROR
+ || WSAGetLastError () != WSAEWOULDBLOCK)
+ break;
+ if (ret > 0)
+ {
+ res = 0;
+ break;
+ }
+ }
+ while (!(res = wsock_evt.wait (get_socket (), has_been_closed)));
+ wsock_evt.release (get_socket ());
+ }
}
if (res == SOCKET_ERROR)
int sscanf (const char *, const char *, ...);
} /* End of "C" section */
-LPWSAOVERLAPPED
-wsock_event::prepare ()
+bool
+wsock_event::prepare (int sock, long event_mask)
{
- LPWSAOVERLAPPED ret = NULL;
-
SetLastError (0);
- if ((event = WSACreateEvent ()) != WSA_INVALID_EVENT)
+ if ((event = WSACreateEvent ()) != WSA_INVALID_EVENT
+ && WSAEventSelect (sock, event, event_mask) == SOCKET_ERROR)
{
- memset (&ovr, 0, sizeof ovr);
- ovr.hEvent = event;
- ret = &ovr;
+ debug_printf ("WSAEventSelect: %E");
+ WSACloseEvent (event);
+ event = WSA_INVALID_EVENT;
}
- else if (GetLastError () == ERROR_PROC_NOT_FOUND) /* winsock2 not available */
- WSASetLastError (0);
-
- debug_printf ("%d = wsock_event::prepare ()", ret);
- return ret;
+ return event != WSA_INVALID_EVENT;
}
int
-wsock_event::wait (int socket, LPDWORD flags)
+wsock_event::wait (int sock, int &closed)
{
int ret = -1;
+ DWORD wsa_err = 0;
WSAEVENT ev[2] = { event, signal_arrived };
- DWORD len;
-
switch (WSAWaitForMultipleEvents (2, ev, FALSE, WSA_INFINITE, FALSE))
{
case WSA_WAIT_EVENT_0:
- if (WSAGetOverlappedResult (socket, &ovr, &len, FALSE, flags))
- ret = (int) len;
- break;
- case WSA_WAIT_EVENT_0 + 1:
- if (!CancelIo ((HANDLE) socket))
+ WSANETWORKEVENTS evts;
+ memset (&evts, 0, sizeof evts);
+ WSAEnumNetworkEvents (sock, event, &evts);
+ if (evts.lNetworkEvents & FD_READ)
+ {
+ if (evts.iErrorCode[FD_READ_BIT])
+ wsa_err = evts.iErrorCode[FD_READ_BIT];
+ else
+ ret = 0;
+ }
+ else if (evts.lNetworkEvents & FD_WRITE)
+ {
+ if (evts.iErrorCode[FD_WRITE_BIT])
+ wsa_err = evts.iErrorCode[FD_WRITE_BIT];
+ else
+ ret = 0;
+ }
+ if (evts.lNetworkEvents & FD_CLOSE)
{
- debug_printf ("CancelIo() %E, fallback to blocking io");
- WSAGetOverlappedResult (socket, &ovr, &len, TRUE, flags);
+ closed = 1;
+ if (!wsa_err && evts.iErrorCode[FD_CLOSE_BIT])
+ wsa_err = evts.iErrorCode[FD_CLOSE_BIT];
+ else
+ ret = 0;
}
- else
- WSASetLastError (WSAEINTR);
+ if (wsa_err)
+ WSASetLastError (wsa_err);
break;
- case WSA_WAIT_FAILED:
+ case WSA_WAIT_EVENT_0 + 1:
+ WSASetLastError (WSAEINTR);
break;
- default: /* Should be impossible. *LOL* */
+ default:
WSASetLastError (WSAEFAULT);
- break;
}
- WSACloseEvent (event);
- event = NULL;
return ret;
}
+void
+wsock_event::release (int sock)
+{
+ WSAEventSelect (sock, event, 0);
+ WSACloseEvent (event);
+ unsigned long non_block = 0;
+ ioctlsocket (sock, FIONBIO, &non_block);
+}
+
WSADATA wsadata;
static fhandler_socket *