Working dup2 algo

This commit is contained in:
Quentin 2019-05-06 14:52:57 +02:00
parent 260e1810ba
commit 79929eb5c8
3 changed files with 27 additions and 6 deletions

View file

@ -22,8 +22,11 @@ int algo_dup2_on_stream(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo
// Check that we didn't already received the packet
struct dup2_ctx* dup2c = app_ctx->misc;
if (ring_ge(dup2c->recv_id, bp->ip.ap.str.id))
if (ring_ge(dup2c->recv_id, bp->ip.ap.str.id)) {
mv_buffer_rtof(app_ctx, fdinfo);
return 0;
}
dup2c->recv_id = bp->ip.ap.str.id;
// 1. Find destination
@ -54,17 +57,18 @@ int algo_dup2_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdin
// 1. A whole packet has been read, we will find someone to write it
to_fdinfo = evt_core_get_from_url (ctx, url[i]);
if (to_fdinfo == NULL) {
fprintf(stderr, "No fd for URL %s in udp-read. Dropping packet :( \n", url[i]);
mv_buffer_wtof (app_ctx, fdinfo);
break;
fprintf(stderr, "No fd for URL %s in udp-read.\n", url[i]);
continue;
}
// 2. We move the buffer and notify the target
if (i == 0) dup_buffer_tow (app_ctx, bp, to_fdinfo);
else mv_buffer_rtow (app_ctx, fdinfo, to_fdinfo);
dup_buffer_tow (app_ctx, bp, to_fdinfo);
main_on_tcp_write(ctx, to_fdinfo);
}
// 3. Release the buffer
mv_buffer_rtof (app_ctx, fdinfo);
return 0;
}

View file

@ -105,6 +105,22 @@ void mv_buffer_rtow(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from, stru
g_queue_push_tail(q, bp);
}
void mv_buffer_rtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from) {
struct buffer_packet* bp;
// 1. We get the packet buffer
bp = g_hash_table_lookup (app_ctx->used_buffer, &from->fd);
if (bp == NULL) {
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s", from->fd, from->url);
exit(EXIT_FAILURE);
}
bp->mode = BP_READING;
bp->aread = 0;
g_queue_push_tail (app_ctx->free_buffer, bp);
g_hash_table_remove(app_ctx->used_buffer, &(from->fd));
}
void mv_buffer_wtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* fdinfo) {
struct buffer_packet* bp = g_hash_table_lookup (app_ctx->used_buffer, &(fdinfo->fd));
if (bp == NULL) {

View file

@ -46,6 +46,7 @@ struct algo_ctx {
};
void mv_buffer_rtow(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from, struct evt_core_fdinfo* to);
void mv_buffer_rtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from);
void mv_buffer_wtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from);
void mv_buffer_rtoa(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from, void* to);
void mv_buffer_atow(struct algo_ctx* app_ctx, void* from, struct evt_core_fdinfo* to);