From 4d6f873a91796ffed3d29e691520aee5f3096a01 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Thu, 16 Sep 2021 11:18:24 +0000 Subject: [PATCH] chore: improve code Signed-off-by: Jianhui Zhao --- src/{web.c => http.c} | 116 +++++++++++++++++++++--------------------- src/{web.h => http.h} | 8 +-- src/rtty.c | 10 ++-- src/rtty.h | 6 +-- 4 files changed, 70 insertions(+), 70 deletions(-) rename src/{web.c => http.c} (54%) rename src/{web.h => http.h} (89%) diff --git a/src/web.c b/src/http.c similarity index 54% rename from src/web.c rename to src/http.c index df44da0..cff0dd2 100644 --- a/src/web.c +++ b/src/http.c @@ -31,34 +31,34 @@ #include #include -#include "web.h" +#include "http.h" #include "net.h" #include "log/log.h" -void web_request_free(struct web_request_ctx *ctx) +void http_conn_free(struct http_connection *con) { - struct rtty *rtty = ctx->rtty; + struct rtty *rtty = con->rtty; struct ev_loop *loop = rtty->loop; - if (ctx->sock > 0) { - ev_io_stop(loop, &ctx->ior); - ev_io_stop(loop, &ctx->iow); - ev_timer_stop(loop, &ctx->tmr); - close(ctx->sock); + if (con->sock > 0) { + ev_io_stop(loop, &con->ior); + ev_io_stop(loop, &con->iow); + ev_timer_stop(loop, &con->tmr); + close(con->sock); } - buffer_free(&ctx->rb); - buffer_free(&ctx->wb); + buffer_free(&con->rb); + buffer_free(&con->wb); - list_del(&ctx->head); + list_del(&con->head); - free(ctx); + free(con); } static void on_net_read(struct ev_loop *loop, struct ev_io *w, int revents) { - struct web_request_ctx *ctx = container_of(w, struct web_request_ctx, ior); - struct rtty *rtty = ctx->rtty; + struct http_connection *conn = container_of(w, struct http_connection, ior); + struct rtty *rtty = conn->rtty; struct buffer *wb = &rtty->wb; uint8_t buf[4096]; int ret; @@ -67,28 +67,28 @@ static void on_net_read(struct ev_loop *loop, struct ev_io *w, int revents) if (ret <= 0) goto done; - buffer_put_u8(wb, MSG_TYPE_WEB); + buffer_put_u8(wb, MSG_TYPE_HTTP); buffer_put_u16be(wb, 18 + ret); - buffer_put_data(wb, ctx->addr, 18); + buffer_put_data(wb, conn->addr, 18); buffer_put_data(wb, buf, ret); ev_io_start(rtty->loop, &rtty->iow); - ctx->active = ev_now(rtty->loop); + conn->active = ev_now(rtty->loop); return; done: - web_request_free(ctx); + http_conn_free(conn); } static void on_net_write(struct ev_loop *loop, struct ev_io *w, int revents) { - struct web_request_ctx *ctx = container_of(w, struct web_request_ctx, iow); + struct http_connection *conn = container_of(w, struct http_connection, iow); - if (buffer_pull_to_fd(&ctx->wb, w->fd, -1) < 0) + if (buffer_pull_to_fd(&conn->wb, w->fd, -1) < 0) goto err; - if (buffer_length(&ctx->wb) > 0) + if (buffer_length(&conn->wb) > 0) return; err: @@ -97,52 +97,52 @@ static void on_net_write(struct ev_loop *loop, struct ev_io *w, int revents) static void on_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents) { - struct web_request_ctx *ctx = container_of(w, struct web_request_ctx, tmr); + struct http_connection *conn = container_of(w, struct http_connection, tmr); ev_tstamp now = ev_now(loop); - if (now - ctx->active < 30) + if (now - conn->active < 30) return; - web_request_free(ctx); + http_conn_free(conn); } static void on_connected(int sock, void *arg) { - struct web_request_ctx *ctx = (struct web_request_ctx *)arg; - struct ev_loop *loop = ctx->rtty->loop; + struct http_connection *conn = (struct http_connection *)arg; + struct ev_loop *loop = conn->rtty->loop; if (sock < 0) { - web_request_free(ctx); + http_conn_free(conn); return; } - ev_io_init(&ctx->ior, on_net_read, sock, EV_READ); - ev_io_start(loop, &ctx->ior); + ev_io_init(&conn->ior, on_net_read, sock, EV_READ); + ev_io_start(loop, &conn->ior); - ev_io_init(&ctx->iow, on_net_write, sock, EV_WRITE); - ev_io_start(loop, &ctx->iow); + ev_io_init(&conn->iow, on_net_write, sock, EV_WRITE); + ev_io_start(loop, &conn->iow); - ev_timer_init(&ctx->tmr, on_timer_cb, 3, 3); - ev_timer_start(loop, &ctx->tmr); + ev_timer_init(&conn->tmr, on_timer_cb, 3, 3); + ev_timer_start(loop, &conn->tmr); - ctx->sock = sock; + conn->sock = sock; } -static struct web_request_ctx *find_exist_ctx(struct list_head *reqs, uint8_t *addr) +static struct http_connection *find_exist_connection(struct list_head *conns, uint8_t *addr) { - struct web_request_ctx *ctx; + struct http_connection *conn; - list_for_each_entry(ctx, reqs, head) { - if (!memcmp(ctx->addr, addr, 18)) - return ctx; + list_for_each_entry(conn, conns, head) { + if (!memcmp(conn->addr, addr, 18)) + return conn; } return NULL; } -void web_request(struct rtty *rtty, int len) +void http_request(struct rtty *rtty, int len) { - struct web_request_ctx *ctx; + struct http_connection *conn; struct sockaddr_in addrin = { .sin_family = AF_INET }; @@ -157,16 +157,16 @@ void web_request(struct rtty *rtty, int len) if (req_len == 0) return; - ctx = find_exist_ctx(&rtty->web_reqs, addr); - if (ctx) { + conn = find_exist_connection(&rtty->http_conns, addr); + if (conn) { buffer_pull(&rtty->rb, NULL, 6); req_len -= 6; - data = buffer_put(&ctx->wb, req_len); + data = buffer_put(&conn->wb, req_len); buffer_pull(&rtty->rb, data, req_len); - if (ctx->sock > 0) - ev_io_start(rtty->loop, &ctx->iow); + if (conn->sock > 0) + ev_io_start(rtty->loop, &conn->iow); return; } @@ -175,26 +175,26 @@ void web_request(struct rtty *rtty, int len) req_len -= 6; - ctx = (struct web_request_ctx *)calloc(1, sizeof(struct web_request_ctx)); - ctx->rtty = rtty; - ctx->active = ev_now(rtty->loop); + conn = (struct http_connection *)calloc(1, sizeof(struct http_connection)); + conn->rtty = rtty; + conn->active = ev_now(rtty->loop); - memcpy(ctx->addr, addr, 18); + memcpy(conn->addr, addr, 18); - data = buffer_put(&ctx->wb, req_len); + data = buffer_put(&conn->wb, req_len); buffer_pull(&rtty->rb, data, req_len); - list_add(&ctx->head, &rtty->web_reqs); + list_add(&conn->head, &rtty->http_conns); - sock = tcp_connect_sockaddr(rtty->loop, (struct sockaddr *)&addrin, sizeof(addrin), on_connected, ctx); + sock = tcp_connect_sockaddr(rtty->loop, (struct sockaddr *)&addrin, sizeof(addrin), on_connected, conn); if (sock < 0) - web_request_free(ctx); + http_conn_free(conn); } -void web_reqs_free(struct list_head *reqs) +void http_conns_free(struct list_head *reqs) { - struct web_request_ctx *ctx, *tmp; + struct http_connection *con, *tmp; - list_for_each_entry_safe(ctx, tmp, reqs, head) - web_request_free(ctx); + list_for_each_entry_safe(con, tmp, reqs, head) + http_conn_free(con); } diff --git a/src/web.h b/src/http.h similarity index 89% rename from src/web.h rename to src/http.h index c173344..fbc8969 100644 --- a/src/web.h +++ b/src/http.h @@ -27,7 +27,7 @@ #include "rtty.h" -struct web_request_ctx { +struct http_connection { struct list_head head; struct rtty *rtty; struct ev_timer tmr; @@ -40,8 +40,8 @@ struct web_request_ctx { uint8_t addr[18]; /* upstream connection address: [port ip] */ }; -void web_request(struct rtty *rtty, int len); -void web_request_free(struct web_request_ctx *ctx); -void web_reqs_free(struct list_head *reqs); +void http_request(struct rtty *rtty, int len); +void web_request_free(struct http_connection *ctx); +void http_conns_free(struct list_head *reqs); #endif \ No newline at end of file diff --git a/src/rtty.c b/src/rtty.c index ade16fe..7a496c5 100644 --- a/src/rtty.c +++ b/src/rtty.c @@ -29,7 +29,7 @@ #include #include "net.h" -#include "web.h" +#include "http.h" #include "file.h" #include "rtty.h" #include "list.h" @@ -281,7 +281,7 @@ void rtty_exit(struct rtty *rtty) rtty->sock = -1; } - web_reqs_free(&rtty->web_reqs); + http_conns_free(&rtty->http_conns); if (!rtty->reconnect) ev_break(rtty->loop, EVBREAK_ALL); @@ -406,8 +406,8 @@ static int parse_msg(struct rtty *rtty) case MSG_TYPE_HEARTBEAT: break; - case MSG_TYPE_WEB: - web_request(rtty, msglen); + case MSG_TYPE_HTTP: + http_request(rtty, msglen); break; default: @@ -660,7 +660,7 @@ int rtty_start(struct rtty *rtty) return -1; INIT_LIST_HEAD(&rtty->ttys); - INIT_LIST_HEAD(&rtty->web_reqs); + INIT_LIST_HEAD(&rtty->http_conns); rtty->active = ev_now(rtty->loop); diff --git a/src/rtty.h b/src/rtty.h index 756a96d..20e62d0 100644 --- a/src/rtty.h +++ b/src/rtty.h @@ -51,8 +51,8 @@ enum { MSG_TYPE_CMD, MSG_TYPE_HEARTBEAT, MSG_TYPE_FILE, - MSG_TYPE_WEB, - MSG_TYPE_MAX = MSG_TYPE_WEB + MSG_TYPE_HTTP, + MSG_TYPE_MAX = MSG_TYPE_HTTP }; struct rtty; @@ -99,7 +99,7 @@ struct rtty { #endif int ntty; /* tty number */ struct list_head ttys; - struct list_head web_reqs; + struct list_head http_conns; }; int rtty_start(struct rtty *rtty);