diff options
| author | ruki <[email protected]> | 2022-04-05 00:44:56 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-05 00:44:56 +0800 |
| commit | f42d862991ebb86316d43e747876e5a6cf5b7d2e (patch) | |
| tree | 1a40e862be87b7d77b816902efae6135409f12d2 /xmake/plugins/service/server.lua | |
| parent | ea532e409179feadb227148a67fd4b22c889cea6 (diff) | |
impl server runloop
Diffstat (limited to 'xmake/plugins/service/server.lua')
| -rw-r--r-- | xmake/plugins/service/server.lua | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/xmake/plugins/service/server.lua b/xmake/plugins/service/server.lua index e63f04662..5fd1cb860 100644 --- a/xmake/plugins/service/server.lua +++ b/xmake/plugins/service/server.lua @@ -20,6 +20,7 @@ -- imports import("core.base.object") +import("core.base.socket") -- define module local server = server or object() @@ -33,12 +34,31 @@ function server:super() return self._super end +-- get the listen address +function server:addr() + return "127.0.0.1" +end + +-- get the listen port +function server:port() + return 90091 +end + -- run main loop function server:runloop() + local sock_clients = {} + local sock = socket.bind(self:addr(), self:port()) + sock:listen(100) + print("%s: listening %s:%d ..", sock, self:addr(), self:port()) while true do - print("hello xmake!") - os.sleep(1000) + local sock_client = sock:accept() + if sock_client then + scheduler.co_start(function (sock) + print("%s: accepted", sock_client) + end, sock_client) + end end + sock:close() end function main() |
