diff options
Diffstat (limited to 'src/task.c')
-rw-r--r-- | src/task.c | 40 |
1 files changed, 38 insertions, 2 deletions
@@ -86,6 +86,30 @@ void fastd_task_schedule_handshake(fastd_context *ctx, fastd_peer *peer, int tim fastd_queue_put(ctx, &ctx->task_queue, &task->entry, timeout); } +static bool is_keepalive(fastd_queue_entry *data, void *extra) { + fastd_task *task = container_of(data, fastd_task, entry); + fastd_peer *peer = extra; + + if (task->peer != peer) + return true; + + return (task->type == TASK_KEEPALIVE); +} + +void fastd_task_schedule_keepalive(fastd_context *ctx, fastd_peer *peer, int timeout) { + if (fastd_queue_has_entry(ctx, &ctx->task_queue, is_keepalive, peer)) { + pr_debug(ctx, "not sending a keepalive to %P, there still is one queued", peer); + return; + } + + fastd_task *task = malloc(sizeof(fastd_task)); + + task->type = TASK_KEEPALIVE; + task->peer = peer; + + fastd_queue_put(ctx, &ctx->task_queue, &task->entry, timeout); +} + typedef struct _replace_peer_extra { fastd_peer *old_peer; fastd_peer *new_peer; @@ -112,6 +136,7 @@ void fastd_task_replace_peer(fastd_context *ctx, fastd_peer *old_peer, fastd_pee typedef struct _delete_task_extra { fastd_peer *peer; bool handshake_only; + bool keepalive_only; } delete_task_extra; static bool delete_task(fastd_queue_entry *data, void *extra) { @@ -125,6 +150,9 @@ static bool delete_task(fastd_queue_entry *data, void *extra) { if (e->handshake_only && task->type != TASK_HANDSHAKE) return true; + if (e->keepalive_only && task->type != TASK_KEEPALIVE) + return true; + switch (task->type) { case TASK_SEND: fastd_buffer_free(task->send.buffer); @@ -136,6 +164,9 @@ static bool delete_task(fastd_queue_entry *data, void *extra) { case TASK_HANDSHAKE: break; + + case TASK_KEEPALIVE: + break; } free(task); @@ -144,11 +175,16 @@ static bool delete_task(fastd_queue_entry *data, void *extra) { } void fastd_task_delete_peer(fastd_context *ctx, fastd_peer *peer) { - delete_task_extra extra = {peer, false}; + delete_task_extra extra = {peer, false, false}; fastd_queue_filter(ctx, &ctx->task_queue, delete_task, &extra); } void fastd_task_delete_peer_handshakes(fastd_context *ctx, fastd_peer *peer) { - delete_task_extra extra = {peer, true}; + delete_task_extra extra = {peer, true, false}; + fastd_queue_filter(ctx, &ctx->task_queue, delete_task, &extra); +} + +void fastd_task_delete_peer_keepalives(fastd_context *ctx, fastd_peer *peer) { + delete_task_extra extra = {peer, false, true}; fastd_queue_filter(ctx, &ctx->task_queue, delete_task, &extra); } |