mirror of
https://github.com/zhaojh329/rtty.git
synced 2026-02-27 09:53:17 +08:00
simplify file context struct
- Remove redundant 'busy' flag, use ctlfd > -1 to check busy state - Remove 'sid' field from file_context, use tty->sid directly - Fix fd comparison to use > -1 instead of > 0 for consistency - Simplify file_context_reset by removing unnecessary busy flag reset This reduces memory usage and Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
21
src/file.c
21
src/file.c
@@ -65,18 +65,16 @@ static int send_file_control_msg(int fd, int type, void *buf, int len)
|
||||
|
||||
void file_context_reset(struct file_context *ctx)
|
||||
{
|
||||
if (ctx->fd > 0) {
|
||||
if (ctx->fd > -1) {
|
||||
close(ctx->fd);
|
||||
ctx->fd = -1;
|
||||
}
|
||||
|
||||
if (ctx->ctlfd > 0) {
|
||||
if (ctx->ctlfd > -1) {
|
||||
close(ctx->ctlfd);
|
||||
ctx->ctlfd = -1;
|
||||
}
|
||||
|
||||
ctx->busy = false;
|
||||
|
||||
if (ctx->buf) {
|
||||
free(ctx->buf);
|
||||
ctx->buf = NULL;
|
||||
@@ -89,7 +87,7 @@ static void notify_user_canceled(struct tty *tty)
|
||||
|
||||
buffer_put_u8(&rtty->wb, MSG_TYPE_FILE);
|
||||
buffer_put_u16be(&rtty->wb, 33);
|
||||
buffer_put_data(&rtty->wb, tty->file.sid, 32);
|
||||
buffer_put_data(&rtty->wb, tty->sid, 32);
|
||||
buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_ABORT);
|
||||
ev_io_start(rtty->loop, &rtty->iow);
|
||||
}
|
||||
@@ -130,7 +128,7 @@ static void send_file_data(struct file_context *ctx)
|
||||
|
||||
buffer_put_u8(&rtty->wb, MSG_TYPE_FILE);
|
||||
buffer_put_u16be(&rtty->wb, 33 + ret);
|
||||
buffer_put_data(&rtty->wb, ctx->sid, 32);
|
||||
buffer_put_data(&rtty->wb, tty->sid, 32);
|
||||
buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_DATA);
|
||||
buffer_put_data(&rtty->wb, ctx->buf, ret);
|
||||
ev_io_start(rtty->loop, &rtty->iow);
|
||||
@@ -172,7 +170,7 @@ static int start_upload_file(struct file_context *ctx, const char *path)
|
||||
|
||||
buffer_put_u8(&rtty->wb, MSG_TYPE_FILE);
|
||||
buffer_put_u16be(&rtty->wb, 33 + strlen(name));
|
||||
buffer_put_data(&rtty->wb, ctx->sid, 32);
|
||||
buffer_put_data(&rtty->wb, tty->sid, 32);
|
||||
buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_SEND);
|
||||
buffer_put_string(&rtty->wb, name);
|
||||
ev_io_start(rtty->loop, &rtty->iow);
|
||||
@@ -224,19 +222,17 @@ bool detect_file_operation(uint8_t *buf, int len, const char *sid, struct file_c
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ctx->busy) {
|
||||
if (ctx->ctlfd > -1) {
|
||||
send_file_control_msg(ctlfd, RTTY_FILE_CTL_BUSY, NULL, 0);
|
||||
close(ctlfd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
strcpy(ctx->sid, sid);
|
||||
|
||||
if (buf[3] == 'R') {
|
||||
buffer_put_u8(&rtty->wb, MSG_TYPE_FILE);
|
||||
buffer_put_u16be(&rtty->wb, 33);
|
||||
buffer_put_data(&rtty->wb, ctx->sid, 32);
|
||||
buffer_put_data(&rtty->wb, tty->sid, 32);
|
||||
buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_RECV);
|
||||
ev_io_start(rtty->loop, &rtty->iow);
|
||||
|
||||
@@ -276,7 +272,6 @@ bool detect_file_operation(uint8_t *buf, int len, const char *sid, struct file_c
|
||||
}
|
||||
}
|
||||
|
||||
ctx->busy = true;
|
||||
ctx->ctlfd = ctlfd;
|
||||
|
||||
return true;
|
||||
@@ -371,7 +366,7 @@ static void send_file_data_ack(struct tty *tty)
|
||||
|
||||
buffer_put_u8(&rtty->wb, MSG_TYPE_FILE);
|
||||
buffer_put_u16be(&rtty->wb, 33);
|
||||
buffer_put_data(&rtty->wb, tty->file.sid, 32);
|
||||
buffer_put_data(&rtty->wb, tty->sid, 32);
|
||||
buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_ACK);
|
||||
ev_io_start(rtty->loop, &rtty->iow);
|
||||
}
|
||||
|
||||
@@ -59,11 +59,9 @@ struct file_control_msg {
|
||||
|
||||
struct file_context {
|
||||
int fd;
|
||||
bool busy;
|
||||
int ctlfd;
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
char sid[33];
|
||||
uint8_t *buf;
|
||||
uint32_t total_size;
|
||||
uint32_t remain_size;
|
||||
|
||||
Reference in New Issue
Block a user