From 479c1737e1a8ed599c6def2d6c99f978af4fd64b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 18 Dec 2023 11:47:09 +0100 Subject: [PATCH] fix: increase buffer size with length field for stream links --- src/transport/unicast/transport.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/transport/unicast/transport.c b/src/transport/unicast/transport.c index 15f660bd8..b41a9ce55 100644 --- a/src/transport/unicast/transport.c +++ b/src/transport/unicast/transport.c @@ -51,14 +51,21 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo if (ret == _Z_RES_OK) { uint16_t mtu = (zl->_mtu < Z_BATCH_UNICAST_SIZE) ? zl->_mtu : Z_BATCH_UNICAST_SIZE; size_t dbuf_size = 0; + size_t wbuf_size = 0; + size_t zbuf_size = 0; _Bool expandable = false; switch (zl->_cap._flow) { case Z_LINK_CAP_FLOW_STREAM: + // Add stream length field to buffer size + wbuf_size = mtu + _Z_MSG_LEN_ENC_SIZE; + zbuf_size = Z_BATCH_UNICAST_SIZE + _Z_MSG_LEN_ENC_SIZE; expandable = true; break; case Z_LINK_CAP_FLOW_DATAGRAM: default: + wbuf_size = mtu; + zbuf_size = Z_BATCH_UNICAST_SIZE; expandable = false; break; } @@ -67,16 +74,17 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo expandable = false; dbuf_size = Z_FRAG_MAX_SIZE; #endif - zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, false); - zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); + // Initialize tx rx buffers + zt->_transport._unicast._wbuf = _z_wbuf_make(wbuf_size, false); + zt->_transport._unicast._zbuf = _z_zbuf_make(zbuf_size); // Initialize the defragmentation buffers zt->_transport._unicast._dbuf_reliable = _z_wbuf_make(dbuf_size, expandable); zt->_transport._unicast._dbuf_best_effort = _z_wbuf_make(dbuf_size, expandable); // Clean up the buffers if one of them failed to be allocated - if ((_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != mtu) || - (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_UNICAST_SIZE) || + if ((_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != wbuf_size) || + (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != zbuf_size) || #if Z_FEATURE_DYNAMIC_MEMORY_ALLOCATION == 0 (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != dbuf_size) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != dbuf_size)) {