diff options
| author | ruki <[email protected]> | 2020-01-31 00:31:03 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-01-30 21:06:19 +0800 |
| commit | 4d8a3d38c1a199a2cff62b3ae725b00bfc67a296 (patch) | |
| tree | cfaf5d3f0f4dc771a747de7155554f716ff39aa2 | |
| parent | 4e2bb608354145ac86d85b418e9a2319eaed189e (diff) | |
improve scheduler and socket connect
| -rw-r--r-- | tests/modules/socket/sched_tcp/echo_client.lua | 13 | ||||
| -rw-r--r-- | tests/modules/socket/sched_tcp/echo_server.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 23 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 8 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/socket.lua | 8 |
5 files changed, 35 insertions, 19 deletions
diff --git a/tests/modules/socket/sched_tcp/echo_client.lua b/tests/modules/socket/sched_tcp/echo_client.lua index 41490317c..257cb451f 100644 --- a/tests/modules/socket/sched_tcp/echo_client.lua +++ b/tests/modules/socket/sched_tcp/echo_client.lua @@ -37,11 +37,14 @@ function _session(addr, port) print("connect %s:%d ..", addr, port) local sock = socket.connect(addr, port) - print("%s: connected!", sock) - - scheduler.co_start(_session_recv, sock) - scheduler.co_start(_session_send, sock) - table.insert(socks, sock) + if sock then + print("%s: connected!", sock) + table.insert(socks, sock) + scheduler.co_start(_session_recv, sock) + scheduler.co_start(_session_send, sock) + else + print("connect %s:%d failed", addr, port) + end end function main(count) diff --git a/tests/modules/socket/sched_tcp/echo_server.lua b/tests/modules/socket/sched_tcp/echo_server.lua index 7f3db2d8c..f922608f6 100644 --- a/tests/modules/socket/sched_tcp/echo_server.lua +++ b/tests/modules/socket/sched_tcp/echo_server.lua @@ -42,9 +42,9 @@ function _listen(addr, port) local sock_client = sock:accept() if sock_client then print("%s: accepted", sock_client) + table.insert(sock_clients, sock_client) scheduler.co_start(_session_recv, sock_client) scheduler.co_start(_session_send, sock_client) - table.insert(sock_clients, sock_client) end end for _, sock_client in ipairs(sock_clients) do diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 98a63f2c4..5a92863b8 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -146,7 +146,7 @@ function scheduler:_poller_resume_co(co, events) -- resume this coroutine task self:_co_tasks_suspended():remove(co) - self:co_resume(co, (bit.band(events, poller.EV_POLLER_ERROR) ~= 0) and -1 or events) + return self:co_resume(co, (bit.band(events, poller.EV_POLLER_ERROR) ~= 0) and -1 or events) end -- the poller events callback @@ -176,17 +176,23 @@ function scheduler:_poller_events_cb(obj, events) if co_recv and co_recv == co_send then pollerdata.co_recv = nil pollerdata.co_send = nil - self:_poller_resume_co(co_recv, events) + return self:_poller_resume_co(co_recv, events) else if co_recv then pollerdata.co_recv = nil - self:_poller_resume_co(co_recv, bit.band(events, bit.bnot(poller.EV_POLLER_SEND))) + local ok, errors = self:_poller_resume_co(co_recv, bit.band(events, bit.bnot(poller.EV_POLLER_SEND))) + if not ok then + return false, errors + end events = bit.band(events, bit.bnot(poller.EV_POLLER_RECV)) end if co_send then pollerdata.co_send = nil - self:_poller_resume_co(co_send, bit.band(events, bit.bnot(poller.EV_POLLER_RECV))) + local ok, errors = self:_poller_resume_co(co_send, bit.band(events, bit.bnot(poller.EV_POLLER_RECV))) + if not ok then + return false, errors + end events = bit.band(events, bit.bnot(poller.EV_POLLER_SEND)) end @@ -196,6 +202,7 @@ function scheduler:_poller_events_cb(obj, events) pollerdata.poller_events_save = events_prev_save end end + return true end -- get all suspended coroutine tasks @@ -498,9 +505,15 @@ function scheduler:runloop() local objevents = e[2] local eventfunc = e[3] if eventfunc then - eventfunc(self, obj, objevents) + ok, errors = eventfunc(self, obj, objevents) + if not ok then + break + end end end + if not ok then + break + end -- spank the timer and trigger all timeout tasks self:_timer():next() diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 7669ac455..079959538 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -666,7 +666,7 @@ function socket.bind(addr, port, opt) local ok, errors = sock:bind(addr, port) if not ok then sock:close() - return nil, string.format("bind %s:%s failed, errors: %s!", addr, port, errors or "") + return nil, string.format("bind %s:%s failed, errors: %s!", addr, port, errors or "unknown") end return sock end @@ -680,7 +680,7 @@ function socket.bind_unix(addr, opt) local ok, errors = sock:bind_unix(addr, opt) if not ok then sock:close() - return nil, string.format("bind unix://%s failed, errors: %s!", addr, errors or "") + return nil, string.format("bind unix://%s failed, errors: %s!", addr, errors or "unknown") end return sock end @@ -694,7 +694,7 @@ function socket.connect(addr, port, opt) local ok, errors = sock:connect(addr, port, opt) if ok <= 0 then sock:close() - return nil, string.format("connect %s:%s failed, errors: %s!", addr, port, errors or "") + return nil, errors end return sock end @@ -708,7 +708,7 @@ function socket.connect_unix(addr, opt) local ok, errors = sock:connect_unix(addr, opt) if ok <= 0 then sock:close() - return nil, string.format("connect unix://%s failed, errors: %s!", addr, errors or "") + return nil, errors end return sock end diff --git a/xmake/core/sandbox/modules/import/core/base/socket.lua b/xmake/core/sandbox/modules/import/core/base/socket.lua index cc028c284..1198fba55 100644 --- a/xmake/core/sandbox/modules/import/core/base/socket.lua +++ b/xmake/core/sandbox/modules/import/core/base/socket.lua @@ -224,19 +224,19 @@ end -- open and connect tcp socket function sandbox_core_base_socket.connect(addr, port, opt) local sock, errors = socket.connect(addr, port, opt) - if not sock then + if not sock and errors then raise(errors) end - return _socket_wrap(sock) + return sock and _socket_wrap(sock) or nil end -- open and connect tcp socket from the unix socket function sandbox_core_base_socket.connect_unix(addr, opt) local sock, errors = socket.connect_unix(addr, opt) - if not sock then + if not sock and errors then raise(errors) end - return _socket_wrap(sock) + return sock and _socket_wrap(sock) or nil end -- return module |
