From 18569e3e9f1fdf0765012d1f96b16e3349a46c1a Mon Sep 17 00:00:00 2001 From: findstr Date: Mon, 8 Aug 2016 12:30:22 +0800 Subject: [PATCH] refine code --- lualib/gate.lua | 3 + lualib/silly/core.lua | 36 +++++----- lualib/spacker.lua | 70 -------------------- silly-src/silly_server.c | 82 ----------------------- silly-src/silly_server.h | 23 ------- silly-src/silly_socket.c | 134 +++++++++++++++++--------------------- silly-src/socket_epoll.h | 55 ++++++++++++++++ silly-src/socket_kevent.h | 76 +++++++++++++++++++++ silly-src/socket_poll.h | 123 +--------------------------------- 9 files changed, 215 insertions(+), 387 deletions(-) delete mode 100644 lualib/spacker.lua delete mode 100644 silly-src/silly_server.c delete mode 100644 silly-src/silly_server.h create mode 100644 silly-src/socket_epoll.h create mode 100644 silly-src/socket_kevent.h diff --git a/lualib/gate.lua b/lualib/gate.lua index 7c59c5a6..a517019f 100644 --- a/lualib/gate.lua +++ b/lualib/gate.lua @@ -228,6 +228,9 @@ function gate.send(fd, ...) if sc == nil then return false end + if sc.__close then + return false + end if sc.pack then d, sz = sc.pack(...) else diff --git a/lualib/silly/core.lua b/lualib/silly/core.lua index fb6e31c4..a28efa7f 100644 --- a/lualib/silly/core.lua +++ b/lualib/silly/core.lua @@ -6,6 +6,13 @@ local core = {} local tinsert = table.insert local tremove = table.remove +local corunning = coroutine.running +local coyield = coroutine.yield +local coresume = coroutine.resume +coroutine.running = nil +coroutine.yield = nil +coroutine.resume = nil + --coroutine pool will be dynamic size --so use the weaktable local copool = {} @@ -15,19 +22,19 @@ setmetatable(copool, weakmt) local function cocreate(f) local co = table.remove(copool) if co then - coroutine.resume(co, "STARTUP", f) + coresume(co, "STARTUP", f) return co end local function cocall() while true do - local ret, func = coroutine.yield("EXIT") + local ret, func = coyield("EXIT") if ret ~= "STARTUP" then print("create coroutine fail", ret) print(debug.traceback()) return end - local ok, err = core.pcall(func, coroutine.yield()) + local ok, err = core.pcall(func, coyield()) if ok == false then print("cocall", err) print(debug.traceback()) @@ -36,8 +43,8 @@ local function cocreate(f) end co = coroutine.create(cocall) - coroutine.resume(co) --wakeup the new coroutine - coroutine.resume(co, "STARTUP", f) --pass the function handler + coresume(co) --wakeup the new coroutine + coresume(co, "STARTUP", f) --pass the function handler if #copool > 100 then print("coroutine pool large than 100", #copool) end @@ -50,7 +57,7 @@ end core.udpwrite = function(fd, p, sz, addr) return silly.udpsend(fd, p, sz, addr) == 0 end -core.running = coroutine.running +core.running = corunning core.quit = silly.quit core.tostring = silly.tostring core.genid = silly.genid @@ -132,7 +139,7 @@ function dispatch_wakeup() if not param then param = {} end - waityield(co, coroutine.resume(co, "WAKEUP", table.unpack(param))) + waityield(co, coresume(co, "WAKEUP", table.unpack(param))) end function core.fork(func) @@ -144,12 +151,12 @@ function core.fork(func) end function core.wait() - local co = coroutine.running() + local co = corunning() assert(wakeup_co_status[co] == nil) assert(sleep_co_session[co] == nil) assert(wait_co_status[co] == nil) wait_co_status[co] = "WAIT" - return waitresume(co, coroutine.yield("WAIT")) + return waitresume(co, coyield("WAIT")) end function core.wakeup(co, ...) @@ -161,11 +168,11 @@ function core.wakeup(co, ...) end function core.sleep(ms) - local co = coroutine.running() + local co = corunning() local session = silly.timeout(ms) sleep_session_co[session] = co sleep_co_session[co] = session - waitresume(co, coroutine.yield("SLEEP")) + waitresume(co, coyield("SLEEP")) end function core.timeout(ms, func) @@ -178,7 +185,7 @@ end function core.start(func, ...) local co = cocreate(func) - waityield(co, coroutine.resume(co, ...)) + waityield(co, coresume(co, ...)) end @@ -248,7 +255,7 @@ local function doconnect(ip, dispatch, bind, dofunc) return nil end assert(socket_connect[fd] == nil) - socket_connect[fd] = coroutine.running() + socket_connect[fd] = corunning() local ok = core.wait() socket_connect[fd] = nil if ok ~= true then @@ -260,7 +267,6 @@ local function doconnect(ip, dispatch, bind, dofunc) end function core.connect(ip, dispatch, bind) - return doconnect(ip, dispatch, bind, silly.connect) end @@ -348,7 +354,7 @@ local function dispatch(type, fd, message, ...) --check if the socket has closed if dispatch then --have ready close local co = cocreate(dispatch) - waityield(co, coroutine.resume(co, type, fd, message, ...)) + waityield(co, coresume(co, type, fd, message, ...)) end dispatch_wakeup() end diff --git a/lualib/spacker.lua b/lualib/spacker.lua deleted file mode 100644 index cf760bbf..00000000 --- a/lualib/spacker.lua +++ /dev/null @@ -1,70 +0,0 @@ -local bp = require "binpacket" -local lp = require "linepacket" -local rp = require "rawpacket" - -local bpacket = nil - -local spacker = { - mode = nil -} - -local spacker_bp = nil -local spacker_rp = nil - -local function create_once(self, mode, p, packer) - if p == nil then - p = { - mode = mode, - packer = packer, - packer_inst = packer:create(), - } - - self.__index = self - setmetatable(p, self) - end - - return p; -end - -function spacker:create(mode) - if mode == nil then - mode = self.mode - end - - assert(mode == "bin" or mode == "line" or mode == "raw") - - if mode == "bin" then - return create_once(self, mode, spacker_bp, bp) - elseif mode == "raw" then - return create_once(self, mode, spacker_rp, rp) - elseif mode == "line" then - local t = { - mode = "line", - packer = lp, - packer_inst = lp.create(), - } - - self.__index = self - setmetatable(t, self) - - return t - end - - return nil -end - -function spacker:push(fd, data, size) - self.packer_inst = self.packer.push(self.packer_inst, fd, data, size) -end - - -function spacker:pop() - return self.packer.pop(self.packer_inst); -end - -function spacker:pack(data) - return self.packer.pack(data) -end - -return spacker - diff --git a/silly-src/silly_server.c b/silly-src/silly_server.c deleted file mode 100644 index d336623a..00000000 --- a/silly-src/silly_server.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include - -#include "silly_queue.h" -#include "silly_malloc.h" -#include "silly_worker.h" - -#include "silly_server.h" - -struct silly_server { - int workcnt; - struct silly_worker **worklist; -}; - - -struct silly_server *SILLY_SERVER; - -int silly_server_init() -{ - SILLY_SERVER = (struct silly_server *)silly_malloc(sizeof(struct silly_server)); - assert(SILLY_SERVER); - SILLY_SERVER->workcnt = 0; - SILLY_SERVER->worklist = NULL; - - return 0; -} -int silly_server_exit() -{ - int i; - for (i = 0; i < SILLY_SERVER->workcnt; i++) - silly_worker_free(SILLY_SERVER->worklist[i]); - silly_free(SILLY_SERVER->worklist); - silly_free(SILLY_SERVER); - - return 0; -} - - -int silly_server_open() -{ - struct silly_server *s = SILLY_SERVER; - - s->worklist = silly_realloc(s->worklist, (s->workcnt + 1) * sizeof(s->worklist[0])); - s->worklist[s->workcnt] = silly_worker_create(s->workcnt); - - return s->workcnt++; -} - -int silly_server_push(int handle, struct silly_message *msg) -{ - assert(handle < SILLY_SERVER->workcnt); - return silly_worker_push(SILLY_SERVER->worklist[handle], msg); -} - -int silly_server_balance(int workid, int sid) -{ - if (workid == -1) - return sid % SILLY_SERVER->workcnt; - - assert(workid < SILLY_SERVER->workcnt); - - return workid; -} - -int silly_server_start(int handle, const struct silly_config *config) -{ - return silly_worker_start(SILLY_SERVER->worklist[handle], config); -} - -void silly_server_stop(int handle) -{ - return silly_worker_stop(SILLY_SERVER->worklist[handle]); -} - -int silly_server_dispatch(int handle) -{ - assert(handle < SILLY_SERVER->workcnt); - return silly_worker_dispatch(SILLY_SERVER->worklist[handle]); -} - - diff --git a/silly-src/silly_server.h b/silly-src/silly_server.h deleted file mode 100644 index aa948dbc..00000000 --- a/silly-src/silly_server.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _SILLY_SERVER_H -#define _SILLY_SERVER_H - -struct silly_message; -struct silly_config; - -int silly_server_init(); -int silly_server_exit(); - -int silly_server_open(); - -//for connect balance -int silly_server_balance(int workid, int sid); - -int silly_server_push(int handle, struct silly_message *msg); - -int silly_server_start(int handle, const struct silly_config *config); -void silly_server_stop(int handle); - -int silly_server_dispatch(int handle); - - -#endif diff --git a/silly-src/silly_socket.c b/silly-src/silly_socket.c index 278d89f8..ecda615f 100644 --- a/silly-src/silly_socket.c +++ b/silly-src/silly_socket.c @@ -133,6 +133,7 @@ allocsocket(struct silly_socket *ss, enum stype type, int protocol) s->protocol = protocol; s->presize = MIN_READBUFF_LEN; s->sid = id; + s->fd = -1; return s; } } @@ -141,15 +142,6 @@ allocsocket(struct silly_socket *ss, enum stype type, int protocol) return NULL; } -static __inline void -freesocket(struct silly_socket *ss, struct socket *s) -{ - (void)ss; - assert(s->wlhead.next == NULL); - s->protocol = 0; - s->type = STYPE_RESERVE; -} - static void wlist_append(struct socket *s, uint8_t *buff, size_t offset, size_t size, const struct sockaddr *addr) { @@ -189,8 +181,18 @@ wlist_empty(struct socket *s) return s->wlhead.next == NULL ? 1 : 0; } +static inline void +freesocket(struct silly_socket *ss, struct socket *s) +{ + (void)ss; + wlist_free(s); + assert(s->wlhead.next == NULL); + __sync_synchronize(); + s->type = STYPE_RESERVE; +} + static struct socket * -newsocket(struct silly_socket *ss, struct socket *s, int fd, enum stype type) +newsocket(struct silly_socket *ss, struct socket *s, int fd, enum stype type, void (* report)(struct silly_socket *ss, struct socket *s, int err)) { int err; if (s == NULL) @@ -201,10 +203,13 @@ newsocket(struct silly_socket *ss, struct socket *s, int fd, enum stype type) } assert(s->type == type || s->type == STYPE_ALLOCED); assert(s->presize == MIN_READBUFF_LEN); + assert(fd >= 0); s->fd = fd; s->type = type; err = sp_add(ss->spfd, fd, s); if (err < 0) { + if (report) + report(ss, s, errno); perror("newsocket"); close(fd); freesocket(ss, s); @@ -221,7 +226,6 @@ delsocket(struct silly_socket *ss, struct socket *s) fprintf(stderr, fmt, s->sid, s->type); return ; } - wlist_free(s); sp_del(ss->spfd, s->fd); close(s->fd); freesocket(ss, s); @@ -306,7 +310,7 @@ report_accept(struct silly_socket *ss, struct socket *listen) nonblock(fd); keepalive(fd); nodelay(fd); - s = newsocket(ss, NULL, fd, STYPE_SOCKET); + s = newsocket(ss, NULL, fd, STYPE_SOCKET, NULL); if (s == NULL) return; sa->sid = s->sid; @@ -316,24 +320,23 @@ report_accept(struct silly_socket *ss, struct socket *listen) } static void -report_close_sid(struct silly_socket *ss, int sid) -{ - struct silly_message_socket *sc = silly_malloc(sizeof(*sc)); - sc->type = SILLY_SCLOSE; - sc->sid = sid; - sc->ud = 0; - silly_worker_push(tocommon(sc)); - return ; -} - -static void -report_close(struct silly_socket *ss, struct socket *s) +report_close(struct silly_socket *ss, struct socket *s, int err) { (void)ss; + int or; + struct silly_message_socket *sc; if (s->type == STYPE_HALFCLOSE)//don't notify the active close return ; - assert(s->type == STYPE_SOCKET || s->type == STYPE_RESERVE); - report_close_sid(ss, s->sid); + or = s->type == STYPE_LISTEN ? 1 : 0; + or += s->type == STYPE_SOCKET ? 1 : 0; + or += s->type == STYPE_CONNECTING ? 1 : 0; + or += s->type == STYPE_ALLOCED ? 1 : 0; + assert(or > 0); + sc = silly_malloc(sizeof(*sc)); + sc->type = SILLY_SCLOSE; + sc->sid = s->sid; + sc->ud = err; + silly_worker_push(tocommon(sc)); return ; } @@ -352,24 +355,7 @@ report_data(struct silly_socket *ss, struct socket *s, int type, uint8_t *data, return ; }; -static void -report_error(struct silly_socket *ss, struct socket *s, int err) -{ - (void)ss; - int or = s->type == STYPE_LISTEN ? 1 : 0; - or += s->type == STYPE_SOCKET ? 1 : 0; - or += s->type == STYPE_CONNECTING ? 1 : 0; - or += s->type == STYPE_ALLOCED ? 1 : 0; - assert(or > 0); - struct silly_message_socket *se = silly_malloc(sizeof(*se)); - se->type = SILLY_SCLOSE; - se->sid = s->sid; - se->ud = err; - silly_worker_push(tocommon(se)); - return ; -} - -static __inline int +static inline int checkconnected(int fd) { int ret; @@ -394,7 +380,7 @@ report_connected(struct silly_socket *ss, struct socket *s) int err; err = checkconnected(s->fd); if (err < 0) { //check ok - report_error(ss, s, errno); + report_close(ss, s, errno); delsocket(ss, s); return ; } @@ -488,7 +474,7 @@ sendudp(int fd, uint8_t *data, size_t sz, const struct sockaddr *addr) case EINTR: continue; case ETRYAGAIN: - return 0; + return -2; default: return -1; } @@ -513,7 +499,7 @@ forward_msg_tcp(struct silly_socket *ss, struct socket *s) } else { silly_free(buff); if (sz < 0) { - report_close(ss, s); + report_close(ss, s, errno); delsocket(ss, s); return -1; } @@ -556,7 +542,7 @@ send_msg_tcp(struct silly_socket *ss, struct socket *s) ssize_t sz; sz = sendn(s->fd, w->buff + w->offset, w->size); if (sz < 0) { - report_close(ss, s); + report_close(ss, s, errno); delsocket(ss, s); return ; } @@ -589,11 +575,9 @@ send_msg_udp(struct silly_socket *ss, struct socket *s) while (w) { ssize_t sz; sz = sendudp(s->fd, w->buff + w->offset, w->size, &w->udpaddress); - if (sz > 0 && sz < w->size) { - w->size -= sz; - w->offset += sz; + if (sz == -2) //EAGAIN, so block it break; - } + assert(sz == -1 || sz == w->size); //send fail && send ok will clear s->wlhead.next = w->next; silly_free(w->buff); @@ -689,7 +673,7 @@ pipe_blockwrite(int fd, struct cmdpacket *pk) return 0; } -static void __inline +static inline void tosockaddr(struct sockaddr *addr, const char *ip, int port) { struct sockaddr_in *in = (struct sockaddr_in *)addr; @@ -801,7 +785,7 @@ trylisten(struct silly_socket *ss, struct cmdpacket *cmd) err = sp_add(ss->spfd, s->fd, s); if (err < 0) { perror("trylisten"); - report_error(ss, s, errno); + report_close(ss, s, errno); close(s->fd); freesocket(ss, s); return err; @@ -821,7 +805,7 @@ tryudpbind(struct silly_socket *ss, struct cmdpacket *cmd) err = sp_add(ss->spfd, s->fd, s); if (err < 0) { perror("tryudpbind"); - report_error(ss, s, errno); + report_close(ss, s, errno); close(s->fd); freesocket(ss, s); return err; @@ -832,7 +816,7 @@ tryudpbind(struct silly_socket *ss, struct cmdpacket *cmd) return err; } -static void __inline +static inline void fill_connectaddr(struct cmdpacket *cmd, const char *addr, int port, const char *bindip, int bindport) { size_t sz; @@ -885,7 +869,9 @@ tryconnect(struct silly_socket *ss, struct cmdpacket *cmd) if (fd < 0 || err < 0) { const char *fmt = "[silly.socket] bind %s:%d, errno:%d\n"; fprintf(stderr, fmt, bip, bport, errno); - report_error(ss, s, errno); + if (fd >= 0) + close(fd); + report_close(ss, s, errno); freesocket(ss, s); return ; } @@ -896,23 +882,21 @@ tryconnect(struct silly_socket *ss, struct cmdpacket *cmd) if (err == -1 && errno != EINPROGRESS) { //error const char *fmt = "[silly.socket] tryconnect %s:%d,errno:%d\n"; fprintf(stderr, fmt, ip, port, errno); - report_error(ss, s, errno); + close(fd); + report_close(ss, s, errno); freesocket(ss, s); return ; } else if (err == 0) { //connect - s = newsocket(ss, s, fd, STYPE_SOCKET); - if (s == NULL) - report_close_sid(ss, sid); - else + s = newsocket(ss, s, fd, STYPE_SOCKET, report_close); + if (s != NULL) report_connected(ss, s); return ; } else { //block - s = newsocket(ss, s, fd, STYPE_CONNECTING); - if (s == NULL) - report_close_sid(ss, sid); - else + s = newsocket(ss, s, fd, STYPE_CONNECTING, report_close); + if (s != NULL) sp_write_enable(ss->spfd, s->fd, s, 1); } + return ; } int @@ -961,16 +945,14 @@ tryudpconnect(struct silly_socket *ss, struct cmdpacket *cmd) struct socket *s = &ss->socketpool[HASH(sid)]; assert(s->sid == sid); assert(s->type == STYPE_ALLOCED); - s = newsocket(ss, s, cmd->u.udpconnect.fd, STYPE_SOCKET); - if (s == NULL) - report_close_sid(ss, sid); - else + s = newsocket(ss, s, cmd->u.udpconnect.fd, STYPE_SOCKET, report_close); + if (s != NULL) report_connected(ss, s); return ; } -static __inline struct socket * +static inline struct socket * checksocket(struct silly_socket *ss, int sid) { struct socket *s = &SSOCKET->socketpool[HASH(sid)]; @@ -1083,7 +1065,7 @@ trysend(struct silly_socket *ss, struct cmdpacket *cmd) ssize_t n = sendn(s->fd, data, sz); if (n < 0) { silly_free(data); - report_close(ss, s); + report_close(ss, s, errno); delsocket(ss, s); return -1; } else if (n < sz) { @@ -1115,12 +1097,12 @@ tryudpsend(struct silly_socket *ss, struct cmdpacket *cmd) addr = NULL; if (wlist_empty(s)) {//try send ssize_t n = sendudp(s->fd, data, sz, addr); - if (n < 0 || n == sz) {//occurs error or send ok + if (n == -1 || n >= 0) { //occurs error or send ok silly_free(data); return 0; } - assert(n > 0 && n < sz); - wlist_append(s, data, n, sz, addr); + assert(n == -2); //EAGAIN + wlist_append(s, data, 0, sz, addr); } else { wlist_append(s, data, 0, sz, addr); } @@ -1251,7 +1233,7 @@ silly_socket_poll() } if (SP_ERR(e)) { - report_close(ss, s); + report_close(ss, s, 0); delsocket(ss, s); continue; } diff --git a/silly-src/socket_epoll.h b/silly-src/socket_epoll.h new file mode 100644 index 00000000..365ace90 --- /dev/null +++ b/silly-src/socket_epoll.h @@ -0,0 +1,55 @@ +#ifndef _SOCKET_EPOLL_H +#define _SOCKET_EPOLL_H +#include + +#define SP_READ(e) (e->events & EPOLLIN) +#define SP_WRITE(e) (e->events & EPOLLOUT) +#define SP_ERR(e) (e->events & (EPOLLERR | EPOLLHUP)) +#define SP_UD(e) (e->data.ptr) + +typedef struct epoll_event sp_event_t; + +static inline int +sp_create(int nr) +{ + return epoll_create(nr); +} + +static inline int +sp_wait(int sp, sp_event_t *event_buff, int cnt) +{ + int ret; + ret = epoll_wait(sp, event_buff, cnt, -1); + return ret; +} + +static inline int +sp_add(int sp, int fd, void *ud) +{ + struct epoll_event event; + event.data.ptr = ud; + event.events = EPOLLIN; + return epoll_ctl(sp, EPOLL_CTL_ADD, fd, &event); +} + +static inline int +sp_del(int sp, int fd) +{ + return epoll_ctl(sp, EPOLL_CTL_DEL, fd, NULL); +} + +static inline int +sp_write_enable(int sp, int fd, void *ud, int enable) +{ + struct epoll_event event; + event.data.ptr = ud; + if (enable == 1) + event.events = EPOLLIN | EPOLLOUT; + else + event.events = EPOLLIN; + + return epoll_ctl(sp, EPOLL_CTL_MOD, fd, &event); +} + +#endif + diff --git a/silly-src/socket_kevent.h b/silly-src/socket_kevent.h new file mode 100644 index 00000000..a62c6fe4 --- /dev/null +++ b/silly-src/socket_kevent.h @@ -0,0 +1,76 @@ +#ifndef _SOCKET_KEVENT_H +#define _SOCKET_KEVENT_H + +#include + +#define SP_READ(e) (e->filter == EVFILT_READ) +#define SP_WRITE(e) (e->filter == EVFILT_WRITE) +#define SP_ERR(e) ((e->filter != EVFILT_READ) && (e->filter != EVFILT_WRITE)) +#define SP_UD(e) (e->udata) + +typedef struct kevent sp_event_t; + +static inline int +sp_create(int nr) +{ + (void)nr; + return kqueue(); +} + +static inline int +sp_wait(int sp, sp_event_t *event_buff, int cnt) +{ + int ret; + ret = kevent(sp, NULL, 0, event_buff, cnt, NULL); + return ret; +} + +static inline int +sp_del(int sp, int fd) +{ + struct kevent event[1]; + EV_SET(&event[0], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); + kevent(sp, event, 1, NULL, 0, NULL); + EV_SET(&event[0], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); + kevent(sp, event, 1, NULL, 0, NULL); + + return 0; +} + +static inline int +sp_write_enable(int sp, int fd, void *ud, int enable) +{ + struct kevent event[1]; + int ctrl = enable ? EV_ENABLE : EV_DISABLE; + (void)ud; + + EV_SET(&event[0], fd, EVFILT_WRITE, ctrl, 0, 0, ud); + return kevent(sp, event, 1, NULL, 0, NULL); +} + +static inline int +sp_add(int sp, int fd, void *ud) +{ + int ret; + struct kevent event[1]; + EV_SET(&event[0], fd, EVFILT_READ, EV_ADD, 0, 0, ud); + ret = kevent(sp, event, 1, NULL, 0, NULL); + if (ret == -1) + return -1; + + EV_SET(&event[0], fd, EVFILT_WRITE, EV_ADD, 0, 0, ud); + ret = kevent(sp, event, 1, NULL, 0, NULL); + if (ret == -1) { + EV_SET(&event[0], fd, EVFILT_READ, EV_DELETE, 0, 0, ud); + kevent(sp, event, 1, NULL, 0, NULL); + } + + ret = sp_write_enable(sp, fd, ud, 0); + if (ret == -1) + sp_del(sp, fd); + + return ret; +} + +#endif + diff --git a/silly-src/socket_poll.h b/silly-src/socket_poll.h index 6bed90c4..1d7d1f3a 100644 --- a/silly-src/socket_poll.h +++ b/silly-src/socket_poll.h @@ -2,131 +2,12 @@ #define _SOCKET_POLL_H #if defined(__linux__) -#include - -#define SP_READ(e) (e->events & EPOLLIN) -#define SP_WRITE(e) (e->events & EPOLLOUT) -#define SP_ERR(e) (e->events & (EPOLLERR | EPOLLHUP)) -#define SP_UD(e) (e->data.ptr) - -typedef struct epoll_event sp_event_t; - -static inline int -sp_create(int nr) -{ - return epoll_create(nr); -} - -static inline int -sp_wait(int sp, sp_event_t *event_buff, int cnt) -{ - int ret; - ret = epoll_wait(sp, event_buff, cnt, -1); - return ret; -} - -static inline int -sp_add(int sp, int fd, void *ud) -{ - struct epoll_event event; - event.data.ptr = ud; - event.events = EPOLLIN; - return epoll_ctl(sp, EPOLL_CTL_ADD, fd, &event); -} - -static inline int -sp_del(int sp, int fd) -{ - return epoll_ctl(sp, EPOLL_CTL_DEL, fd, NULL); -} - -static inline int -sp_write_enable(int sp, int fd, void *ud, int enable) -{ - struct epoll_event event; - event.data.ptr = ud; - if (enable == 1) - event.events = EPOLLIN | EPOLLOUT; - else - event.events = EPOLLIN; - - return epoll_ctl(sp, EPOLL_CTL_MOD, fd, &event); -} +#include "socket_epoll.h" #elif (defined(__macosx__)) -#include - -#define SP_READ(e) (e->filter == EVFILT_READ) -#define SP_WRITE(e) (e->filter == EVFILT_WRITE) -#define SP_ERR(e) ((e->filter != EVFILT_READ) && (e->filter != EVFILT_WRITE)) -#define SP_UD(e) (e->udata) - -typedef struct kevent sp_event_t; - -static inline int -sp_create(int nr) -{ - (void)nr; - return kqueue(); -} - -static inline int -sp_wait(int sp, sp_event_t *event_buff, int cnt) -{ - int ret; - ret = kevent(sp, NULL, 0, event_buff, cnt, NULL); - return ret; -} - -static inline int -sp_del(int sp, int fd) -{ - struct kevent event[1]; - EV_SET(&event[0], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); - kevent(sp, event, 1, NULL, 0, NULL); - EV_SET(&event[0], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); - kevent(sp, event, 1, NULL, 0, NULL); - - return 0; -} - -static inline int -sp_write_enable(int sp, int fd, void *ud, int enable) -{ - struct kevent event[1]; - int ctrl = enable ? EV_ENABLE : EV_DISABLE; - (void)ud; - - EV_SET(&event[0], fd, EVFILT_WRITE, ctrl, 0, 0, ud); - return kevent(sp, event, 1, NULL, 0, NULL); -} - -static inline int -sp_add(int sp, int fd, void *ud) -{ - int ret; - struct kevent event[1]; - EV_SET(&event[0], fd, EVFILT_READ, EV_ADD, 0, 0, ud); - ret = kevent(sp, event, 1, NULL, 0, NULL); - if (ret == -1) - return -1; - - EV_SET(&event[0], fd, EVFILT_WRITE, EV_ADD, 0, 0, ud); - ret = kevent(sp, event, 1, NULL, 0, NULL); - if (ret == -1) { - EV_SET(&event[0], fd, EVFILT_READ, EV_DELETE, 0, 0, ud); - kevent(sp, event, 1, NULL, 0, NULL); - } - - ret = sp_write_enable(sp, fd, ud, 0); - if (ret == -1) - sp_del(sp, fd); - - return ret; -} - +#include "socket_kevent.h" #endif