uses preadv2() to listen cancel messages from the client.

This commit is contained in:
2026-05-29 15:38:11 +03:00
parent 69e12f0df1
commit 35aabe675d
3 changed files with 12 additions and 13 deletions
+9 -6
View File
@@ -23,6 +23,7 @@
#include <sys/statvfs.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <unistd.h>
@@ -293,21 +294,23 @@ void file_download(session *sess) {
int source_d = sess->fd;
int dest_d = sess->sd;
char buf[INBUFSIZE];
int rlen = read(sess->sd, buf, INBUFSIZE);
if (rlen > 0) {
struct iovec iov = {.iov_base = buf, .iov_len = INBUFSIZE};
/* uses GNU library */
ssize_t ret = preadv2(sess->fd, &iov, 1, 0, RWF_NOWAIT);
if (ret > 0) {
char *line;
query_extract_from_buf_2(buf, &rlen, &line);
query_extract_from_buf_2(buf, &ret, &line);
if (!strcmp(line, "cancel\n")) {
free(line);
/* replace session buffer with the rest of the local buffer */
memcpy(sess->buf, buf, rlen);
sess->buf_used = rlen;
memcpy(sess->buf, buf, ret);
sess->buf_used = ret;
clear_file_from_sess(sess);
sess->state = OP_WAIT;
return;
}
}
rlen = read(source_d, buf, INBUFSIZE);
int rlen = read(source_d, buf, INBUFSIZE);
if (rlen == 0) {
if (sess->file->rest) {
print_log(stdout, pl_error, "Error downloading file %s!\n",
+1 -6
View File
@@ -31,11 +31,6 @@ int32_t accept_client(int ls, session *connections[], char *wm) {
perror("accept");
return -1;
}
/* set non-blocking behaviour */
int flags = fcntl(sd, F_GETFL, 0);
flags = flags | O_NONBLOCK;
fcntl(sd, F_SETFL, flags);
session *sess = make_new_session(sd, &addr, wm);
print_log(stdout, pl_info, "New connection: %s:%u\n",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
@@ -140,7 +135,7 @@ int query_extract_from_buf(session *sess, char **output_line) {
return pos + 1;
}
int query_extract_from_buf_2(char *buf, int *buf_used, char **output_line) {
int query_extract_from_buf_2(char *buf, ssize_t *buf_used, char **output_line) {
char *line;
int pos = -1;
int _buf_used = *buf_used;
+2 -1
View File
@@ -4,13 +4,14 @@
#ifndef SESSION_H
#define SESSION_H
#include "main.h"
#include <stdio.h>
int accept_client(int ls, session *connections[], char *wm);
void session_send_string(struct session *sess, const char *str, ...);
int query_extract_from_buf(session *sess, char **output_line);
int session_do_read(session *sess, char **read_str);
void perform_session_action(session *sess, char *line, server_data_t *s_d);
void close_session(session *connections[], int sd);
int query_extract_from_buf_2(char *buf, int *buf_used, char **output_line);
int query_extract_from_buf_2(char *buf, ssize_t *buf_used, char **output_line);
/* session *make_new_session(int fd, struct sockaddr_in *from, char *wm); */
/* int query_extract_from_sess(session *sess, char **output_line); */
#endif