Refactoring on_tunnel func

This commit is contained in:
ruti 2023-10-28 14:31:54 +02:00
parent 04b9a0097b
commit 93fdc80191

64
proxy.c
View File

@ -475,61 +475,59 @@ static inline int on_tunnel(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize, int out) char *buffer, size_t bfsize, int out)
{ {
ssize_t n = 0; ssize_t n = 0;
char *rb = buffer;
struct eval *pair = val->pair; struct eval *pair = val->pair;
if (pair->tmpbuf && out) { if (pair->tmpbuf && out) {
mod_etype(pool, val, POLLOUT, 0);
mod_etype(pool, val->pair, POLLIN, 1);
pair = val; pair = val;
val = val->pair; val = val->pair;
}
do {
if (val->tmpbuf) {
n = val->size - val->offset; n = val->size - val->offset;
rb = val->tmpbuf + val->offset; ssize_t sn = send(pair->fd, val->tmpbuf + val->offset, n, 0);
} else {
n = recv(val->fd, buffer, bfsize, 0);
if (n < 0 && errno == EAGAIN)
break;
if (n < 1) {
if (n) perror("recv server");
return -1;
}
}
ssize_t sn = send(pair->fd, rb, n, 0);
if (sn != n) { if (sn != n) {
if (sn < 0 && errno != EAGAIN) { if (sn < 0 && errno != EAGAIN) {
perror("send"); perror("send");
return -1; return -1;
} else if (sn < 0) { }
if (sn > 0)
val->offset += sn;
return 0;
}
free(val->tmpbuf);
val->tmpbuf = 0;
mod_etype(pool, val, POLLOUT, 0);
mod_etype(pool, val->pair, POLLIN, 1);
}
do {
n = recv(val->fd, buffer, bfsize, 0);
if (n < 0 && errno == EAGAIN)
break;
if (n < 1) {
if (n) perror("recv");
return -1;
}
ssize_t sn = send(pair->fd, buffer, n, 0);
if (sn != n) {
if (sn < 0) {
if (errno != EAGAIN) {
perror("send");
return -1;
}
sn = 0; sn = 0;
} }
LOG(LOG_S, "EAGAIN, set POLLOUT (fd: %d)\n", pair->fd); LOG(LOG_S, "EAGAIN, set POLLOUT (fd: %d)\n", pair->fd);
mod_etype(pool, val, POLLIN, 0);
mod_etype(pool, pair, POLLOUT, 1);
if (val->tmpbuf) {
LOG(LOG_S, "EAGAIN, AGAIN ! (fd: %d)\n", pair->fd);
if (sn > 0)
val->offset += sn;
break;
}
val->size = n - sn; val->size = n - sn;
if (!(val->tmpbuf = malloc(val->size))) { if (!(val->tmpbuf = malloc(val->size))) {
perror("malloc"); perror("malloc");
return -1; return -1;
} }
memcpy(val->tmpbuf, buffer + sn, val->size); memcpy(val->tmpbuf, buffer + sn, val->size);
mod_etype(pool, val, POLLIN, 0);
mod_etype(pool, pair, POLLOUT, 1);
break; break;
} }
else if (val->tmpbuf) {
free(val->tmpbuf);
val->tmpbuf = 0;
rb = buffer;
continue;
}
} while (n == bfsize); } while (n == bfsize);
return 0; return 0;
} }