From 2cc55402361be4a017d10a9641807f2bd6b4ea5c Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 16 Aug 2014 17:48:21 +0200 Subject: [PATCH] Increase sndbuf when necessary. --- utcp.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/utcp.c b/utcp.c index 436c9e8..a65eb14 100644 --- a/utcp.c +++ b/utcp.c @@ -346,10 +346,26 @@ ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) { uint32_t bufused = c->snd.nxt - c->snd.una; + /* Check our send buffer. + * - If it's big enough, just put the data in there. + * - If not, decide whether to enlarge. (TODO, now we just always enlarge) + * - Cap len so it doesn't overflow our buffer. + */ + + if(len > c->sndbufsize - bufused) { + c->sndbufsize *= 2; + c->sndbuf = realloc(c->sndbuf, c->sndbufsize); + } + if(len > c->sndbufsize - bufused) len = c->sndbufsize - bufused; - memcpy(c->sndbuf + (c->snd.nxt - c->snd.una), data, len); + if(!len) { + errno == EWOULDBLOCK; + return 0; + } + + memcpy(c->sndbuf + bufused, data, len); // Send segments -- 2.39.5