From ccc03aa5c9bf331b05532953a79ef817d4d2ea7d Mon Sep 17 00:00:00 2001 From: Jindong Zhang Date: Tue, 10 May 2022 00:39:13 +0800 Subject: [PATCH] add task counter --- src/entry.c | 2 +- src/fileexchange.c | 5 +++++ src/intent.h | 1 + src/pipe.c | 6 ++++++ src/state.c | 19 ++++++++++++++++++- src/utils.c | 28 +++++++++++++++++++++++----- src/utils.h | 15 ++++++++++++++- 7 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/entry.c b/src/entry.c index e903d15..3fb9eb0 100644 --- a/src/entry.c +++ b/src/entry.c @@ -108,7 +108,7 @@ int main(int argc, const char *argv[]) { log_add_fp(f, verbose_level); } } - + termtunnel_state_init(); q = queue_create(); // spt_init(argc, argv); diff --git a/src/fileexchange.c b/src/fileexchange.c index bb2a1fb..38e0bee 100644 --- a/src/fileexchange.c +++ b/src/fileexchange.c @@ -120,6 +120,7 @@ int file_sender_start() { } static int file_send_request(path_exchange_t *pe) { + set_running_task_changed(1); int nfd = vnet_tcp_connect(receiver_service_port); log_debug("open local %s to send file %s", pe->src_path, pe->dst_path); vnet_send(nfd, pe->dst_path, strlen(pe->dst_path) + 1); // with_zero_as_split @@ -146,10 +147,12 @@ static int file_send_request(path_exchange_t *pe) { // check path is exist, file can writeable. vnet_close(nfd); close(fd); + set_running_task_changed(-1); return 0; } int file_send_start(char *src_path, char *dst_path) { + path_exchange_t *pe = (path_exchange_t *)malloc(sizeof(path_exchange_t)); strcpy(pe->src_path, src_path); strcpy(pe->dst_path, dst_path); @@ -173,6 +176,7 @@ int file_send_start(char *src_path, char *dst_path) { } static int file_recv_request(path_exchange_t *pe) { + set_running_task_changed(1); int nfd = vnet_tcp_connect(sender_service_port); vnet_send(nfd, pe->src_path, strlen(pe->src_path) + 1); // with_zero_as_split log_info("open %s for write to recv", pe->dst_path); @@ -201,6 +205,7 @@ static int file_recv_request(path_exchange_t *pe) { // TODO(jdz) check path is exist, file can writeable. vnet_close(nfd); close(fd); + set_running_task_changed(-1); return 0; } diff --git a/src/intent.h b/src/intent.h index 74c8d78..e850e37 100644 --- a/src/intent.h +++ b/src/intent.h @@ -28,6 +28,7 @@ #define COMMAND_FILE_EXCHANGE 7 #define COMMAND_PORT_FORWARD 8 #define COMMAND_RETURN 9 +#define COMMAND_GET_RUNNING_TASK_COUNT 10 typedef struct ci { int i; int mode; diff --git a/src/pipe.c b/src/pipe.c index bd97cec..25da253 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -572,6 +572,12 @@ void server_handle_client_packet(int64_t type, char *buf, ssize_t len) { log_info("start ok"); break; } + case COMMAND_GET_RUNNING_TASK_COUNT: { + + get_running_task_count(); + //comm_write_packet_to_cli(COMMAND_RETURN, strdup("bind done (guess)\n"), + // sizeof("bind done (guess)\n")); + } case COMMAND_PORT_FORWARD: { port_forward_intent_t *a = (port_forward_intent_t *)buf; log_info("portforward %s:%hu <-> %s:%hu", a->src_host, a->src_port, diff --git a/src/state.c b/src/state.c index dbcb257..d56421a 100644 --- a/src/state.c +++ b/src/state.c @@ -6,6 +6,7 @@ */ #include "state.h" +#include "utils.h" static int state_mode; int get_state_mode() { return state_mode; } @@ -24,4 +25,20 @@ int set_client_process() { int set_agent_process() { state_mode = MODE_AGENT_PROCESS; return 0; -} \ No newline at end of file +} + +static struct counter_t task_counter; + +void set_running_task_changed(int value) { + utils_counter_increment_by(&task_counter, value); + return; +} + +int get_running_task_count() { + return utils_counter_get(&task_counter); +} + +int termtunnel_state_init() { + utils_counter_init(&task_counter); + return 0; +} diff --git a/src/utils.c b/src/utils.c index 00789ad..41c703f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,11 +5,8 @@ * https://opensource.org/licenses/MIT */ -#ifndef TERMTUNNEL_UTILS_H - -#define TERMTUNNEL_UTILS_H #include "utils.h" - +#include #include #include #include @@ -18,6 +15,7 @@ #include #include #include + //如果仅仅 ~ICANON; ~ECHO //那么,我们还是可以通过键盘触发SIGINT,SIGSTOP等信号。此时可以手动选择转发给远端。 //但是依据ssh,这样设置可以直接将信号转发给远程终端,从而如果我们的程序收到了键盘产生的信号的话,一定是从另一个console中手动kill过来的。 @@ -94,4 +92,24 @@ void set_stdin_raw() { _stdin_is_raw = true; } -#endif \ No newline at end of file +void utils_counter_init(struct counter_t *c) { + c->value = 0; + pthread_mutex_init(&c->lock, NULL); +} + +void utils_counter_increment_by(struct counter_t *c, int by) { + pthread_mutex_lock(&c->lock); + c->value += by; + pthread_mutex_unlock(&c->lock); +} + +void utils_counter_increment(struct counter_t *c) { + utils_counter_increment_by(c, 1); +} + +int utils_counter_get(struct counter_t *c) { + pthread_mutex_lock(&c->lock); + int rc = c->value; + pthread_mutex_unlock(&c->lock); + return rc; +} diff --git a/src/utils.h b/src/utils.h index e56bfec..2d12666 100644 --- a/src/utils.h +++ b/src/utils.h @@ -9,7 +9,7 @@ #define TERMTUNNEL_UTILS_H #include - +#include #include "log.h" #define CHECK(x, ...) \ if (!(x)) { \ @@ -17,9 +17,22 @@ exit(0); \ } +struct counter_t { + int value; + pthread_mutex_t lock; +}; + + extern int writen(int fd, void *buf, int n); extern void set_stdin_raw(); extern void restore_stdin(); extern void *memdup(const void *src, size_t n); extern const char *green_encode(const char *buf, int len, int *result_len); + + +extern void utils_counter_init(struct counter_t *c); +extern void utils_counter_increment_by(struct counter_t *c, int by); +extern void utils_counter_increment(struct counter_t *c); +extern int utils_counter_get(struct counter_t *c); + #endif \ No newline at end of file