Structure must be shared
This commit is contained in:
parent
a4c3018c79
commit
40dbd44da5
4 changed files with 39 additions and 13 deletions
|
@ -225,7 +225,7 @@ void on_udp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
|||
printf("A full UDP packet has been read from UDP\n");
|
||||
|
||||
// 3. A whole packet has been read, we will find someone to write it
|
||||
sprintf(url, "tcp:write:127.0.0.1:7500");
|
||||
sprintf(url, "tcp:write:127.0.0.1:7503");
|
||||
to_fdinfo = evt_core_get_from_url (ctx, url);
|
||||
if (to_fdinfo == NULL) goto co_error;
|
||||
|
||||
|
|
|
@ -110,33 +110,45 @@ on_socks5_err:
|
|||
init_tcp_client (app_ctx, pos);
|
||||
}
|
||||
|
||||
void free_udp_t(void* v) {
|
||||
struct udp_target* udp_t = v;
|
||||
udp_t->ref_count--;
|
||||
if (udp_t <= 0) {
|
||||
free(udp_t);
|
||||
}
|
||||
}
|
||||
|
||||
void init_udp_socket(char* port, struct donar_client_ctx* ctx) {
|
||||
int sock1, sock2;
|
||||
char url[1024];
|
||||
struct evt_core_cat cat = {0};
|
||||
struct evt_core_fdinfo fdinfo = {0};
|
||||
|
||||
// 1. Init shared parameters for the fdinfo structure
|
||||
struct udp_target* udp_t = malloc(sizeof(struct udp_target));
|
||||
if (udp_t == NULL) goto socket_failed;
|
||||
memset(udp_t, 0, sizeof(struct udp_target));
|
||||
udp_t->ref_count = 2;
|
||||
|
||||
fdinfo.cat = &cat;
|
||||
fdinfo.url = url;
|
||||
fdinfo.free_other = evt_core_free_app_ctx_simple;
|
||||
fdinfo.free_other = free_udp_t;
|
||||
fdinfo.other = udp_t;
|
||||
|
||||
// 2. Duplicate sockets
|
||||
sock1 = create_udp_server (port);
|
||||
if (sock1 < 0) goto socket_failed;
|
||||
sock2 = dup(sock1);
|
||||
if (sock2 < 0) goto socket_failed;
|
||||
|
||||
// 3. Register them
|
||||
fdinfo.cat->name = "udp-read";
|
||||
fdinfo.fd = sock1;
|
||||
fdinfo.other = malloc(sizeof(struct udp_target));
|
||||
if (fdinfo.other == NULL) goto socket_failed;
|
||||
memset(fdinfo.other, 0, sizeof(struct udp_target));
|
||||
sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port);
|
||||
evt_core_add_fd (&(ctx->evts), &fdinfo);
|
||||
|
||||
fdinfo.cat->name = "udp-write";
|
||||
fdinfo.fd = sock2;
|
||||
fdinfo.other = malloc(sizeof(struct udp_target));
|
||||
if (fdinfo.other == NULL) goto socket_failed;
|
||||
memset(fdinfo.other, 0, sizeof(struct udp_target));
|
||||
sprintf(fdinfo.url, "udp:write:127.0.0.1:%s", port);
|
||||
evt_core_add_fd (&(ctx->evts), &fdinfo);
|
||||
return;
|
||||
|
|
|
@ -50,14 +50,31 @@ socket_create_err:
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void free_udp_ts(void* v) {
|
||||
struct udp_target* udp_t = v;
|
||||
udp_t->ref_count--;
|
||||
if (udp_t <= 0) {
|
||||
free(udp_t);
|
||||
}
|
||||
}
|
||||
|
||||
void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) {
|
||||
int sock1, sock2;
|
||||
char url[1024];
|
||||
|
||||
struct evt_core_cat cat = {0};
|
||||
struct evt_core_fdinfo fdinfo = {0};
|
||||
|
||||
// 1. Init shared parameters for the fdinfo structure
|
||||
struct udp_target* udp_t = malloc(sizeof(struct udp_target));
|
||||
if (udp_t == NULL) goto socket_failed;
|
||||
memset(udp_t, 0, sizeof(struct udp_target));
|
||||
udp_t->ref_count = 2;
|
||||
|
||||
fdinfo.cat = &cat;
|
||||
fdinfo.url = url;
|
||||
fdinfo.free_other = evt_core_free_app_ctx_simple;
|
||||
fdinfo.free_other = free_udp_ts;
|
||||
fdinfo.other = udp_t;
|
||||
|
||||
sock1 = create_udp_client ("127.0.0.1", port);
|
||||
if (sock1 < 0) goto socket_failed;
|
||||
|
@ -66,15 +83,11 @@ void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) {
|
|||
|
||||
fdinfo.fd = sock1;
|
||||
fdinfo.cat->name = "udp-read";
|
||||
fdinfo.other = malloc(sizeof(struct udp_target));
|
||||
if (fdinfo.other == NULL) goto socket_failed;
|
||||
sprintf(fdinfo.url, "udp:read:127.0.0.1:%s", port);
|
||||
evt_core_add_fd (&(ctx->evts), &fdinfo);
|
||||
|
||||
fdinfo.fd = sock2;
|
||||
fdinfo.cat->name = "udp-write";
|
||||
fdinfo.other = malloc(sizeof(struct udp_target));
|
||||
if (fdinfo.other == NULL) goto socket_failed;
|
||||
sprintf(fdinfo.url, "udp:write:127.0.0.1:%s", port);
|
||||
evt_core_add_fd (&(ctx->evts), &fdinfo);
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ struct udp_target {
|
|||
struct sockaddr_in addr;
|
||||
socklen_t addrlen;
|
||||
int set;
|
||||
int ref_count;
|
||||
};
|
||||
|
||||
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp);
|
||||
|
|
Loading…
Reference in a new issue