From 7099a86ca74fa637f26af38674f80fb8efd5f6fa Mon Sep 17 00:00:00 2001 From: Trumeet Date: Tue, 26 Jul 2022 17:36:20 -0700 Subject: refactor(libacron/acronc/helloworld): move to separate directories The corresponding CMakeLists.txt files are still rough. --- client/acronc/main.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 client/acronc/main.c (limited to 'client/acronc/main.c') diff --git a/client/acronc/main.c b/client/acronc/main.c new file mode 100644 index 0000000..6e18ed2 --- /dev/null +++ b/client/acronc/main.c @@ -0,0 +1,105 @@ +/* + * Created by yuuta on 7/23/22. + */ + +#include "log.h" +#include "handler.h" +#include "config.h" +#include "helpers.h" +#include "client.h" + +#include +#include +#include +#include +#include +#include + +static uv_loop_t lop; +uv_loop_t *loop = &lop; + +static void on_close(uv_handle_t *handle); + +static ac_connection_parameters_t params = { + .sock = NULL, + .version = 0, + .port = 25575, + .host = NULL, + .id = NULL, + .token = NULL, + .on_send = NULL, +}; + +static void cleanup(void) { + LOGD("cleanup()"); + /* sock must be closed before cleanup(). */ + uv_loop_close(loop); + ac_free(); + uv_tty_reset_mode(); +} + +static void on_stdin_closed(void) { + sock_ext(true); +} + +static void on_sock_closed(void) { + uv_stop(loop); +} + +static int on_input(ac_request_t *req) { + /* The socket will close itself upon errors. So do the input stream. */ + return sock_request(req); +} + +static int on_sock_ready(void) { + return h_stdin(on_input, on_stdin_closed); +} + +static void on_resolv(int status, const struct addrinfo *ai, void (*on_connected)(bool)) { + if (status) { + uv_stop(loop); + return; + } + + if (h_socket(¶ms, + ai, + on_connected, + on_sock_ready, + handle_object, + on_sock_closed)) { + /* Mark as true to clean up resources and prevent next retry. */ + on_connected(true); + uv_stop(loop); + } +} + +int main(int argc, const char **argv) { + int r; + + if ((r = config_parse(argc, argv, ¶ms))) return r; + atexit(cleanup); + libac_config_t config = { +#ifdef DEBUG + .out = NULL, +#else + .out = NULL, +#endif + .tok = NULL + }; + if ((r = ac_init(&config))) errx(r, "Cannot initialize Acron library."); + + if ((r = uv_loop_init(loop))) goto uviniterr; + if ((r = h_signal())) goto uviniterr; + if ((r = a_dns(params.host, params.port, on_resolv))) goto uviniterr; + + if ((r = uv_run(loop, UV_RUN_DEFAULT))) { + if (r == 1) { + /* Seems to return 1 if uv_stop is called. */ + return 0; + } + errx(-r, "Cannot run: %s", uv_strerror(r)); + } + return 0; + uviniterr: + errx(-r, "Cannot initialize: %s", uv_strerror(r)); +} \ No newline at end of file -- cgit v1.2.3