From a44e75201f6bef8df9dd6cb40737cbde27ea16a9 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sun, 21 Apr 2013 15:40:08 +0000
Subject: [PATCH]  * Streaming: Pcap window size fixes

---
 .../src/net/i2p/client/streaming/PacketHandler.java   |  2 +-
 .../java/src/net/i2p/client/streaming/PcapWriter.java | 11 ++++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/apps/streaming/java/src/net/i2p/client/streaming/PacketHandler.java b/apps/streaming/java/src/net/i2p/client/streaming/PacketHandler.java
index 6cdf3dc81a..ed10b6e2ab 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/PacketHandler.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/PacketHandler.java
@@ -101,9 +101,9 @@ class PacketHandler {
         
         Connection con = (sendId > 0 ? _manager.getConnectionByInboundId(sendId) : null); 
         if (con != null) {
-            receiveKnownCon(con, packet);
             if (_log.shouldLog(Log.INFO))
                 displayPacket(packet, "RECV", "wsize " + con.getOptions().getWindowSize() + " rto " + con.getOptions().getRTO());
+            receiveKnownCon(con, packet);
         } else {
             receiveUnknownCon(packet, sendId, queueIfNoConn);
             displayPacket(packet, "UNKN", null);
diff --git a/apps/streaming/java/src/net/i2p/client/streaming/PcapWriter.java b/apps/streaming/java/src/net/i2p/client/streaming/PcapWriter.java
index 1eee76190f..930ced57c8 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/PcapWriter.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/PcapWriter.java
@@ -265,19 +265,24 @@ public class PcapWriter {
         long window = ConnectionOptions.INITIAL_WINDOW_SIZE;
         long msgSize = ConnectionOptions.DEFAULT_MAX_MESSAGE_SIZE;
         if (con != null) {
+            // calculate the receive window, which doesn't have an exact streaming equivalent
             if (isInbound) {
+                // Inbound pkt: his rcv buffer ~= our outbound window
                 // try to represent what he thinks the window is, we don't really know
                 // this isn't really right, the lastsendid can get way ahead
-                window = acked + con.getOptions().getWindowSize() - con.getLastSendId();
+                window = con.getLastSendId() + con.getOptions().getWindowSize() - acked;
             } else {
+                // Ourbound pkt: our rcv buffer ~= his outbound window
+                // TODO just use a recent high unackedIn count?
                 // following is from ConnectionPacketHandler
+                // this is not interesting, we have lots of buffers
                 long ready = con.getInputStream().getHighestReadyBockId();
                 int available = con.getOptions().getInboundBufferSize() - con.getInputStream().getTotalReadySize();
                 int allowedBlocks = available/con.getOptions().getMaxMessageSize();
                 window = (ready + allowedBlocks) - pkt.getSequenceNum();
             }
-            if (window < 0)
-                window = 0;
+            if (window <= 1)
+                window = 2; // TCP min
             msgSize = con.getOptions().getMaxMessageSize();
         }
         // messages -> bytes
-- 
GitLab