1
0
mirror of https://github.com/facebookincubator/mvfst.git synced 2025-08-08 09:42:06 +03:00

Move ByteEventCallback to QuicCallbacks.h

Summary:
The purpose of this is so that we can import this header from the WebTransport implementation and use the `ByteEventCallback` directly instead of creating a wrapper, thereby saving an allocation.

There's no functional change in this commit, it's just moving things around.

Relevant files:
* quic/api/QuicCallbacks.h
* quic/api/QuicSocketLite.h

Reviewed By: hanidamlaj

Differential Revision: D70000563

fbshipit-source-id: 9523cc788f50b4ba218be33e84f7d5b4f44a73c2
This commit is contained in:
Aman Sharma
2025-02-25 17:58:37 -08:00
committed by Facebook GitHub Bot
parent 00d442b8bc
commit 07f91b0698
8 changed files with 78 additions and 84 deletions

View File

@@ -94,4 +94,56 @@ class ConnectionWriteCallback {
/* error */) noexcept {} /* error */) noexcept {}
}; };
/**
* Structure used to communicate TX and ACK/Delivery notifications.
*/
struct ByteEvent {
enum class Type { ACK = 1, TX = 2 };
static constexpr std::array<Type, 2> kByteEventTypes = {
{Type::ACK, Type::TX}};
StreamId id{0};
uint64_t offset{0};
Type type;
// sRTT at time of event
// TODO(bschlinker): Deprecate, caller can fetch transport state if
// desired.
std::chrono::microseconds srtt{0us};
};
/**
* Structure used to communicate cancellation of a ByteEvent.
*
* According to Dictionary.com, cancellation is more frequent in American
* English than cancellation. Yet in American English, the preferred style is
* typically not to double the final L, so cancel generally becomes canceled.
*/
using ByteEventCancellation = ByteEvent;
/**
* Callback class for receiving byte event (TX/ACK) notifications.
*/
class ByteEventCallback {
public:
virtual ~ByteEventCallback() = default;
/**
* Invoked when a byte event has been successfully registered.
* Since this is a convenience notification and not a mandatory callback,
* not marking this as pure virtual.
*/
virtual void onByteEventRegistered(ByteEvent /* byteEvent */) {}
/**
* Invoked when the byte event has occurred.
*/
virtual void onByteEvent(ByteEvent byteEvent) = 0;
/**
* Invoked if byte event is canceled due to reset, shutdown, or other error.
*/
virtual void onByteEventCanceled(ByteEventCancellation cancellation) = 0;
};
} // namespace quic } // namespace quic

View File

@@ -440,58 +440,6 @@ class QuicSocketLite {
virtual void onPing() noexcept = 0; virtual void onPing() noexcept = 0;
}; };
/**
* Structure used to communicate TX and ACK/Delivery notifications.
*/
struct ByteEvent {
enum class Type { ACK = 1, TX = 2 };
static constexpr std::array<Type, 2> kByteEventTypes = {
{Type::ACK, Type::TX}};
StreamId id{0};
uint64_t offset{0};
Type type;
// sRTT at time of event
// TODO(bschlinker): Deprecate, caller can fetch transport state if
// desired.
std::chrono::microseconds srtt{0us};
};
/**
* Structure used to communicate cancellation of a ByteEvent.
*
* According to Dictionary.com, cancellation is more frequent in American
* English than cancellation. Yet in American English, the preferred style is
* typically not to double the final L, so cancel generally becomes canceled.
*/
using ByteEventCancellation = ByteEvent;
/**
* Callback class for receiving byte event (TX/ACK) notifications.
*/
class ByteEventCallback {
public:
virtual ~ByteEventCallback() = default;
/**
* Invoked when a byte event has been successfully registered.
* Since this is a convenience notification and not a mandatory callback,
* not marking this as pure virtual.
*/
virtual void onByteEventRegistered(ByteEvent /* byteEvent */) {}
/**
* Invoked when the byte event has occurred.
*/
virtual void onByteEvent(ByteEvent byteEvent) = 0;
/**
* Invoked if byte event is canceled due to reset, shutdown, or other error.
*/
virtual void onByteEventCanceled(ByteEventCancellation cancellation) = 0;
};
/** /**
* Callback class for receiving ack notifications * Callback class for receiving ack notifications
*/ */

View File

@@ -12,6 +12,7 @@ mvfst_cpp_library(
"//folly/portability:gmock", "//folly/portability:gmock",
"//quic:exception", "//quic:exception",
"//quic/api:loop_detector_callback", "//quic/api:loop_detector_callback",
"//quic/api:quic_callbacks",
"//quic/api:transport", "//quic/api:transport",
"//quic/codec:types", "//quic/codec:types",
"//quic/common:network_data", "//quic/common:network_data",

View File

@@ -11,6 +11,7 @@
#include <quic/QuicException.h> #include <quic/QuicException.h>
#include <quic/api/LoopDetectorCallback.h> #include <quic/api/LoopDetectorCallback.h>
#include <quic/api/QuicCallbacks.h>
#include <quic/api/QuicSocket.h> #include <quic/api/QuicSocket.h>
#include <quic/codec/QuicConnectionId.h> #include <quic/codec/QuicConnectionId.h>
#include <quic/common/NetworkData.h> #include <quic/common/NetworkData.h>
@@ -155,29 +156,25 @@ class MockDeliveryCallback : public QuicSocket::DeliveryCallback {
MOCK_METHOD(void, onCanceled, (StreamId, uint64_t)); MOCK_METHOD(void, onCanceled, (StreamId, uint64_t));
}; };
class MockByteEventCallback : public QuicSocket::ByteEventCallback { class MockByteEventCallback : public ByteEventCallback {
public: public:
~MockByteEventCallback() override = default; ~MockByteEventCallback() override = default;
MOCK_METHOD(void, onByteEventRegistered, (QuicSocket::ByteEvent)); MOCK_METHOD(void, onByteEventRegistered, (ByteEvent));
MOCK_METHOD(void, onByteEvent, (QuicSocket::ByteEvent)); MOCK_METHOD(void, onByteEvent, (ByteEvent));
MOCK_METHOD(void, onByteEventCanceled, (QuicSocket::ByteEvent)); MOCK_METHOD(void, onByteEventCanceled, (ByteEvent));
static auto getTxMatcher(StreamId id, uint64_t offset) { static auto getTxMatcher(StreamId id, uint64_t offset) {
return AllOf( return AllOf(
testing::Field( testing::Field(&ByteEvent::type, testing::Eq(ByteEvent::Type::TX)),
&QuicSocket::ByteEvent::type, testing::Field(&ByteEvent::id, testing::Eq(id)),
testing::Eq(QuicSocket::ByteEvent::Type::TX)), testing::Field(&ByteEvent::offset, testing::Eq(offset)));
testing::Field(&QuicSocket::ByteEvent::id, testing::Eq(id)),
testing::Field(&QuicSocket::ByteEvent::offset, testing::Eq(offset)));
} }
static auto getAckMatcher(StreamId id, uint64_t offset) { static auto getAckMatcher(StreamId id, uint64_t offset) {
return AllOf( return AllOf(
testing::Field( testing::Field(&ByteEvent::type, testing::Eq(ByteEvent::Type::ACK)),
&QuicSocket::ByteEvent::type, testing::Field(&ByteEvent::id, testing::Eq(id)),
testing::Eq(QuicSocket::ByteEvent::Type::ACK)), testing::Field(&ByteEvent::offset, testing::Eq(offset)));
testing::Field(&QuicSocket::ByteEvent::id, testing::Eq(id)),
testing::Field(&QuicSocket::ByteEvent::offset, testing::Eq(offset)));
} }
}; };

View File

@@ -34,8 +34,6 @@ using namespace folly;
namespace quic::test { namespace quic::test {
constexpr uint8_t kStreamIncrement = 0x04; constexpr uint8_t kStreamIncrement = 0x04;
using ByteEvent = QuicTransportBase::ByteEvent;
using ByteEventCancellation = QuicTransportBase::ByteEventCancellation;
enum class TestFrameType : uint8_t { enum class TestFrameType : uint8_t {
STREAM, STREAM,
@@ -166,7 +164,7 @@ class TestPingCallback : public QuicSocket::PingCallback {
void onPing() noexcept override {} void onPing() noexcept override {}
}; };
class TestByteEventCallback : public QuicSocket::ByteEventCallback { class TestByteEventCallback : public ByteEventCallback {
public: public:
using HashFn = std::function<size_t(const ByteEvent&)>; using HashFn = std::function<size_t(const ByteEvent&)>;
using ComparatorFn = std::function<bool(const ByteEvent&, const ByteEvent&)>; using ComparatorFn = std::function<bool(const ByteEvent&, const ByteEvent&)>;

View File

@@ -4834,7 +4834,7 @@ TEST_F(QuicTransportTest, GetStreamPackestTxedSingleByte) {
// one // one
EXPECT_CALL(firstByteTxCb, onByteEvent(getTxMatcher(stream, 0))) EXPECT_CALL(firstByteTxCb, onByteEvent(getTxMatcher(stream, 0)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* event */) { .WillOnce(Invoke([&](ByteEvent /* event */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 1); EXPECT_EQ(info.numPacketsTxWithNewData, 1);
})); }));
@@ -4867,13 +4867,13 @@ TEST_F(QuicTransportTest, GetStreamPacketsTxedMultipleBytes) {
// be 1 // be 1
EXPECT_CALL(firstByteTxCb, onByteEvent(getTxMatcher(stream, 0))) EXPECT_CALL(firstByteTxCb, onByteEvent(getTxMatcher(stream, 0)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* event */) { .WillOnce(Invoke([&](ByteEvent /* event */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 1); EXPECT_EQ(info.numPacketsTxWithNewData, 1);
})); }));
EXPECT_CALL(lastByteTxCb, onByteEvent(getTxMatcher(stream, lastByte))) EXPECT_CALL(lastByteTxCb, onByteEvent(getTxMatcher(stream, lastByte)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* event */) { .WillOnce(Invoke([&](ByteEvent /* event */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 1); EXPECT_EQ(info.numPacketsTxWithNewData, 1);
})); }));
@@ -4939,7 +4939,7 @@ TEST_F(QuicTransportTest, GetStreamPacketsTxedMultiplePackets) {
// first byte and first packet last bytes get Txed on first loopForWrites // first byte and first packet last bytes get Txed on first loopForWrites
EXPECT_CALL(firstByteTxCb, onByteEvent(getTxMatcher(stream, 0))) EXPECT_CALL(firstByteTxCb, onByteEvent(getTxMatcher(stream, 0)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* event */) { .WillOnce(Invoke([&](ByteEvent /* event */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 1); EXPECT_EQ(info.numPacketsTxWithNewData, 1);
})); }));
@@ -4947,7 +4947,7 @@ TEST_F(QuicTransportTest, GetStreamPacketsTxedMultiplePackets) {
firstPacketNearTailByteTxCb, firstPacketNearTailByteTxCb,
onByteEvent(getTxMatcher(stream, firstPacketNearTailByte))) onByteEvent(getTxMatcher(stream, firstPacketNearTailByte)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* even */) { .WillOnce(Invoke([&](ByteEvent /* even */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 1); EXPECT_EQ(info.numPacketsTxWithNewData, 1);
})); }));
@@ -4960,7 +4960,7 @@ TEST_F(QuicTransportTest, GetStreamPacketsTxedMultiplePackets) {
secondPacketNearHeadByteTxCb, secondPacketNearHeadByteTxCb,
onByteEvent(getTxMatcher(stream, secondPacketNearHeadByte))) onByteEvent(getTxMatcher(stream, secondPacketNearHeadByte)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* even */) { .WillOnce(Invoke([&](ByteEvent /* even */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 2); EXPECT_EQ(info.numPacketsTxWithNewData, 2);
})); }));
@@ -4968,7 +4968,7 @@ TEST_F(QuicTransportTest, GetStreamPacketsTxedMultiplePackets) {
secondPacketNearTailByteTxCb, secondPacketNearTailByteTxCb,
onByteEvent(getTxMatcher(stream, secondPacketNearTailByte))) onByteEvent(getTxMatcher(stream, secondPacketNearTailByte)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* even */) { .WillOnce(Invoke([&](ByteEvent /* even */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 2); EXPECT_EQ(info.numPacketsTxWithNewData, 2);
})); }));
@@ -4979,7 +4979,7 @@ TEST_F(QuicTransportTest, GetStreamPacketsTxedMultiplePackets) {
// last byte will be sent on the fifth loopForWrites // last byte will be sent on the fifth loopForWrites
EXPECT_CALL(lastByteTxCb, onByteEvent(getTxMatcher(stream, lastByte))) EXPECT_CALL(lastByteTxCb, onByteEvent(getTxMatcher(stream, lastByte)))
.Times(1) .Times(1)
.WillOnce(Invoke([&](QuicSocket::ByteEvent /* event */) { .WillOnce(Invoke([&](ByteEvent /* event */) {
auto info = *transport_->getStreamTransportInfo(stream); auto info = *transport_->getStreamTransportInfo(stream);
EXPECT_EQ(info.numPacketsTxWithNewData, 5); EXPECT_EQ(info.numPacketsTxWithNewData, 5);
})); }));

View File

@@ -23,7 +23,6 @@ using namespace folly;
namespace quic::test { namespace quic::test {
namespace { namespace {
using ByteEvent = QuicTransportBase::ByteEvent;
auto constexpr kTestMaxPacingRate = std::numeric_limits<uint64_t>::max(); auto constexpr kTestMaxPacingRate = std::numeric_limits<uint64_t>::max();
} // namespace } // namespace

View File

@@ -21,7 +21,7 @@ class ServerStreamHandler : public quic::QuicSocket::ConnectionSetupCallback,
public quic::QuicSocket::ReadCallback, public quic::QuicSocket::ReadCallback,
public quic::QuicSocket::WriteCallback, public quic::QuicSocket::WriteCallback,
public quic::QuicTimerCallback, public quic::QuicTimerCallback,
public QuicSocketLite::ByteEventCallback { public ByteEventCallback {
public: public:
explicit ServerStreamHandler( explicit ServerStreamHandler(
folly::EventBase* evbIn, folly::EventBase* evbIn,
@@ -286,12 +286,12 @@ class ServerStreamHandler : public quic::QuicSocket::ConnectionSetupCallback,
this, std::chrono::milliseconds(burstDeadlineMs_)); this, std::chrono::milliseconds(burstDeadlineMs_));
} }
void onByteEvent(QuicSocketLite::ByteEvent byteEvent) override { void onByteEvent(ByteEvent byteEvent) override {
CHECK_EQ(byteEvent.id, streamBurstSendResult_.streamId); CHECK_EQ(byteEvent.id, streamBurstSendResult_.streamId);
auto now = Clock::now(); auto now = Clock::now();
if (byteEvent.type == QuicSocketLite::ByteEvent::Type::TX) { if (byteEvent.type == ByteEvent::Type::TX) {
streamBurstSendResult_.trueTxStartTs = now; streamBurstSendResult_.trueTxStartTs = now;
} else if (byteEvent.type == QuicSocketLite::ByteEvent::Type::ACK) { } else if (byteEvent.type == ByteEvent::Type::ACK) {
auto ackedLatencyUs = auto ackedLatencyUs =
std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::duration_cast<std::chrono::microseconds>(
now - streamBurstSendResult_.startTs); now - streamBurstSendResult_.startTs);
@@ -311,8 +311,7 @@ class ServerStreamHandler : public quic::QuicSocket::ConnectionSetupCallback,
} }
} }
void onByteEventCanceled( void onByteEventCanceled(ByteEventCancellation cancellation) override {
QuicSocketLite::ByteEventCancellation cancellation) override {
VLOG(4) << "got stream " << cancellation.id << " offset " VLOG(4) << "got stream " << cancellation.id << " offset "
<< cancellation.offset << " cancelled"; << cancellation.offset << " cancelled";
} }