From 1f85bf64ed73d219407b92aea8714b0b389b1182 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Mon, 21 Nov 2022 19:11:10 +0000
Subject: [PATCH] Fix request tally and netstream buffer bugs

---
 src/streams/netstream.cpp   | 8 ++++----
 src/streams/netstream.hpp   | 2 +-
 test/stream_integration.cpp | 2 ++
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/streams/netstream.cpp b/src/streams/netstream.cpp
index 4e87384..36e4470 100644
--- a/src/streams/netstream.cpp
+++ b/src/streams/netstream.cpp
@@ -224,7 +224,7 @@ void Net::_earlyProcessPacket(ftl::net::Peer *p, int16_t ttimeoff, const StreamP
     // Manage recuring requests
     if (!host_ && spkt.channel == Channel::kEndFrame && localFrame.frameset() < tally_.size()) {
         // Are we close to reaching the end of our frames request?
-        if (tally_[localFrame.frameset()] <= 5) {
+        if (tally_[localFrame.frameset()] <= frames_to_request_ / 2) {
             // Yes, so send new requests
             for (const auto f : enabled(localFrame.frameset())) {
                 const auto &sel = enabledChannels(f);
@@ -251,7 +251,6 @@ void Net::_processPacket(ftl::net::Peer *p, int16_t ttimeoff, const StreamPacket
     spkt.hint_capability = 0;
     spkt.hint_source_total = 0;
     spkt.version = 4;
-    if (p) spkt.hint_peerid = p->localID();
 
     bool isRequest = host_ && pkt.data.size() == 0 && (spkt.flags & ftl::protocol::kFlagRequest);
 
@@ -355,7 +354,7 @@ void Net::_run() {
                         int64_t pts = current->packets.first.timestamp - state->base_pkt_ts_ + buffering_;
 
                         // Should the packet be dispatched yet
-                        if (pts == ats) {
+                        if (pts == ats && !current->done) {
                             framePackets.push_back(&(*current));
 
                             if (current->packets.first.channel == Channel::kEndFrame) {
@@ -459,8 +458,9 @@ bool Net::begin() {
             // TODO(Nick): This buffer could be faster?
             auto &buf = state->buffer.emplace_back();
             buf.packets.first = spkt_raw;
+            buf.packets.first.hint_peerid = p.localID();
             buf.packets.second = std::move(pkt);
-            buf.peer = &p;
+            buf.peer = nullptr;
             buf.done = false;
         } else {
             _processPacket(&p, ttimeoff, spkt_raw, pkt);
diff --git a/src/streams/netstream.hpp b/src/streams/netstream.hpp
index 89b22ca..fb2db0e 100644
--- a/src/streams/netstream.hpp
+++ b/src/streams/netstream.hpp
@@ -80,7 +80,7 @@ class Net : public Stream {
 
     static void installRPC(ftl::net::Universe *net);
 
-    static constexpr int kFramesToRequest = 30;
+    static constexpr int kFramesToRequest = 80;
 
     // Unit test support
     virtual void hasPosted(const ftl::protocol::StreamPacket &, const ftl::protocol::DataPacket &) {}
diff --git a/test/stream_integration.cpp b/test/stream_integration.cpp
index 6cb9352..3fdfaf5 100644
--- a/test/stream_integration.cpp
+++ b/test/stream_integration.cpp
@@ -110,6 +110,8 @@ TEST_CASE("TCP Stream", "[net]") {
             return true;
         });
 
+        s2->setProperty(ftl::protocol::StreamProperty::kRequestSize, 30);
+
         s1->begin();
         s2->begin();
 
-- 
GitLab