-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
#include "array.h" | ||
#include <sys/event.h> | ||
#include "aio.h" | ||
#include "container/array.h" | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
|
||
|
||
typedef struct kevent kevent_t; | ||
struct event_ctx_t | ||
{ | ||
int fd; | ||
kevent_t * events; | ||
int event_count; | ||
int event_cap; | ||
}; | ||
|
||
event_ctx_t * event_create() | ||
{ | ||
event_ctx_t * ctx = (event_ctx_t*)malloc(sizeof(event_ctx_t)); | ||
ctx->fd = kqueue(); | ||
ctx->event_count = 0; | ||
ctx->event_cap = 10; | ||
ctx->events = (kevent_t*)malloc(sizeof(kevent_t)*ctx->event_cap); | ||
return ctx; | ||
} | ||
|
||
void event_destroy(event_ctx_t * ctx) | ||
{ | ||
free(ctx->events); | ||
free(ctx); | ||
} | ||
|
||
void event_add(event_ctx_t * ctx, int fd) | ||
{ | ||
struct kevent change; | ||
EV_SET(&change, fd, EVFILT_READ | EVFILT_WRITE | EVFILT_EXCEPT, EV_ADD, 0, 0, NULL); | ||
kevent(ctx->fd, &change, 1,NULL, 0, NULL); | ||
if (ctx->event_count == ctx->event_cap) { | ||
ctx->event_cap *= 2; | ||
ctx->events = realloc(ctx->events, sizeof(kevent_t) * ctx->event_cap); | ||
|
||
} | ||
ctx->event_count ++; | ||
} | ||
|
||
void event_del(event_ctx_t * ctx, int fd) | ||
{ | ||
struct kevent change; | ||
EV_SET(&change, fd, EVFILT_READ | EVFILT_WRITE | EVFILT_EXCEPT, EV_DELETE, 0, 0, NULL); | ||
kevent(ctx->fd, &change, 1,NULL, 0, NULL); | ||
ctx->event_count --; | ||
} | ||
|
||
int event_wait(event_ctx_t * ctx, array_t * fired_events, long long ms) | ||
{ | ||
struct timespec timeout; | ||
timeout.tv_sec = ms/1000; // 5秒 | ||
timeout.tv_nsec = (ms%1000)*1000; | ||
int ready = kevent(ctx->fd, NULL, 0, ctx->events, ctx->event_cap, &timeout); | ||
array_resize(fired_events, ready); | ||
for (int i = 0; i < ready; ++ i) { | ||
struct kevent e = ctx->events[i]; | ||
int events = 0; | ||
if (e.filter & EVFILT_READ) events |= IO_READ; | ||
if (e.filter & EVFILT_WRITE) events |= IO_WRITE; | ||
if (e.filter & EVFILT_EXCEPT) events = IO_READ | IO_WRITE; | ||
array_set(fired_events, i, encode_event(e.ident, events)); | ||
} | ||
return ready; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "aio.h" | ||
#include "macros.h" | ||
#ifdef WIN32 | ||
#include <winsock2.h> | ||
#else | ||
#include <sys/select.h> | ||
#endif | ||
#include <stdlib.h> | ||
|
||
struct event_ctx_t | ||
{ | ||
int max_fd; | ||
fd_set all_fds; | ||
}; | ||
|
||
event_ctx_t * event_create() | ||
{ | ||
event_ctx_t * ctx = (event_ctx_t*)malloc(sizeof(event_ctx_t)); | ||
ctx->max_fd = 0; | ||
FD_ZERO(&ctx->all_fds); | ||
return ctx; | ||
} | ||
|
||
void event_destroy(event_ctx_t * ctx) | ||
{ | ||
free(ctx); | ||
} | ||
|
||
void event_add(event_ctx_t * ctx, int fd) | ||
{ | ||
FD_SET(fd, &ctx->all_fds); | ||
if (fd > ctx->max_fd) { | ||
ctx->max_fd = fd; | ||
} | ||
} | ||
|
||
void event_del(event_ctx_t * ctx, int fd) | ||
{ | ||
FD_CLR(fd, &ctx->all_fds); | ||
if (ctx->max_fd == fd) { | ||
ctx->max_fd --; | ||
while (!FD_ISSET(ctx->max_fd, &ctx->all_fds)) { | ||
ctx->max_fd --; | ||
} | ||
} | ||
} | ||
|
||
int event_wait(event_ctx_t * ctx, array_t * fired_events, long long ms) | ||
{ | ||
fd_set read_fds = ctx->all_fds; | ||
fd_set write_fds = ctx->all_fds; | ||
fd_set execpt_fds = ctx->all_fds; | ||
struct timeval tv; | ||
tv.tv_sec = ms/1000; | ||
tv.tv_usec = (ms%1000)*1000; | ||
int ready = select(ctx->max_fd + 1, &read_fds, &write_fds, &execpt_fds, ms == -1L? NULL : &tv); | ||
array_resize(fired_events, ready); | ||
if (ready > 0) { | ||
for (int i = 0; i<= ctx->max_fd; ++ i) { | ||
int events = 0; | ||
if (FD_ISSET(i, &read_fds)) { events |= IO_READ; } | ||
if (FD_ISSET(i, &write_fds)) { events |= IO_WRITE; } | ||
if (FD_ISSET(i, &execpt_fds)) { events = IO_READ | IO_WRITE; } | ||
if (events) { | ||
array_set(fired_events, i, encode_event(i, events)); | ||
} | ||
} | ||
} | ||
|
||
return ready; | ||
} |