Skip to content

Commit

Permalink
Merge pull request #8 from beordle/improve_doc
Browse files Browse the repository at this point in the history
Improve doc & fix a bug
  • Loading branch information
beordle authored May 3, 2022
2 parents fc52f87 + 2a27a9b commit 7605ae0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 35 deletions.
13 changes: 8 additions & 5 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ static struct termios ttystate_backup;
uv_tty_t agent_stdout_tty;
uv_tty_t agent_stdin_tty;
static int64_t pending_send = 0; //记录待转发的字节,用于tty 流控
int max_suggested_size = 10240;
void agent_read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);

static void alloc_buffer(uv_handle_t *handle, size_t suggested_size,
uv_buf_t *buf) {
if (suggested_size > max_suggested_size) {
suggested_size = max_suggested_size;
}
buf->base = (char *)malloc(suggested_size);
buf->len = suggested_size;
}
Expand Down Expand Up @@ -137,20 +141,19 @@ void agent_read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
}

static char *data = NULL;
// TODO 动态大小
if (data == NULL) {
data = (char *)malloc(9999);
data = (char *)malloc(2 * max_suggested_size);
}
static int data_size = 0;
CHECK(nread < 9999, "nread<9999");
CHECK(nread < max_suggested_size, "nread<2*max_suggested_size");
memcpy(data + data_size, buf->base, nread);
data_size += nread;
CHECK(data_size < 9999, "data_size>=9999");
CHECK(data_size < max_suggested_size, "data_size>=max_suggested_size");

int used_data_size = process_stdin(data, data_size);
int unused_data_size = data_size - used_data_size;
if (unused_data_size > 0) {
CHECK(unused_data_size < 9999, "erorr");
CHECK(unused_data_size < max_suggested_size, "erorr");
memmove(data, data + used_data_size, unused_data_size);
}
data_size = unused_data_size;
Expand Down
4 changes: 2 additions & 2 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ int main(int argc, const char *argv[]) {
#endif

log_set_quiet(true);
char *verbose_env_str = getenv("VERBOSE");
if (getenv("VERBOSE") != NULL) {
char *verbose_env_str = getenv("TERMTUNNEL_VERBOSE");
if (verbose_env_str != NULL) {
int verbose_level = atoi(verbose_env_str);
if (verbose_level < 0 || verbose_level > 9) {
verbose_level = 0;
Expand Down
10 changes: 0 additions & 10 deletions src/fileexchange.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,6 @@ static int file_send_request(path_exchange_t *pe) {
return 0;
}

#if 0
// from libuv
void exchange_notify_handler(uv_async_t *handle)
{
log_info("exchange_notify_handler\n");
//comm_write_packet_to_cli(COMMAND_TTY_PING, NULL, 0);
return;
}
#endif 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);
Expand Down
5 changes: 2 additions & 3 deletions src/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,10 @@ static void common_read_tty(uv_stream_t *stream, ssize_t nread,
}
fsm_append_input(global_fsm_context, buf->base, nread);
fsm_run(global_fsm_context);
// TODO:可能因为单frame过大导致2000不够用
char *dst = (char *)malloc(2000);
char *dst = (char *)malloc(5 * VIR_MTU);
int sz = 0;
while (true) {
sz = fsm_pop_output(global_fsm_context, dst, 2000);
sz = fsm_pop_output(global_fsm_context, dst, 5 * VIR_MTU);
if (sz > 0) {
server_handle_green_packet(dst, sz);
} else {
Expand Down
17 changes: 7 additions & 10 deletions src/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ int get_pty_fd() { return pty_fd; }

static int do_exec(const char *name, int ptypt, char **argv, int argc) {
struct winsize ttysize;
if (ioctl(myself_fd, TIOCGWINSZ, &ttysize) == 0) {
// pass
// sleep(5);
} else {
if (ioctl(myself_fd, TIOCGWINSZ, &ttysize) != 0) {
// fallback
ttysize.ws_col = 20;
ttysize.ws_row = 20;
ttysize.ws_xpixel = 0;
Expand All @@ -69,31 +67,30 @@ static int do_exec(const char *name, int ptypt, char **argv, int argc) {
int slavept = open(name, O_RDWR);

if (slavept < 0) {
printf("open slave error\n");
fprintf(stderr, "open slave error\n");
exit(EXIT_FAILURE);
}

ioctl(slavept, TIOCSWINSZ, &ttysize);

close(0);
close(1);
close(2);

dup2(slavept, 0);
dup2(slavept, 1);
dup2(slavept, 2);

close(slavept);
close(pty_fd);

int fdlimit = (int)sysconf(_SC_OPEN_MAX);
int32_t fdlimit = (int32_t)sysconf(_SC_OPEN_MAX);
for (int i = STDERR_FILENO + 1; i < fdlimit; i++) {
close(i);
}

execvp(new_argv[0], new_argv); //不会传递环境变量
//unsetenv("TERMTUNNEL_VERBOSE");
execvp(new_argv[0], new_argv);
fprintf(stderr, "%s: %s\n", new_argv[0], strerror(errno));
exit(EXIT_FAILURE);
// printf("kkk2k\n");
return 0;
}

Expand Down
7 changes: 2 additions & 5 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ void *memdup(const void *src, size_t n) {
bool stdin_is_raw() { return _stdin_is_raw; }

int writen(int fd, void *buf, int n) {
int nwrite, left = n;
int nwrite = 0;
int left = n;
while (left > 0) {
if ((nwrite = write(fd, buf, left)) == -1) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
} else {
if (nwrite == n) {
return 0;
} else {
left -= nwrite;
buf += nwrite;
}
}
}
return n;
Expand Down

0 comments on commit 7605ae0

Please sign in to comment.