From efee4e0eae8cc15fdb8614c022e67b9568d8a0cb Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 19 Nov 2021 10:21:40 +0000 Subject: [PATCH] Add some more bufchain_try_* functions. We already had bufchain_try_fetch_consume; now we also have bufchain_try_fetch (for when you want to wait until that much data is available but then not commit to removing it), and bufchain_try_consume (so you can conveniently ignore a certain amount of incoming data). --- misc.h | 2 ++ utils/bufchain.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/misc.h b/misc.h index 5c8f982e..84643936 100644 --- a/misc.h +++ b/misc.h @@ -123,6 +123,8 @@ ptrlen bufchain_prefix(bufchain *ch); void bufchain_consume(bufchain *ch, size_t len); void bufchain_fetch(bufchain *ch, void *data, size_t len); void bufchain_fetch_consume(bufchain *ch, void *data, size_t len); +bool bufchain_try_consume(bufchain *ch, size_t len); +bool bufchain_try_fetch(bufchain *ch, void *data, size_t len); bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len); size_t bufchain_fetch_consume_up_to(bufchain *ch, void *data, size_t len); void bufchain_set_callback_inner( diff --git a/utils/bufchain.c b/utils/bufchain.c index 60ebc2be..9b02c65f 100644 --- a/utils/bufchain.c +++ b/utils/bufchain.c @@ -153,6 +153,26 @@ void bufchain_fetch_consume(bufchain *ch, void *data, size_t len) bufchain_consume(ch, len); } +bool bufchain_try_fetch(bufchain *ch, void *data, size_t len) +{ + if (ch->buffersize >= len) { + bufchain_fetch(ch, data, len); + return true; + } else { + return false; + } +} + +bool bufchain_try_consume(bufchain *ch, size_t len) +{ + if (ch->buffersize >= len) { + bufchain_consume(ch, len); + return true; + } else { + return false; + } +} + bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len) { if (ch->buffersize >= len) {