Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit c8328a4

Browse files
authored
Add OnClosed event for WebTransportClientInterface. (#61)
1 parent d155d96 commit c8328a4

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

web_transport/sdk/api/owt/quic/web_transport_client_interface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class OWT_EXPORT WebTransportClientInterface {
3333
// Called when an incoming stream is received.
3434
virtual void OnIncomingStream(WebTransportStreamInterface*) = 0;
3535
// Called when datagram is processed.
36-
virtual void OnDatagramProcessed(MessageStatus) {}
36+
virtual void OnDatagramProcessed(MessageStatus) = 0;
37+
// Called when the connection is closed.
38+
virtual void OnClosed(uint32_t code, const char* reason) = 0;
3739
};
3840
virtual ~WebTransportClientInterface() = default;
3941
// Set a visitor for the client.

web_transport/sdk/impl/tests/web_transport_echo_visitors.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ ServerEchoVisitor::~ServerEchoVisitor() {}
4646

4747
void ServerEchoVisitor::OnSession(WebTransportSessionInterface* session) {
4848
CHECK(session);
49-
LOG(INFO) << "Server on session.";
5049
std::unique_ptr<SessionEchoVisitor> visitor =
5150
std::make_unique<SessionEchoVisitor>();
5251
session->SetVisitor(visitor.get());
5352
session_visitors_.push_back(std::move(visitor));
53+
sessions_.emplace_back(session);
5454
}
5555

5656
} // namespace test

web_transport/sdk/impl/tests/web_transport_echo_visitors.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ class ServerEchoVisitor : public WebTransportServerInterface::Visitor {
4949
void OnEnded() override {}
5050
void OnSession(WebTransportSessionInterface* session) override;
5151

52+
std::vector<WebTransportSessionInterface*> Sessions() const {
53+
return sessions_;
54+
}
55+
5256
private:
5357
std::vector<std::unique_ptr<SessionEchoVisitor>> session_visitors_;
58+
// A list of sessions created in the order of their creation. Closed sessions
59+
// are not removed.
60+
std::vector<WebTransportSessionInterface*> sessions_;
5461
};
5562

5663
} // namespace test

web_transport/sdk/impl/tests/web_transport_owt_end_to_end_test.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ClientMockVisitor : public WebTransportClientInterface::Visitor {
3939
MOCK_METHOD0(OnConnectionFailed, void());
4040
MOCK_METHOD1(OnIncomingStream, void(WebTransportStreamInterface*));
4141
MOCK_METHOD1(OnDatagramProcessed, void(MessageStatus));
42+
MOCK_METHOD2(OnClosed, void(uint32_t, const char*));
4243
};
4344

4445
class StreamMockVisitor : public WebTransportStreamInterface::Visitor {
@@ -214,7 +215,7 @@ class WebTransportOwtEndToEndTest : public net::TestWithTaskEnvironment {
214215
TestConnectionHelper* helper_; // Owned by |context_|.
215216
ClientMockVisitor visitor_;
216217
std::unique_ptr<WebTransportClientInterface> client_;
217-
std::unique_ptr<WebTransportServerInterface::Visitor> server_visitor_;
218+
std::unique_ptr<ServerEchoVisitor> server_visitor_;
218219
};
219220

220221
TEST_F(WebTransportOwtEndToEndTest, Connect) {
@@ -281,6 +282,20 @@ TEST_F(WebTransportOwtEndToEndTest, ClientSendsDatagram) {
281282
client_->SendOrQueueDatagram(data, data_size);
282283
}
283284

285+
TEST_F(WebTransportOwtEndToEndTest, ClientOnClosed) {
286+
StartEchoServer();
287+
client_ = CreateClient(GetServerUrl("/echo"));
288+
client_->SetVisitor(&visitor_);
289+
EXPECT_CALL(visitor_, OnConnected()).WillOnce(StopRunning());
290+
client_->Connect();
291+
Run();
292+
EXPECT_EQ(server_visitor_->Sessions().size(), (unsigned long)1);
293+
EXPECT_CALL(visitor_, OnClosed(testing::_, testing::_))
294+
.WillOnce(StopRunning());
295+
server_visitor_->Sessions()[0]->Close(0, "");
296+
Run();
297+
}
298+
284299
} // namespace test
285300
} // namespace quic
286301
} // namespace owt

web_transport/sdk/impl/web_transport_http3_client.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,11 @@ void WebTransportHttp3Client::OnConnectionClosed(
721721
}
722722

723723
if (error == ::quic::QUIC_NO_ERROR) {
724-
TransitionToState(net::WebTransportState::CLOSED);
724+
// Session might be closed before connection is closed, e.g.: received a
725+
// CLOSE_WEBTRANSPORT_SESSION capsule.
726+
if (state_ != net::WebTransportState::CLOSED) {
727+
TransitionToState(net::WebTransportState::CLOSED);
728+
}
725729
return;
726730
}
727731

web_transport/sdk/impl/web_transport_owt_client_impl.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ void WebTransportOwtClientImpl::OnError(const net::WebTransportError& error) {
150150
}
151151

152152
void WebTransportOwtClientImpl::OnClosed(
153-
const absl::optional<net::WebTransportCloseInfo>& close_info) {
154-
LOG(INFO) << "OnClosed.";
153+
const absl::optional<net::WebTransportCloseInfo>& close_info) {
154+
if (!visitor_)
155+
return;
156+
bool has_value(close_info.has_value());
157+
visitor_->OnClosed(has_value ? close_info->code : 0,
158+
has_value ? nullptr : close_info->reason.c_str());
155159
}
156160

157161
WebTransportStreamInterface*

0 commit comments

Comments
 (0)