Make the event loop expose a Windows event interface.
This allows event loop users to specify Win32 events to wait on, thus making the event loop more flexible.
This commit is contained in:
parent
611217c96e
commit
2f9a1d4ab5
2 changed files with 29 additions and 11 deletions
37
src/event.c
37
src/event.c
|
@ -72,9 +72,11 @@ void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
|
||||||
|
|
||||||
io->fd = fd;
|
io->fd = fd;
|
||||||
#ifdef HAVE_MINGW
|
#ifdef HAVE_MINGW
|
||||||
io->event = WSACreateEvent();
|
if (io->fd != -1) {
|
||||||
if (io->event == WSA_INVALID_EVENT)
|
io->event = WSACreateEvent();
|
||||||
abort();
|
if (io->event == WSA_INVALID_EVENT)
|
||||||
|
abort();
|
||||||
|
}
|
||||||
event_count++;
|
event_count++;
|
||||||
#endif
|
#endif
|
||||||
io->cb = cb;
|
io->cb = cb;
|
||||||
|
@ -87,10 +89,19 @@ void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_MINGW
|
||||||
|
void io_add_event(io_t *io, io_cb_t cb, void *data, WSAEVENT event) {
|
||||||
|
io_add(io, cb, data, -1, 0);
|
||||||
|
io->event = event;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void io_set(io_t *io, int flags) {
|
void io_set(io_t *io, int flags) {
|
||||||
if (flags == io->flags)
|
if (flags == io->flags)
|
||||||
return;
|
return;
|
||||||
io->flags = flags;
|
io->flags = flags;
|
||||||
|
if (io->fd == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
#ifndef HAVE_MINGW
|
#ifndef HAVE_MINGW
|
||||||
if(flags & IO_READ)
|
if(flags & IO_READ)
|
||||||
|
@ -119,7 +130,7 @@ void io_del(io_t *io) {
|
||||||
|
|
||||||
io_set(io, 0);
|
io_set(io, 0);
|
||||||
#ifdef HAVE_MINGW
|
#ifdef HAVE_MINGW
|
||||||
if (WSACloseEvent(io->event) == FALSE)
|
if (io->fd != -1 && WSACloseEvent(io->event) == FALSE)
|
||||||
abort();
|
abort();
|
||||||
event_count--;
|
event_count--;
|
||||||
#endif
|
#endif
|
||||||
|
@ -334,13 +345,17 @@ bool event_loop(void) {
|
||||||
if (!io)
|
if (!io)
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
WSANETWORKEVENTS network_events;
|
if (io->fd == -1) {
|
||||||
if (WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0)
|
io->cb(io->data, 0);
|
||||||
return false;
|
} else {
|
||||||
if (network_events.lNetworkEvents & WRITE_EVENTS)
|
WSANETWORKEVENTS network_events;
|
||||||
io->cb(io->data, IO_WRITE);
|
if (WSAEnumNetworkEvents(io->fd, io->event, &network_events) != 0)
|
||||||
if (network_events.lNetworkEvents & READ_EVENTS)
|
return false;
|
||||||
io->cb(io->data, IO_READ);
|
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
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,9 @@ typedef struct signal_t {
|
||||||
extern struct timeval now;
|
extern struct timeval now;
|
||||||
|
|
||||||
extern void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags);
|
extern void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags);
|
||||||
|
#ifdef HAVE_MINGW
|
||||||
|
extern void io_add_event(io_t *io, io_cb_t cb, void* data, WSAEVENT event);
|
||||||
|
#endif
|
||||||
extern void io_del(io_t *io);
|
extern void io_del(io_t *io);
|
||||||
extern void io_set(io_t *io, int flags);
|
extern void io_set(io_t *io, int flags);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue