mirror of
https://github.com/zhaojh329/rtty.git
synced 2026-02-27 09:53:17 +08:00
@@ -173,5 +173,9 @@ int main(int argc, char **argv)
|
||||
|
||||
ev_run(loop, 0);
|
||||
|
||||
rtty_exit(&rtty);
|
||||
|
||||
ev_loop_destroy(loop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
12
src/rtty.c
12
src/rtty.c
@@ -222,7 +222,7 @@ static void set_tty_winsize(struct rtty *rtty, int sid)
|
||||
log_err("ioctl TIOCSWINSZ failed: %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
static void rtty_exit(struct rtty *rtty)
|
||||
void rtty_exit(struct rtty *rtty)
|
||||
{
|
||||
ev_io_stop(rtty->loop, &rtty->ior);
|
||||
ev_io_stop(rtty->loop, &rtty->iow);
|
||||
@@ -238,8 +238,12 @@ static void rtty_exit(struct rtty *rtty)
|
||||
rtty_ssl_free(rtty->ssl);
|
||||
#endif
|
||||
|
||||
close(rtty->sock);
|
||||
rtty->sock = -1;
|
||||
if (rtty->sock > 0) {
|
||||
close(rtty->sock);
|
||||
rtty->sock = -1;
|
||||
}
|
||||
|
||||
web_reqs_free(&rtty->web_reqs);
|
||||
|
||||
if (!rtty->reconnect)
|
||||
ev_break(rtty->loop, EVBREAK_ALL);
|
||||
@@ -485,6 +489,8 @@ int rtty_start(struct rtty *rtty)
|
||||
|
||||
start_file_service(&rtty->file_context);
|
||||
|
||||
INIT_LIST_HEAD(&rtty->web_reqs);
|
||||
|
||||
rtty->active = ev_now(rtty->loop);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#include "buffer.h"
|
||||
#include "file.h"
|
||||
#include "list.h"
|
||||
|
||||
#define RTTY_MAX_TTY 5
|
||||
#define RTTY_HEARTBEAT_INTEVAL 5.0
|
||||
@@ -81,9 +82,11 @@ struct rtty {
|
||||
void *ssl; /* Context wrap of openssl, wolfssl and mbedtls */
|
||||
struct tty *ttys[RTTY_MAX_TTY];
|
||||
struct file_context file_context;
|
||||
struct list_head web_reqs;
|
||||
};
|
||||
|
||||
int rtty_start(struct rtty *rtty);
|
||||
void rtty_exit(struct rtty *rtty);
|
||||
void rtty_send_msg(struct rtty *rtty, int type, void *data, int len);
|
||||
|
||||
#endif
|
||||
|
||||
45
src/web.c
45
src/web.c
@@ -31,28 +31,11 @@
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "web.h"
|
||||
#include "net.h"
|
||||
#include "log.h"
|
||||
|
||||
struct web_request_ctx {
|
||||
struct list_head head;
|
||||
struct rtty *rtty;
|
||||
struct ev_timer tmr;
|
||||
struct ev_io ior;
|
||||
struct ev_io iow;
|
||||
struct buffer rb;
|
||||
struct buffer wb;
|
||||
ev_tstamp active;
|
||||
bool closed;
|
||||
int sock;
|
||||
int id;
|
||||
};
|
||||
|
||||
static LIST_HEAD(reqs);
|
||||
|
||||
static void web_request_free(struct web_request_ctx *ctx)
|
||||
void web_request_free(struct web_request_ctx *ctx)
|
||||
{
|
||||
struct rtty *rtty = ctx->rtty;
|
||||
struct ev_loop *loop = rtty->loop;
|
||||
@@ -65,10 +48,12 @@ static void web_request_free(struct web_request_ctx *ctx)
|
||||
close(ctx->sock);
|
||||
}
|
||||
|
||||
buffer_put_u8(wb, MSG_TYPE_WEB);
|
||||
buffer_put_u16be(wb, 2);
|
||||
buffer_put_u16be(wb, ctx->id);
|
||||
ev_io_start(loop, &rtty->iow);
|
||||
if (rtty->sock > 0) {
|
||||
buffer_put_u8(wb, MSG_TYPE_WEB);
|
||||
buffer_put_u16be(wb, 2);
|
||||
buffer_put_u16be(wb, ctx->id);
|
||||
ev_io_start(loop, &rtty->iow);
|
||||
}
|
||||
|
||||
buffer_free(&ctx->rb);
|
||||
buffer_free(&ctx->wb);
|
||||
@@ -156,11 +141,11 @@ static void on_connected(int sock, void *arg)
|
||||
ctx->sock = sock;
|
||||
}
|
||||
|
||||
static struct web_request_ctx *find_exist_ctx(int port)
|
||||
static struct web_request_ctx *find_exist_ctx(struct list_head *reqs, int port)
|
||||
{
|
||||
struct web_request_ctx *ctx;
|
||||
|
||||
list_for_each_entry(ctx, &reqs, head)
|
||||
list_for_each_entry(ctx, reqs, head)
|
||||
if (ctx->id == port)
|
||||
return ctx;
|
||||
return NULL;
|
||||
@@ -178,7 +163,7 @@ void web_request(struct rtty *rtty, int len)
|
||||
id = buffer_pull_u16be(&rtty->rb);
|
||||
req_len = len - 2;
|
||||
|
||||
ctx = find_exist_ctx(id);
|
||||
ctx = find_exist_ctx(&rtty->web_reqs, id);
|
||||
if (ctx) {
|
||||
if (req_len == 0) {
|
||||
ctx->closed = true;
|
||||
@@ -214,9 +199,17 @@ void web_request(struct rtty *rtty, int len)
|
||||
data = buffer_put(&ctx->wb, req_len);
|
||||
buffer_pull(&rtty->rb, data, req_len);
|
||||
|
||||
list_add(&ctx->head, &reqs);
|
||||
list_add(&ctx->head, &rtty->web_reqs);
|
||||
|
||||
sock = tcp_connect_sockaddr(rtty->loop, (struct sockaddr *)&addrin, sizeof(addrin), on_connected, ctx);
|
||||
if (sock < 0)
|
||||
web_request_free(ctx);
|
||||
}
|
||||
|
||||
void web_reqs_free(struct list_head *reqs)
|
||||
{
|
||||
struct web_request_ctx *ctx, *tmp;
|
||||
|
||||
list_for_each_entry_safe(ctx, tmp, reqs, head)
|
||||
web_request_free(ctx);
|
||||
}
|
||||
|
||||
16
src/web.h
16
src/web.h
@@ -27,6 +27,22 @@
|
||||
|
||||
#include "rtty.h"
|
||||
|
||||
struct web_request_ctx {
|
||||
struct list_head head;
|
||||
struct rtty *rtty;
|
||||
struct ev_timer tmr;
|
||||
struct ev_io ior;
|
||||
struct ev_io iow;
|
||||
struct buffer rb;
|
||||
struct buffer wb;
|
||||
ev_tstamp active;
|
||||
bool closed;
|
||||
int sock;
|
||||
int id;
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user