Skip to content

Commit d9420f1

Browse files
committed
correctly track recvied bytes
1 parent 31d8519 commit d9420f1

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lib/src/Http2Transport.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,14 @@ static void serializeFrame(OByteStream &buffer,
599599
// We need to handle both cases. Also it could happen that the TCP stream
600600
// just cuts off in the middle of a frame (or header). We need to handle that
601601
// too.
602-
static std::tuple<std::optional<H2Frame>, uint32_t, uint8_t, bool> parseH2Frame(
603-
trantor::MsgBuffer *msg)
602+
// return: {optional<H2Frame>, streamId, h2flags, isFatalError, consumedBytes}
603+
static std::tuple<std::optional<H2Frame>, uint32_t, uint8_t, bool, uint32_t>
604+
parseH2Frame(trantor::MsgBuffer *msg)
604605
{
605606
if (msg->readableBytes() < 9)
606607
{
607608
LOG_TRACE << "Not enough bytes to parse H2 frame header";
608-
return {std::nullopt, 0, 0, false};
609+
return {std::nullopt, 0, 0, false, 0};
609610
}
610611

611612
uint8_t *ptr = (uint8_t *)msg->peek();
@@ -616,7 +617,7 @@ static std::tuple<std::optional<H2Frame>, uint32_t, uint8_t, bool> parseH2Frame(
616617
if (msg->readableBytes() < length + 9)
617618
{
618619
LOG_TRACE << "Not enough bytes to parse H2 frame";
619-
return {std::nullopt, 0, 0, false};
620+
return {std::nullopt, 0, 0, false, 0};
620621
}
621622

622623
const uint8_t type = header.readU8();
@@ -628,7 +629,7 @@ static std::tuple<std::optional<H2Frame>, uint32_t, uint8_t, bool> parseH2Frame(
628629
{
629630
// TODO: Handle fatal protocol error
630631
LOG_TRACE << "Unsupported H2 frame type: " << (int)type;
631-
return {std::nullopt, streamId, 0, true};
632+
return {std::nullopt, streamId, 0, true, 0};
632633
}
633634

634635
LOG_TRACE << "H2 frame: length=" << length << " type=" << (int)type
@@ -658,7 +659,7 @@ static std::tuple<std::optional<H2Frame>, uint32_t, uint8_t, bool> parseH2Frame(
658659
{
659660
LOG_WARN << "Unsupported H2 frame type: " << (int)type;
660661
msg->retrieve(length + 9);
661-
return {std::nullopt, streamId, 0, false};
662+
return {std::nullopt, streamId, 0, false, 0};
662663
}
663664

664665
if (payload.remaining() != 0)
@@ -668,10 +669,10 @@ static std::tuple<std::optional<H2Frame>, uint32_t, uint8_t, bool> parseH2Frame(
668669
if (!frame)
669670
{
670671
LOG_TRACE << "Failed to parse H2 frame of type: " << (int)type;
671-
return {std::nullopt, streamId, 0, true};
672+
return {std::nullopt, streamId, 0, true, 0};
672673
}
673674

674-
return {frame, streamId, flags, false};
675+
return {frame, streamId, flags, false, length + 9};
675676
}
676677

677678
void Http2Transport::sendRequestInLoop(const HttpRequestPtr &req,
@@ -824,7 +825,6 @@ void Http2Transport::onRecvMessage(const trantor::TcpConnectionPtr &,
824825
Defer d([this]() { sendBufferedData(); });
825826
LOG_TRACE << "HTTP/2 message received:";
826827
assert(bytesReceived_ != nullptr);
827-
*bytesReceived_ += msg->readableBytes();
828828
LOG_TRACE << dump_hex_beautiful(msg->peek(), msg->readableBytes());
829829
while (true)
830830
{
@@ -838,7 +838,9 @@ void Http2Transport::onRecvMessage(const trantor::TcpConnectionPtr &,
838838
if (msg->readableBytes() == 0)
839839
break;
840840

841-
auto [frameOpt, streamId, flags, error] = parseH2Frame(msg);
841+
auto [frameOpt, streamId, flags, error, consumedBytes] =
842+
parseH2Frame(msg);
843+
*bytesReceived_ += consumedBytes;
842844
if (error)
843845
{
844846
LOG_TRACE << "Fatal protocol error happened during stream parsing";

0 commit comments

Comments
 (0)