]> git.meshlink.io Git - utcp/commitdiff
Increase sndbuf when necessary.
authorGuus Sliepen <guus@meshlink.io>
Sat, 16 Aug 2014 15:48:21 +0000 (17:48 +0200)
committerGuus Sliepen <guus@meshlink.io>
Sat, 16 Aug 2014 15:48:21 +0000 (17:48 +0200)
utcp.c

diff --git a/utcp.c b/utcp.c
index 436c9e8cdcd4703e39e88401fe35fa02a47c0934..a65eb145a5477b860ee7c2663cfc0793a4fece92 100644 (file)
--- 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