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

Commit 905cc06

Browse files
authored
Add close method to WebTransport session. (#1110)
1 parent 43f42de commit 905cc06

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

source/agent/addons/quic/QuicTransportConnection.cc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ NAN_MODULE_INIT(QuicTransportConnection::init)
4040
instanceTpl->SetInternalFieldCount(1);
4141

4242
Nan::SetPrototypeMethod(tpl, "createBidirectionalStream", createBidirectionalStream);
43+
Nan::SetPrototypeMethod(tpl, "close", close);
4344

4445
s_constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
4546
Nan::Set(target, Nan::New("QuicTransportConnection").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
@@ -116,9 +117,30 @@ NAUV_WORK_CB(QuicTransportConnection::onStreamCallback)
116117
}
117118
}
118119

119-
NAN_METHOD(QuicTransportConnection::createBidirectionalStream){
120+
NAN_METHOD(QuicTransportConnection::createBidirectionalStream)
121+
{
120122
QuicTransportConnection* obj = Nan::ObjectWrap::Unwrap<QuicTransportConnection>(info.Holder());
121-
auto stream=obj->m_session->CreateBidirectionalStream();
123+
auto stream = obj->m_session->CreateBidirectionalStream();
122124
v8::Local<v8::Object> streamObject = QuicTransportStream::newInstance(stream);
123125
info.GetReturnValue().Set(streamObject);
126+
}
127+
128+
NAN_METHOD(QuicTransportConnection::close)
129+
{
130+
QuicTransportConnection* obj = Nan::ObjectWrap::Unwrap<QuicTransportConnection>(info.Holder());
131+
uint32_t closeCode = 0;
132+
std::string reason;
133+
if (info.Length() != 0) {
134+
auto maybeCloseCode = Nan::Get(Nan::To<v8::Object>(info[0]).ToLocalChecked(), Nan::New<v8::String>("closeCode").ToLocalChecked());
135+
if (!maybeCloseCode.IsEmpty()) {
136+
closeCode = Nan::To<uint32_t>(maybeCloseCode.ToLocalChecked()).ToChecked();
137+
}
138+
auto maybeReason = Nan::Get(Nan::To<v8::Object>(info[0]).ToLocalChecked(), Nan::New<v8::String>("reason").ToLocalChecked());
139+
if (!maybeReason.IsEmpty()) {
140+
auto reasonObj =maybeReason.ToLocalChecked();
141+
Nan::Utf8String reasonStr(Nan::To<v8::String>(reasonObj).ToLocalChecked());
142+
reason = std::string(*reasonStr);
143+
}
144+
}
145+
obj->m_session->Close(closeCode, reason.c_str());
124146
}

source/agent/addons/quic/QuicTransportConnection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class QuicTransportConnection : public Nan::ObjectWrap, public owt::quic::WebTra
3636
static NAN_MODULE_INIT(init);
3737
static NAN_METHOD(newInstance);
3838
static NAN_METHOD(createBidirectionalStream);
39+
// Close a WebTransport session, take an optional argument of WebTransportCloseInfo defined in https://w3c.github.io/webtransport/#web-transport-close-info.
40+
static NAN_METHOD(close);
3941
static NAUV_WORK_CB(onStreamCallback);
4042

4143
static Nan::Persistent<v8::Function> s_constructor;

source/agent/addons/quic/quic_sdk_url

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://api.github.com/repos/open-webrtc-toolkit/owt-sdk-quic/actions/artifacts/97822442/zip
1+
https://api.github.com/repos/open-webrtc-toolkit/owt-sdk-quic/actions/artifacts/114967253/zip

source/agent/quic/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
251251
var conn = router.getConnection(connectionId);
252252
router.removeConnection(connectionId).then(function(ok) {
253253
if (conn) {
254+
if (typeof conn.close === 'function') {
254255
conn.close();
256+
}
255257
}
256258
callback('callback', 'ok');
257259
}, onError(callback));
@@ -298,7 +300,9 @@ module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
298300
var conn = router.getConnection(connectionId);
299301
router.removeConnection(connectionId).then(function(ok) {
300302
if (conn) {
303+
if (typeof conn.close === 'function') {
301304
conn.close();
305+
}
302306
}
303307
callback('callback', 'ok');
304308
}, onError(callback));

source/agent/quic/webtransport/quicTransportServer.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ module.exports = class QuicTransportServer extends EventEmitter {
3636
setTimeout(() => {
3737
// Must be authenticated in `authenticationTimeout` seconds.
3838
if (!connection.transportId) {
39-
connection.close();
39+
connection.close(
40+
{closeCode: 0, reason: 'Timeout for authentication.'});
4041
}
4142
}, authenticationTimeout * 1000);
4243
connection.onincomingstream = (stream) => {
@@ -49,14 +50,21 @@ module.exports = class QuicTransportServer extends EventEmitter {
4950
if (connection.transportId) {
5051
log.error(
5152
'Received a new signaling stream on an authenticated connection. Close connection.')
52-
connection.close();
53+
connection.close({
54+
closeCode: 0,
55+
reason:
56+
'Received a new signaling stream on an authenticated connection.'
57+
});
5358
}
5459
// Signaling stream. Waiting for transport ID then.
5560
} else if (!connection.transportId) {
5661
log.error(
5762
'Stream ' + streamId +
5863
' added on unauthenticated transport. Close connection.');
59-
connection.close();
64+
connection.close({
65+
closeCode: 0,
66+
reason: 'Stream added on unauthenticated transport'
67+
});
6068
} else {
6169
log.debug(
6270
'A new stream ' + streamId + ' is created on transport ' +
@@ -120,13 +128,14 @@ module.exports = class QuicTransportServer extends EventEmitter {
120128
token = JSON.parse(Buffer.from(message, 'base64').toString());
121129
} catch (error) {
122130
log.error('Invalid token.');
123-
connection.close();
131+
connection.close({closeCode: 0, reason: 'Invalid token.'});
124132
return;
125133
}
126134
this._validateTokenCallback(token).then(result => {
127135
if (result !== 'ok') {
128136
log.error('Authentication failed.');
129-
connection.close();
137+
connection.close(
138+
{closeCode: 0, reason: 'Authentication failed.'});
130139
return;
131140
}
132141
log.debug('Created connection for transport ID '+connection.transportId);

0 commit comments

Comments
 (0)