mirror of
https://github.com/hufrea/byedpi.git
synced 2025-07-01 19:13:04 +00:00
Faster buffer retrieval
This commit is contained in:
parent
0028c8ced8
commit
8c33de2bf9
47
conev.c
47
conev.c
@ -93,11 +93,11 @@ void del_event(struct poolhd *pool, struct eval *val)
|
|||||||
epoll_ctl(pool->efd, EPOLL_CTL_DEL, val->fd, 0);
|
epoll_ctl(pool->efd, EPOLL_CTL_DEL, val->fd, 0);
|
||||||
#endif
|
#endif
|
||||||
if (val->buff) {
|
if (val->buff) {
|
||||||
buff_unlock(val->buff);
|
buff_push(pool, val->buff);
|
||||||
val->buff = 0;
|
val->buff = 0;
|
||||||
}
|
}
|
||||||
if (val->sq_buff) {
|
if (val->sq_buff) {
|
||||||
buff_unlock(val->sq_buff);
|
buff_push(pool, val->sq_buff);
|
||||||
val->sq_buff = 0;
|
val->sq_buff = 0;
|
||||||
}
|
}
|
||||||
close(val->fd);
|
close(val->fd);
|
||||||
@ -131,12 +131,9 @@ void del_event(struct poolhd *pool, struct eval *val)
|
|||||||
|
|
||||||
void destroy_pool(struct poolhd *pool)
|
void destroy_pool(struct poolhd *pool)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < pool->count; x++) {
|
while (pool->count) {
|
||||||
struct eval *val = pool->links[x];
|
struct eval *val = pool->links[0];
|
||||||
if (val->fd) {
|
del_event(pool, val);
|
||||||
close(val->fd);
|
|
||||||
val->fd = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
free(pool->items);
|
free(pool->items);
|
||||||
free(pool->links);
|
free(pool->links);
|
||||||
@ -332,16 +329,12 @@ void loop_event(struct poolhd *pool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct buffer *buff_get(struct buffer *root, size_t size)
|
struct buffer *buff_pop(struct poolhd *pool, size_t size)
|
||||||
{
|
{
|
||||||
struct buffer *prev = root;
|
struct buffer *root = pool->root_buff;
|
||||||
|
if (root) {
|
||||||
while (root) {
|
pool->root_buff = root->next;
|
||||||
if (!root->lock) {
|
return root;
|
||||||
return root;
|
|
||||||
}
|
|
||||||
prev = root;
|
|
||||||
root = root->next;
|
|
||||||
}
|
}
|
||||||
struct buffer *buff = malloc(sizeof(struct buffer) + size);
|
struct buffer *buff = malloc(sizeof(struct buffer) + size);
|
||||||
if (!buff) {
|
if (!buff) {
|
||||||
@ -352,20 +345,30 @@ struct buffer *buff_get(struct buffer *root, size_t size)
|
|||||||
|
|
||||||
memset(buff, 0, sizeof(struct buffer));
|
memset(buff, 0, sizeof(struct buffer));
|
||||||
buff->size = size;
|
buff->size = size;
|
||||||
|
|
||||||
if (prev) {
|
|
||||||
prev->next = buff;
|
|
||||||
}
|
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void buff_push(struct poolhd *pool, struct buffer *buff)
|
||||||
|
{
|
||||||
|
if (!buff) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
buff->lock = 0;
|
||||||
|
buff->offset = 0;
|
||||||
|
buff->next = pool->root_buff;
|
||||||
|
pool->root_buff = buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void buff_destroy(struct buffer *root)
|
void buff_destroy(struct buffer *root)
|
||||||
{
|
{
|
||||||
while (root) {
|
int i = 0;
|
||||||
|
for (; root; i++) {
|
||||||
struct buffer *c = root;
|
struct buffer *c = root;
|
||||||
root = root->next;
|
root = root->next;
|
||||||
free(c);
|
free(c);
|
||||||
}
|
}
|
||||||
|
LOG(LOG_S, "buffers count: %d\n", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
conev.h
14
conev.h
@ -117,12 +117,16 @@ void remove_timer(struct poolhd *pool, struct eval *val);
|
|||||||
|
|
||||||
void loop_event(struct poolhd *pool);
|
void loop_event(struct poolhd *pool);
|
||||||
|
|
||||||
struct buffer *buff_get(struct buffer *root, size_t size);
|
struct buffer *buff_pop(struct poolhd *pool, size_t size);
|
||||||
|
|
||||||
|
void buff_push(struct poolhd *pool, struct buffer *buff);
|
||||||
|
|
||||||
void buff_destroy(struct buffer *root);
|
void buff_destroy(struct buffer *root);
|
||||||
|
|
||||||
#define buff_unlock(buff) \
|
static struct buffer *buff_ppop(struct poolhd *pool, size_t size)
|
||||||
buff->lock = 0; \
|
{
|
||||||
buff->offset = 0;
|
struct buffer *b = buff_pop(pool, size);
|
||||||
|
if (b) buff_push(pool, b);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
24
extend.c
24
extend.c
@ -165,11 +165,12 @@ static int reconnect(struct poolhd *pool, struct eval *val, int m)
|
|||||||
client->attempt = m;
|
client->attempt = m;
|
||||||
client->cache = 1;
|
client->cache = 1;
|
||||||
|
|
||||||
struct buffer *buff = buff_get(pool->root_buff, client->sq_buff->size);
|
if (!client->buff) {
|
||||||
buff->lock = client->sq_buff->lock;
|
client->buff = buff_pop(pool, client->sq_buff->size);
|
||||||
memcpy(buff->data, client->sq_buff->data, buff->lock);
|
}
|
||||||
|
client->buff->lock = client->sq_buff->lock;
|
||||||
|
memcpy(client->buff->data, client->sq_buff->data, client->buff->lock);
|
||||||
|
|
||||||
client->buff = buff;
|
|
||||||
client->buff->offset = 0;
|
client->buff->offset = 0;
|
||||||
client->round_sent = 0;
|
client->round_sent = 0;
|
||||||
return 0;
|
return 0;
|
||||||
@ -349,9 +350,9 @@ static int on_response(struct poolhd *pool, struct eval *val,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline void free_first_req(struct eval *client)
|
static inline void free_first_req(struct poolhd *pool, struct eval *client)
|
||||||
{
|
{
|
||||||
buff_unlock(client->sq_buff);
|
buff_push(pool, client->sq_buff);
|
||||||
client->sq_buff = 0;
|
client->sq_buff = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -483,17 +484,14 @@ ssize_t tcp_recv_hook(struct poolhd *pool,
|
|||||||
&& (val->sq_buff || val->recv_count == n))
|
&& (val->sq_buff || val->recv_count == n))
|
||||||
{
|
{
|
||||||
if (!val->sq_buff) {
|
if (!val->sq_buff) {
|
||||||
buff->lock = 1;
|
if (!(val->sq_buff = buff_pop(pool, buff->size))) {
|
||||||
{
|
return -1;
|
||||||
struct buffer *b = buff_get(pool->root_buff, buff->size);
|
|
||||||
val->sq_buff = b;
|
|
||||||
}
|
}
|
||||||
buff->lock = 0;
|
|
||||||
}
|
}
|
||||||
val->sq_buff->lock += n;
|
val->sq_buff->lock += n;
|
||||||
|
|
||||||
if ((size_t )val->sq_buff->lock >= val->sq_buff->size) {
|
if ((size_t )val->sq_buff->lock >= val->sq_buff->size) {
|
||||||
free_first_req(val);
|
free_first_req(pool, val);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
memcpy(val->sq_buff->data + val->sq_buff->lock - n, buff->data, n);
|
memcpy(val->sq_buff->data + val->sq_buff->lock - n, buff->data, n);
|
||||||
@ -503,7 +501,7 @@ ssize_t tcp_recv_hook(struct poolhd *pool,
|
|||||||
if (on_response(pool, val, buff->data, n) == 0) {
|
if (on_response(pool, val, buff->data, n) == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
free_first_req(val->pair);
|
free_first_req(pool, val->pair);
|
||||||
int m = val->pair->attempt;
|
int m = val->pair->attempt;
|
||||||
|
|
||||||
if (val->pair->cache && cache_add(&val->addr, m) < 0) {
|
if (val->pair->cache && cache_add(&val->addr, m) < 0) {
|
||||||
|
32
proxy.c
32
proxy.c
@ -656,7 +656,7 @@ int on_tunnel(struct poolhd *pool, struct eval *val, int etype)
|
|||||||
val = pair;
|
val = pair;
|
||||||
pair = val->pair;
|
pair = val->pair;
|
||||||
}
|
}
|
||||||
if (val->buff && val->buff->lock) {
|
if (val->buff) {
|
||||||
if (etype & POLLHUP) {
|
if (etype & POLLHUP) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -671,7 +671,7 @@ int on_tunnel(struct poolhd *pool, struct eval *val, int etype)
|
|||||||
val->buff->offset += sn;
|
val->buff->offset += sn;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
buff_unlock(val->buff);
|
buff_push(pool, val->buff);
|
||||||
val->buff = 0;
|
val->buff = 0;
|
||||||
|
|
||||||
if (mod_etype(pool, val, POLLIN) ||
|
if (mod_etype(pool, val, POLLIN) ||
|
||||||
@ -680,7 +680,11 @@ int on_tunnel(struct poolhd *pool, struct eval *val, int etype)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct buffer *buff = buff_get(pool->root_buff, params.bfsize);
|
struct buffer *buff = buff_pop(pool, params.bfsize);
|
||||||
|
if (!buff) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
val->buff = buff;
|
||||||
do {
|
do {
|
||||||
n = tcp_recv_hook(pool, val, buff);
|
n = tcp_recv_hook(pool, val, buff);
|
||||||
//if (n < 0 && get_e() == EAGAIN) {
|
//if (n < 0 && get_e() == EAGAIN) {
|
||||||
@ -698,7 +702,6 @@ int on_tunnel(struct poolhd *pool, struct eval *val, int etype)
|
|||||||
if (sn < n) {
|
if (sn < n) {
|
||||||
LOG(LOG_S, "send: %zd != %zd (fd=%d)\n", sn, n, pair->fd);
|
LOG(LOG_S, "send: %zd != %zd (fd=%d)\n", sn, n, pair->fd);
|
||||||
|
|
||||||
val->buff = buff;
|
|
||||||
buff->lock = n;
|
buff->lock = n;
|
||||||
buff->offset = sn;
|
buff->offset = sn;
|
||||||
|
|
||||||
@ -707,17 +710,21 @@ int on_tunnel(struct poolhd *pool, struct eval *val, int etype)
|
|||||||
uniperror("mod_etype");
|
uniperror("mod_etype");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
return 0;
|
||||||
}
|
}
|
||||||
} while (n == (ssize_t )buff->size);
|
} while (n == (ssize_t )buff->size);
|
||||||
|
buff_push(pool, val->buff);
|
||||||
|
val->buff = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int on_udp_tunnel(struct poolhd *pool, struct eval *val, int et)
|
int on_udp_tunnel(struct poolhd *pool, struct eval *val, int et)
|
||||||
{
|
{
|
||||||
struct buffer *buff = buff_get(pool->root_buff, params.bfsize);
|
struct buffer *buff = buff_ppop(pool, params.bfsize);
|
||||||
|
if (!buff) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
char *data = buff->data;
|
char *data = buff->data;
|
||||||
size_t data_len = buff->size;
|
size_t data_len = buff->size;
|
||||||
|
|
||||||
@ -803,8 +810,10 @@ int on_udp_tunnel(struct poolhd *pool, struct eval *val, int et)
|
|||||||
int on_request(struct poolhd *pool, struct eval *val, int et)
|
int on_request(struct poolhd *pool, struct eval *val, int et)
|
||||||
{
|
{
|
||||||
union sockaddr_u dst = {0};
|
union sockaddr_u dst = {0};
|
||||||
struct buffer *buff = buff_get(pool->root_buff, params.bfsize);
|
struct buffer *buff = buff_ppop(pool, params.bfsize);
|
||||||
|
if (!buff) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
ssize_t n = recv(val->fd, buff->data, buff->size, 0);
|
ssize_t n = recv(val->fd, buff->data, buff->size, 0);
|
||||||
if (n < 1) {
|
if (n < 1) {
|
||||||
if (n) uniperror("ss recv");
|
if (n) uniperror("ss recv");
|
||||||
@ -936,11 +945,6 @@ int start_event_loop(int srvfd)
|
|||||||
close(srvfd);
|
close(srvfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
pool->root_buff = buff_get(0, params.bfsize);
|
|
||||||
if (!pool->root_buff) {
|
|
||||||
destroy_pool(pool);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
loop_event(pool);
|
loop_event(pool);
|
||||||
|
|
||||||
LOG(LOG_S, "exit\n");
|
LOG(LOG_S, "exit\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user