+2006-07-31 Corinna Vinschen <corinna@vinschen.de>
+
+ * fhandler_socket.cc (fhandler_socket::evaluate_events): Circumvent
+ potential race condition.
+ (fhandler_socket::recv_internal): Fix MSG_PEEK bug on blocking sockets
+ and simplify recv loop.
+ (fhandler_socket::send_internal): Only lock when changing wsock_events.
+
2006-07-31 Corinna Vinschen <corinna@vinschen.de>
* autoload.cc: Drop LoadDLLprime for wsock32 since no wsock32 function
bool erase)
{
int ret = 0;
+ long events_now = 0;
WSANETWORKEVENTS evts = { 0 };
if (!(WSAEnumNetworkEvents (get_socket (), wsock_evt, &evts)))
{
LOCK_EVENTS;
wsock_events->events |= evts.lNetworkEvents;
+ events_now = (wsock_events->events & event_mask);
if (evts.lNetworkEvents & FD_CONNECT)
wsock_events->connect_errorcode = evts.iErrorCode[FD_CONNECT_BIT];
UNLOCK_EVENTS;
}
LOCK_EVENTS;
- if ((events = (wsock_events->events & event_mask)) != 0)
+ if ((events = events_now) != 0
+ || (events = (wsock_events->events & event_mask)) != 0)
{
if (events & FD_CONNECT)
{
int evt_mask = FD_READ | ((flags & MSG_OOB) ? FD_OOB : 0);
flags &= MSG_WINMASK;
- if (flags & MSG_PEEK)
+ /* Note: Don't call WSARecvFrom(MSG_PEEK) without actually having data
+ waiting in the buffers, otherwise the event handling gets messed up
+ for some reason. */
+ while (!(res = wait_for_events (evt_mask | FD_CLOSE)))
{
- LOCK_EVENTS;
res = WSARecvFrom (get_socket (), wsabuf, wsacnt, &ret,
&flags, from, fromlen, NULL, NULL);
- wsock_events->events &= ~evt_mask;
- UNLOCK_EVENTS;
- }
- else
- {
- do
- {
- LOCK_EVENTS;
- res = WSARecvFrom (get_socket (), wsabuf, wsacnt, &ret,
- &flags, from, fromlen, NULL, NULL);
- wsock_events->events &= ~evt_mask;
- UNLOCK_EVENTS;
- }
- while (res && WSAGetLastError () == WSAEWOULDBLOCK
- && !(res = wait_for_events (evt_mask | FD_CLOSE)))
- ;
+ if (!res || WSAGetLastError () != WSAEWOULDBLOCK)
+ break;
}
if (res == SOCKET_ERROR)
do
{
- LOCK_EVENTS;
if ((res = WSASendTo (get_socket (), wsabuf, wsacnt, &ret,
flags & MSG_WINMASK, to, tolen, NULL, NULL))
&& (err = WSAGetLastError ()) == WSAEWOULDBLOCK)
- wsock_events->events &= ~FD_WRITE;
- UNLOCK_EVENTS;
+ {
+ LOCK_EVENTS;
+ wsock_events->events &= ~FD_WRITE;
+ UNLOCK_EVENTS;
+ }
}
while (res && err == WSAEWOULDBLOCK
&& !(res = wait_for_events (FD_WRITE | FD_CLOSE)));