From 4e11e8cd12e00db723f90821b5ba57ae22d3fa4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 29 Mar 2021 19:47:03 -0400
Subject: [PATCH] mxsock: oneShot

Change-Id: Icf205d732f7eb419d59b1de31389a715ddb5b7e1
---
 src/jamidht/multiplexed_socket.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/jamidht/multiplexed_socket.cpp b/src/jamidht/multiplexed_socket.cpp
index deda80be18..ed21e43602 100644
--- a/src/jamidht/multiplexed_socket.cpp
+++ b/src/jamidht/multiplexed_socket.cpp
@@ -462,12 +462,14 @@ MultiplexedSocket::write(const uint16_t& channel,
         ec = std::make_error_code(std::errc::message_size);
         return -1;
     }
-    msgpack::sbuffer buffer(len + 16);
+    bool oneShot = len < 8192;
+    msgpack::sbuffer buffer(oneShot ? 16 + len : 16);
     msgpack::packer<msgpack::sbuffer> pk(&buffer);
     pk.pack_array(2);
     pk.pack(channel);
     pk.pack_bin(len);
-    pk.pack_bin_body((const char*) buf, len);
+    if (oneShot)
+        pk.pack_bin_body((const char*) buf, len);
 
     std::unique_lock<std::mutex> lk(pimpl_->writeMtx);
     if (!pimpl_->endpoint) {
@@ -476,6 +478,8 @@ MultiplexedSocket::write(const uint16_t& channel,
         return -1;
     }
     int res = pimpl_->endpoint->write((const unsigned char*) buffer.data(), buffer.size(), ec);
+    if (not oneShot and res >= 0)
+        res = pimpl_->endpoint->write(buf, len, ec);
     lk.unlock();
     if (res < 0) {
         if (ec)
-- 
GitLab