Try to remove sendto/recvfrom
This commit is contained in:
parent
0f7167ee84
commit
78fbac88d3
6 changed files with 34 additions and 34 deletions
|
@ -4,7 +4,6 @@ struct naive_ctx {
|
||||||
int ref_count;
|
int ref_count;
|
||||||
struct buffer_packet tcp_to_udp;
|
struct buffer_packet tcp_to_udp;
|
||||||
struct buffer_packet udp_to_tcp;
|
struct buffer_packet udp_to_tcp;
|
||||||
struct udp_target udp_t;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void free_nothing(void* app_ctx) {}
|
void free_nothing(void* app_ctx) {}
|
||||||
|
@ -34,7 +33,7 @@ void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
||||||
|
|
||||||
struct evt_core_fdinfo* listen_info = g_hash_table_lookup(ctx->socklist, &fd);
|
struct evt_core_fdinfo* listen_info = g_hash_table_lookup(ctx->socklist, &fd);
|
||||||
if (listen_info == NULL) goto co_error;
|
if (listen_info == NULL) goto co_error;
|
||||||
sscanf(listen_info->url, "tcp:listen:%d", &port);
|
sscanf(listen_info->url, "tcp:listen:127.0.0.1:%d", &port);
|
||||||
|
|
||||||
fdinfo.fd = conn_sock1;
|
fdinfo.fd = conn_sock1;
|
||||||
fdinfo.cat->name = "tcp-read";
|
fdinfo.cat->name = "tcp-read";
|
||||||
|
@ -55,15 +54,17 @@ co_error:
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
char* get_port(char* out, char* in) {
|
||||||
// Get target file descriptor
|
sscanf(in, "%*[a-z]:%*[a-z]:%*[a-zA-Z0-9.]:%[0-9]", out);
|
||||||
struct evt_core_cat* udp = g_hash_table_lookup (ctx->catlist, "udp-write");
|
return out;
|
||||||
if (udp == NULL || udp->socklist->len < 1) goto co_error;
|
}
|
||||||
int udp_fd = g_array_index(udp->socklist, int, 0);
|
|
||||||
|
|
||||||
|
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
||||||
// Init data structures for the transfer
|
// Init data structures for the transfer
|
||||||
|
char url[255];
|
||||||
struct naive_ctx* app_ctx = cat->app_ctx;
|
struct naive_ctx* app_ctx = cat->app_ctx;
|
||||||
struct buffer_packet* bp = &(app_ctx->tcp_to_udp);
|
struct buffer_packet* bp = &(app_ctx->tcp_to_udp);
|
||||||
|
struct evt_core_fdinfo* fdinfo;
|
||||||
int read_res = FDS_READY;
|
int read_res = FDS_READY;
|
||||||
int write_res = FDS_READY;
|
int write_res = FDS_READY;
|
||||||
|
|
||||||
|
@ -79,7 +80,10 @@ void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
||||||
|
|
||||||
// 2. Write packet to UDP socket
|
// 2. Write packet to UDP socket
|
||||||
if (bp->mode == BP_WRITING) {
|
if (bp->mode == BP_WRITING) {
|
||||||
write_res = write_packet_to_udp(udp_fd, bp, &(app_ctx->udp_t));
|
sprintf(url, "udp:write:127.0.0.1:%d", bp->ip.ap.str.port);
|
||||||
|
fdinfo = evt_core_get_from_url(ctx, url);
|
||||||
|
if (fdinfo == NULL) goto co_error;
|
||||||
|
write_res = write_packet_to_udp(fdinfo->fd, bp);
|
||||||
if (read_res == FDS_ERR) goto co_error;
|
if (read_res == FDS_ERR) goto co_error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ void init_tcp_servers(struct donar_server_ctx* ctx) {
|
||||||
|
|
||||||
fdinfo.cat->name = "tcp-listen";
|
fdinfo.cat->name = "tcp-listen";
|
||||||
fdinfo.fd = sock;
|
fdinfo.fd = sock;
|
||||||
sprintf(fdinfo.url, "tcp:listen:%d", ctx->ports[i]);
|
sprintf(fdinfo.url, "tcp:listen:127.0.0.1:%d", ctx->ports[i]);
|
||||||
evt_core_add_fd(&(ctx->evts), &fdinfo);
|
evt_core_add_fd(&(ctx->evts), &fdinfo);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -174,3 +174,10 @@ void evt_core_loop(struct evt_core_ctx* ctx) {
|
||||||
|
|
||||||
evt_core_free(ctx);
|
evt_core_free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd) {
|
||||||
|
return g_hash_table_lookup (ctx->socklist, &fd);
|
||||||
|
}
|
||||||
|
struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url) {
|
||||||
|
return g_hash_table_lookup (ctx->urltofd, url);
|
||||||
|
}
|
||||||
|
|
|
@ -46,3 +46,5 @@ void evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* user_data
|
||||||
struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd);
|
struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd);
|
||||||
void evt_core_free(struct evt_core_ctx* ctx);
|
void evt_core_free(struct evt_core_ctx* ctx);
|
||||||
void evt_core_loop(struct evt_core_ctx* ctx);
|
void evt_core_loop(struct evt_core_ctx* ctx);
|
||||||
|
struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd);
|
||||||
|
struct evt_core_fdinfo* evt_core_get_from_url(struct evt_core_ctx* ctx, char* url);
|
||||||
|
|
33
src/packet.c
33
src/packet.c
|
@ -43,28 +43,18 @@ enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp) {
|
||||||
return FDS_READY;
|
return FDS_READY;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t) {
|
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp) {
|
||||||
ssize_t nwrite;
|
ssize_t nwrite;
|
||||||
size_t bytes_to_send;
|
size_t bytes_to_send;
|
||||||
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
|
size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char);
|
||||||
struct sockaddr* addr = (struct sockaddr*)&(udp_t->addr);
|
|
||||||
socklen_t addrlen = udp_t->addrlen;
|
|
||||||
|
|
||||||
if (!udp_t->set) {
|
|
||||||
printf("UDP address is not yet set. Hope we are a connected client...\n");
|
|
||||||
addr = NULL;
|
|
||||||
addrlen = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bp->mode == BP_READING) return FDS_ERR;
|
if (bp->mode == BP_READING) return FDS_ERR;
|
||||||
|
|
||||||
bytes_to_send = bp->ip.ap.str.size - pkt_size_size;
|
bytes_to_send = bp->ip.ap.str.size - pkt_header_size;
|
||||||
nwrite = sendto(fd,
|
nwrite = send(fd,
|
||||||
&(bp->ip.ap.str.payload),
|
&(bp->ip.ap.str.payload),
|
||||||
bytes_to_send,
|
bytes_to_send,
|
||||||
0,
|
0);
|
||||||
addr,
|
|
||||||
addrlen);
|
|
||||||
|
|
||||||
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
|
if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN;
|
||||||
if (nwrite != bytes_to_send) return FDS_ERR;
|
if (nwrite != bytes_to_send) return FDS_ERR;
|
||||||
|
@ -75,23 +65,20 @@ enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_t
|
||||||
return FDS_READY;
|
return FDS_READY;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp_target* udp_t) {
|
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp) {
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
if (bp->mode == BP_WRITING) return FDS_ERR;
|
if (bp->mode == BP_WRITING) return FDS_ERR;
|
||||||
|
|
||||||
udp_t->addrlen = sizeof(struct sockaddr_in);
|
size_t pkt_header_size = sizeof(bp->ip.ap.str) - sizeof(char); // We remove the payload
|
||||||
size_t pkt_size_size = sizeof(bp->ip.ap.str.size);
|
size_t udp_packet_size = sizeof(struct internet_packet) - pkt_header_size;
|
||||||
size_t udp_packet_size = sizeof(struct internet_packet) - pkt_size_size;
|
|
||||||
|
|
||||||
nread = recvfrom(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC,
|
nread = recv(fd, &(bp->ip.ap.str.payload), udp_packet_size, MSG_TRUNC);
|
||||||
(struct sockaddr*)&(udp_t->addr), &(udp_t->addrlen));
|
|
||||||
|
|
||||||
if ((int)nread > (int)udp_packet_size) return FDS_ERR;
|
if ((int)nread > (int)udp_packet_size) return FDS_ERR;
|
||||||
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
|
if (nread == -1 && errno == EAGAIN) return FDS_AGAIN;
|
||||||
if (nread == -1) return FDS_ERR;
|
if (nread == -1) return FDS_ERR;
|
||||||
|
|
||||||
udp_t->set = 1;
|
bp->ip.ap.str.size = nread + pkt_header_size;
|
||||||
bp->ip.ap.str.size = nread + pkt_size_size;
|
|
||||||
|
|
||||||
bp->mode = BP_WRITING;
|
bp->mode = BP_WRITING;
|
||||||
bp->awrite = 0;
|
bp->awrite = 0;
|
||||||
|
|
|
@ -57,5 +57,5 @@ struct udp_target {
|
||||||
|
|
||||||
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp);
|
enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp);
|
||||||
enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp);
|
enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp);
|
||||||
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t);
|
enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp);
|
||||||
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp, struct udp_target* udp_t);
|
enum FD_STATE read_packet_from_udp (int fd, struct buffer_packet* bp);
|
||||||
|
|
Loading…
Reference in a new issue