diff options
| author | ruki <[email protected]> | 2022-04-10 19:08:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-10 19:08:20 +0800 |
| commit | 1e0a67d3dcecd18f2756e02b57cb05e486d346ae (patch) | |
| tree | 8177b4b88e4e1c0845c46ea4690de0bbf9123a9b /xmake/modules | |
| parent | 12e69c85c7be7f5a7f4a847ba826323240a35f3e (diff) | |
improve connect
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/private/service/client/remote_build_client.lua | 39 | ||||
| -rw-r--r-- | xmake/modules/private/service/connect_service.lua | 31 | ||||
| -rw-r--r-- | xmake/modules/private/service/disconnect_service.lua | 5 |
3 files changed, 43 insertions, 32 deletions
diff --git a/xmake/modules/private/service/client/remote_build_client.lua b/xmake/modules/private/service/client/remote_build_client.lua index df9e43b2a..9fcd38fcf 100644 --- a/xmake/modules/private/service/client/remote_build_client.lua +++ b/xmake/modules/private/service/client/remote_build_client.lua @@ -19,8 +19,11 @@ -- -- imports +import("core.base.socket") import("private.service.config") +import("private.service.message") import("private.service.client.client") +import("private.service.socket_stream") -- define module local remote_build_client = remote_build_client or client() @@ -36,6 +39,42 @@ function remote_build_client:class() return remote_build_client end +-- connect to the remote server +function remote_build_client:connect(addr, port) + local statusfile = self:statusfile() + local sock = socket.connect(addr, port) + local connected = false + print("%s: connect %s:%d ..", self, addr, port) + if sock then + local stream = socket_stream(sock) + if stream:send_msg(message.new_ping()) and stream:flush() then + local msg = stream:recv_msg() + if msg then + vprint(msg:body()) + connected = true + end + end + end + if connected then + print("%s: connected!", client) + io.save(statusfile, {addr = addr, port = port}) + else + print("%s: connect %s:%d failed", self, addr, port) + os.tryrm(statusfile) + end +end + +-- disconnect server +function remote_build_client:disconnect() + local statusfile = self:statusfile() + if os.isfile(statusfile) then + os.rm(statusfile) + print("%s: disconnected!", self) + else + print("%s: has been disconnected!", self) + end +end + -- is connected? function remote_build_client:is_connected() return os.isfile(self:statusfile()) diff --git a/xmake/modules/private/service/connect_service.lua b/xmake/modules/private/service/connect_service.lua index 2b9e73a42..deddc0fd8 100644 --- a/xmake/modules/private/service/connect_service.lua +++ b/xmake/modules/private/service/connect_service.lua @@ -23,8 +23,6 @@ import("core.base.option") import("core.base.socket") import("core.base.scheduler") import("private.service.config") -import("private.service.socket_stream") -import("private.service.message") import("private.service.client.remote_build_client") function _get_address() @@ -42,32 +40,9 @@ function _get_address() return addr, port end -function _connect(addr, port) - local client = remote_build_client() - local statusfile = client:statusfile() - local sock = socket.connect(addr, port) - local connected = false - print("%s: connect %s:%d ..", client, addr, port) - if sock then - local stream = socket_stream(sock) - if stream:send_msg(message.new_ping()) and stream:flush() then - local msg = stream:recv_msg() - if msg then - vprint(msg:body()) - connected = true - end - end - end - if connected then - print("%s: connected!", client) - io.save(statusfile, {addr = addr, port = port}) - else - print("%s: connect %s:%d failed", client, addr, port) - os.tryrm(statusfile) - end -end - function main() - scheduler.co_start(_connect, _get_address()) + scheduler.co_start(function () + remote_build_client():connect(_get_address()) + end) end diff --git a/xmake/modules/private/service/disconnect_service.lua b/xmake/modules/private/service/disconnect_service.lua index ac8d16af7..d793c8633 100644 --- a/xmake/modules/private/service/disconnect_service.lua +++ b/xmake/modules/private/service/disconnect_service.lua @@ -26,10 +26,7 @@ import("private.service.config") import("private.service.client.remote_build_client") function main() - local client = remote_build_client() - local statusfile = client:statusfile() - os.tryrm(statusfile) - print("%s: disconnected!", client) + remote_build_client():disconnect() end |
