static const long WRITE_EVENTS = FD_WRITE | FD_CONNECT;
static DWORD event_count = 0;
#endif
-static volatile bool running;
+static bool running;
static int io_compare(const io_t *a, const io_t *b) {
#ifndef HAVE_MINGW
io->fd = fd;
#ifdef HAVE_MINGW
- io->event = WSACreateEvent();
- if (io->event == WSA_INVALID_EVENT)
- abort();
+ if (io->fd != -1) {
+ io->event = WSACreateEvent();
+ if (io->event == WSA_INVALID_EVENT)
+ abort();
+ }
event_count++;
#endif
io->cb = cb;
abort();
}
+#ifdef HAVE_MINGW
+void io_add_event(io_t *io, io_cb_t cb, void *data, WSAEVENT event) {
+ io->event = event;
+ io_add(io, cb, data, -1, 0);
+}
+#endif
+
void io_set(io_t *io, int flags) {
if (flags == io->flags)
return;
io->flags = flags;
+ if (io->fd == -1)
+ return;
#ifndef HAVE_MINGW
if(flags & IO_READ)
io_set(io, 0);
#ifdef HAVE_MINGW
- if (WSACloseEvent(io->event) == FALSE)
+ if (io->fd != -1 && WSACloseEvent(io->event) == FALSE)
abort();
event_count--;
#endif
fd_set writable;
while(running) {
- memcpy(&readable, &readfds, sizeof readable);
- memcpy(&writable, &writefds, sizeof writable);
struct timeval diff;
struct timeval *tv = get_time_remaining(&diff);
+ memcpy(&readable, &readfds, sizeof readable);
+ memcpy(&writable, &writefds, sizeof writable);
int fds = 0;
DWORD timeout_ms = tv ? (tv->tv_sec * 1000 + tv->tv_usec / 1000 + 1) : WSA_INFINITE;
if (!event_count) {
- LeaveCriticalSection(&mutex);
Sleep(timeout_ms);
- EnterCriticalSection(&mutex);
continue;
}
event_index++;
}
- LeaveCriticalSection(&mutex);
DWORD result = WSAWaitForMultipleEvents(event_count, events, FALSE, timeout_ms, FALSE);
- EnterCriticalSection(&mutex);
WSAEVENT event;
if (result >= WSA_WAIT_EVENT_0 && result < WSA_WAIT_EVENT_0 + event_count)
if (!io)
abort();
- WSANETWORKEVENTS network_events;
- if (WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0)
- return false;
- if (network_events.lNetworkEvents & WRITE_EVENTS)
- io->cb(io->data, IO_WRITE);
- if (network_events.lNetworkEvents & READ_EVENTS)
- io->cb(io->data, IO_READ);
+ if (io->fd == -1) {
+ io->cb(io->data, 0);
+ } else {
+ WSANETWORKEVENTS network_events;
+ if (WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0)
+ return false;
+ if (network_events.lNetworkEvents & WRITE_EVENTS)
+ io->cb(io->data, IO_WRITE);
+ if (network_events.lNetworkEvents & READ_EVENTS)
+ io->cb(io->data, IO_READ);
+ }
}
#endif
return true;
}
-void event_flush_output(void) {
- for splay_each(io_t, io, &io_tree)
- if(io->flags & IO_WRITE)
- io->cb(io->data, IO_WRITE);
-}
-
void event_exit(void) {
running = false;
}