提交 69ced621 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Improve Transport.

上级 9f73fee8
...@@ -77,7 +77,7 @@ class Transport extends EventEmitter ...@@ -77,7 +77,7 @@ class Transport extends EventEmitter
case 'icestatechange': case 'icestatechange':
{ {
this._data.iceState = data.iceState; this._data.iceState = data.iceState;
// Unset .iceSelectedTuple if required // Unset .iceSelectedTuple if 'disconnected'
if (data.iceState === 'disconnected') if (data.iceState === 'disconnected')
{ {
delete this._data.iceSelectedTuple; delete this._data.iceSelectedTuple;
...@@ -95,6 +95,10 @@ class Transport extends EventEmitter ...@@ -95,6 +95,10 @@ class Transport extends EventEmitter
break; break;
} }
case 'close':
this.close(undefined, true);
break;
default: default:
logger.error('ignoring unknown event "%s"', event); logger.error('ignoring unknown event "%s"', event);
} }
...@@ -159,22 +163,11 @@ class Transport extends EventEmitter ...@@ -159,22 +163,11 @@ class Transport extends EventEmitter
// Remove notification subscriptions // Remove notification subscriptions
this._channel.removeAllListeners(this._internal.transportId); this._channel.removeAllListeners(this._internal.transportId);
// We won't receive events anymore, so be nice and manually update // We won't receive events anymore, so be nice and manually update some
// some fields and emit events before firing 'close' // fields
delete this._data.iceSelectedTuple; delete this._data.iceSelectedTuple;
if (this._data.iceState !== 'closed')
{
this._data.iceState = 'closed'; this._data.iceState = 'closed';
this.emit('icestatechange', { iceState: 'closed' });
}
if (!/^failed|closed$/.test(this._data.dtlsState))
{
this._data.dtlsState = 'closed'; this._data.dtlsState = 'closed';
this.emit('dtlsstatechange', { dtlsState: 'closed' });
}
if (!dontSendChannel) if (!dontSendChannel)
{ {
......
...@@ -71,7 +71,7 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) => ...@@ -71,7 +71,7 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{ {
let server = mediasoup.Server(); let server = mediasoup.Server();
t.plan(10); t.plan(6);
t.tearDown(() => server.close()); t.tearDown(() => server.close());
let room = server.Room(); let room = server.Room();
...@@ -82,23 +82,11 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) => ...@@ -82,23 +82,11 @@ tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{ {
t.pass('peer.createTransport() succeeded'); t.pass('peer.createTransport() succeeded');
transport.on('icestatechange', (data) =>
{
t.pass('event "icestatechange" fired');
t.equal(data.iceState, 'closed', 'event `data.iceState` must be "closed"');
t.equal(transport.iceState, 'closed', '`transport.iceState` must be "closed"');
});
transport.on('dtlsstatechange', (data) =>
{
t.pass('event "dtlsstatechange" fired');
t.equal(data.dtlsState, 'closed', 'event `data.dtlsState` must be "closed"');
t.equal(transport.dtlsState, 'closed', '`transport.dtlsState` must be "closed"');
});
transport.on('close', (error) => transport.on('close', (error) =>
{ {
t.error(error, 'transport should close cleanly'); t.error(error, 'transport should close cleanly');
t.equal(transport.iceState, 'closed', '`transport.iceState` must be "closed"');
t.equal(transport.dtlsState, 'closed', '`transport.dtlsState` must be "closed"');
peer.dump() peer.dump()
.then((data) => .then((data) =>
......
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
"maxlen": 200, "maxlen": 200,
"proto": true, "proto": true,
"sub": false, "sub": false,
"browser": false, "browser": true,
"browserify": false, "browserify": true,
"devel": false, "devel": false,
"jquery": false, "jquery": false,
"mocha": false, "mocha": false,
......
...@@ -10,15 +10,29 @@ ...@@ -10,15 +10,29 @@
<script type="text/javascript" src="mediasoup-tester.js"></script> <script type="text/javascript" src="mediasoup-tester.js"></script>
<style> <style>
body body, html
{ {
background-color: magenta;
height: 100%; height: 100%;
} }
.click2closeProtoo
{
background-color: darksalmon;
height: 50%;
text-align: center;;
}
.click2closePeerConnection
{
background-color: darkseagreen;
height: 50%;
text-align: center;;
}
</style> </style>
</head> </head>
<body> <body>
<div class="click2closeProtoo">CLICK TO CLOSE PROTOO</div>
<div class="click2closePeerConnection">CLICK TO CLOSE PEERCONNECTION</div>
</body> </body>
</html> </html>
...@@ -44,6 +44,25 @@ class Peer extends EventEmitter ...@@ -44,6 +44,25 @@ class Peer extends EventEmitter
{} {}
} }
closeProtoo()
{
this._debug('closeProtoo()');
this._protoo.close();
}
closePeerConnection()
{
this._debug('closePeerConnection()');
try
{
this._pc.close();
}
catch (error)
{}
}
testTransport() testTransport()
{ {
this._debug('testTransport()'); this._debug('testTransport()');
......
...@@ -46,5 +46,8 @@ function run() ...@@ -46,5 +46,8 @@ function run()
peer.testTransport(); peer.testTransport();
}); });
global.document.addEventListener('click', () => peer.close()); // global.document.addEventListener('click', () => peer.close());
document.querySelector('.click2closeProtoo').addEventListener('click', () => peer.closeProtoo());
document.querySelector('.click2closePeerConnection').addEventListener('click', () => peer.closePeerConnection());
} }
...@@ -43,8 +43,6 @@ namespace RTC ...@@ -43,8 +43,6 @@ namespace RTC
Transport* CreateAssociatedTransport(uint32_t transportId); Transport* CreateAssociatedTransport(uint32_t transportId);
private: private:
void ClosePorts();
bool IsAlive();
void MayRunDTLSTransport(); void MayRunDTLSTransport();
/* Private methods to unify UDP and TCP behavior. */ /* Private methods to unify UDP and TCP behavior. */
...@@ -121,18 +119,6 @@ namespace RTC ...@@ -121,18 +119,6 @@ namespace RTC
/* Inline methods. */ /* Inline methods. */
inline
bool Transport::IsAlive()
{
if (this->dtlsTransport->GetState() == DTLSTransport::DtlsState::FAILED ||
this->dtlsTransport->GetState() == DTLSTransport::DtlsState::CLOSED)
{
return false;
}
return true;
}
inline inline
std::string& Transport::GetIceUsernameFragment() std::string& Transport::GetIceUsernameFragment()
{ {
......
...@@ -40,6 +40,7 @@ public: ...@@ -40,6 +40,7 @@ public:
const std::string& GetPeerIP(); const std::string& GetPeerIP();
MS_PORT GetPeerPort(); MS_PORT GetPeerPort();
void Close(); void Close();
bool IsClosing();
virtual void Dump(); virtual void Dump();
private: private:
...@@ -132,4 +133,10 @@ MS_PORT TCPConnection::GetPeerPort() ...@@ -132,4 +133,10 @@ MS_PORT TCPConnection::GetPeerPort()
return this->peerPort; return this->peerPort;
} }
inline
bool TCPConnection::IsClosing()
{
return this->isClosing;
}
#endif #endif
...@@ -23,6 +23,7 @@ public: ...@@ -23,6 +23,7 @@ public:
void Write(const MS_BYTE* data, size_t len); void Write(const MS_BYTE* data, size_t len);
void Write(const std::string &data); void Write(const std::string &data);
void Close(); void Close();
bool IsClosing();
virtual void Dump(); virtual void Dump();
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
...@@ -63,4 +64,11 @@ void UnixStreamSocket::Write(const std::string &data) ...@@ -63,4 +64,11 @@ void UnixStreamSocket::Write(const std::string &data)
Write((const MS_BYTE*)data.c_str(), data.size()); Write((const MS_BYTE*)data.c_str(), data.size());
} }
inline
bool UnixStreamSocket::IsClosing()
{
return this->isClosing;
}
#endif #endif
...@@ -158,6 +158,9 @@ namespace Channel ...@@ -158,6 +158,9 @@ namespace Channel
// Be ready to parse more than a single message in a single TCP chunk. // Be ready to parse more than a single message in a single TCP chunk.
while (true) while (true)
{ {
if (IsClosing())
return;
size_t read_len = this->bufferDataLen - this->msgStart; size_t read_len = this->bufferDataLen - this->msgStart;
char* json_start = nullptr; char* json_start = nullptr;
size_t json_len; size_t json_len;
......
...@@ -238,7 +238,6 @@ namespace RTC ...@@ -238,7 +238,6 @@ namespace RTC
this->state = IceState::DISCONNECTED; this->state = IceState::DISCONNECTED;
// Notify the listener. // Notify the listener.
this->listener->onICESelectedTuple(this, nullptr);
this->listener->onICEDisconnected(this); this->listener->onICEDisconnected(this);
} }
} }
......
...@@ -29,7 +29,7 @@ namespace RTC ...@@ -29,7 +29,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
MS_DEBUG("data received [local: %s : %u | remote: %s : %u]", MS_DEBUG("data received [local: %s : %u, remote: %s : %u]",
GetLocalIP().c_str(), (unsigned int)GetLocalPort(), GetLocalIP().c_str(), (unsigned int)GetLocalPort(),
GetPeerIP().c_str(), (unsigned int)GetPeerPort()); GetPeerIP().c_str(), (unsigned int)GetPeerPort());
...@@ -52,6 +52,12 @@ namespace RTC ...@@ -52,6 +52,12 @@ namespace RTC
// Be ready to parse more than a single frame in a single TCP chunk. // Be ready to parse more than a single frame in a single TCP chunk.
while (true) while (true)
{ {
// We may receive multiple packets in the same TCP chunk. If onw of them is
// a DTLS Close Alert this would be closed (Close() called) so we cannot call
// our listeners anymore.
if (IsClosing())
return;
size_t data_len = this->bufferDataLen - this->frameStart; size_t data_len = this->bufferDataLen - this->frameStart;
size_t packet_len; size_t packet_len;
......
...@@ -352,6 +352,7 @@ namespace RTC ...@@ -352,6 +352,7 @@ namespace RTC
// Notify the listener. // Notify the listener.
// NOTE: Don't do it if closing (since at this point the listener is already freed). // NOTE: Don't do it if closing (since at this point the listener is already freed).
// At the end, this is just called if the connection was remotely closed.
if (!IsClosing()) if (!IsClosing())
this->listener->onRTCTCPConnectionClosed(this, static_cast<RTC::TCPConnection*>(connection), is_closed_by_peer); this->listener->onRTCTCPConnectionClosed(this, static_cast<RTC::TCPConnection*>(connection), is_closed_by_peer);
} }
......
...@@ -226,20 +226,6 @@ namespace RTC ...@@ -226,20 +226,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (this->iceServer)
{
this->iceServer->Close();
this->iceServer = nullptr;
}
if (this->dtlsTransport)
{
this->dtlsTransport->Close();
this->dtlsTransport = nullptr;
}
ClosePorts();
// if (this->srtpRecvSession) // if (this->srtpRecvSession)
// { // {
// this->srtpRecvSession->Close(); // this->srtpRecvSession->Close();
...@@ -252,6 +238,25 @@ namespace RTC ...@@ -252,6 +238,25 @@ namespace RTC
// this->srtpSendSession = nullptr; // this->srtpSendSession = nullptr;
// } // }
if (this->dtlsTransport)
this->dtlsTransport->Close();
if (this->iceServer)
this->iceServer->Close();
for (auto socket : this->udpSockets)
socket->Close();
this->udpSockets.clear();
for (auto server : this->tcpServers)
server->Close();
this->tcpServers.clear();
this->selectedTuple = nullptr;
// Notify.
this->notifier->Emit(this->transportId, "close");
// If this was allocated (it did not throw in the constructor) notify the // If this was allocated (it did not throw in the constructor) notify the
// listener and delete it. // listener and delete it.
if (this->allocated) if (this->allocated)
...@@ -413,14 +418,6 @@ namespace RTC ...@@ -413,14 +418,6 @@ namespace RTC
RTC::DTLSTransport::Fingerprint remoteFingerprint; RTC::DTLSTransport::Fingerprint remoteFingerprint;
RTC::DTLSTransport::Role remoteRole = RTC::DTLSTransport::Role::AUTO; // Default value if missing. RTC::DTLSTransport::Role remoteRole = RTC::DTLSTransport::Role::AUTO; // Default value if missing.
if (!IsAlive())
{
MS_ERROR("Transport not alive");
request->Reject(500, "Transport not alive");
return;
}
// Ensure this method is not called twice. // Ensure this method is not called twice.
if (this->remoteDtlsParametersGiven) if (this->remoteDtlsParametersGiven)
{ {
...@@ -517,31 +514,12 @@ namespace RTC ...@@ -517,31 +514,12 @@ namespace RTC
} }
} }
// This closes all the UDP/TCP servers.
void Transport::ClosePorts()
{
MS_TRACE();
this->selectedTuple = nullptr;
for (auto socket : this->udpSockets)
socket->Close();
this->udpSockets.clear();
for (auto server : this->tcpServers)
server->Close();
this->tcpServers.clear();
}
Transport* Transport::CreateAssociatedTransport(uint32_t transportId) Transport* Transport::CreateAssociatedTransport(uint32_t transportId)
{ {
MS_TRACE(); MS_TRACE();
static Json::Value nullData(Json::nullValue); static Json::Value nullData(Json::nullValue);
if (!IsAlive())
MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a non alive Transport");
if (this->iceServer->GetComponent() != ICEServer::IceComponent::RTP) if (this->iceServer->GetComponent() != ICEServer::IceComponent::RTP)
MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a RTCP Transport"); MS_THROW_ERROR("cannot call CreateAssociatedTransport() on a RTCP Transport");
...@@ -693,9 +671,6 @@ namespace RTC ...@@ -693,9 +671,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(socket, remote_addr); RTC::TransportTuple tuple(socket, remote_addr);
onSTUNDataRecv(&tuple, data, len); onSTUNDataRecv(&tuple, data, len);
} }
...@@ -704,9 +679,6 @@ namespace RTC ...@@ -704,9 +679,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(socket, remote_addr); RTC::TransportTuple tuple(socket, remote_addr);
onDTLSDataRecv(&tuple, data, len); onDTLSDataRecv(&tuple, data, len);
} }
...@@ -715,9 +687,6 @@ namespace RTC ...@@ -715,9 +687,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(socket, remote_addr); RTC::TransportTuple tuple(socket, remote_addr);
onRTPDataRecv(&tuple, data, len); onRTPDataRecv(&tuple, data, len);
} }
...@@ -726,9 +695,6 @@ namespace RTC ...@@ -726,9 +695,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(socket, remote_addr); RTC::TransportTuple tuple(socket, remote_addr);
onRTCPDataRecv(&tuple, data, len); onRTCPDataRecv(&tuple, data, len);
} }
...@@ -737,11 +703,9 @@ namespace RTC ...@@ -737,11 +703,9 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(connection); RTC::TransportTuple tuple(connection);
if (is_closed_by_peer)
this->iceServer->RemoveTuple(&tuple); this->iceServer->RemoveTuple(&tuple);
} }
...@@ -749,9 +713,6 @@ namespace RTC ...@@ -749,9 +713,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(connection); RTC::TransportTuple tuple(connection);
onSTUNDataRecv(&tuple, data, len); onSTUNDataRecv(&tuple, data, len);
} }
...@@ -760,9 +721,6 @@ namespace RTC ...@@ -760,9 +721,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(connection); RTC::TransportTuple tuple(connection);
onDTLSDataRecv(&tuple, data, len); onDTLSDataRecv(&tuple, data, len);
} }
...@@ -779,9 +737,6 @@ namespace RTC ...@@ -779,9 +737,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
RTC::TransportTuple tuple(connection); RTC::TransportTuple tuple(connection);
onRTCPDataRecv(&tuple, data, len); onRTCPDataRecv(&tuple, data, len);
} }
...@@ -790,9 +745,6 @@ namespace RTC ...@@ -790,9 +745,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsAlive())
return;
// Send the STUN response over the same transport tuple. // Send the STUN response over the same transport tuple.
tuple->Send(msg->GetRaw(), msg->GetLength()); tuple->Send(msg->GetRaw(), msg->GetLength());
} }
...@@ -805,9 +757,6 @@ namespace RTC ...@@ -805,9 +757,6 @@ namespace RTC
Json::Value eventData; Json::Value eventData;
if (!IsAlive())
return;
/* /*
* RFC 5245 section 11.2 "Receiving Media": * RFC 5245 section 11.2 "Receiving Media":
* *
...@@ -815,15 +764,13 @@ namespace RTC ...@@ -815,15 +764,13 @@ namespace RTC
* on any candidates provided for that component. * on any candidates provided for that component.
*/ */
// Update the selected tuple.
this->selectedTuple = tuple; this->selectedTuple = tuple;
// Notify if tuple is given. // Notify.
if (tuple)
{
eventData[k_iceSelectedTuple] = tuple->toJson(); eventData[k_iceSelectedTuple] = tuple->toJson();
this->notifier->Emit(this->transportId, "iceselectedtuplechange", eventData); this->notifier->Emit(this->transportId, "iceselectedtuplechange", eventData);
} }
}
void Transport::onICEConnected(RTC::ICEServer* iceServer) void Transport::onICEConnected(RTC::ICEServer* iceServer)
{ {
...@@ -874,11 +821,15 @@ namespace RTC ...@@ -874,11 +821,15 @@ namespace RTC
MS_DEBUG("ICE disconnected"); MS_DEBUG("ICE disconnected");
// Unset the // Unset the selected tuple.
this->selectedTuple = nullptr;
// Notify. // Notify.
eventData[k_iceState] = v_disconnected; eventData[k_iceState] = v_disconnected;
this->notifier->Emit(this->transportId, "icestatechange", eventData); this->notifier->Emit(this->transportId, "icestatechange", eventData);
// This is a fatal error so close the transport.
Close();
} }
void Transport::onDTLSConnecting(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSConnecting(RTC::DTLSTransport* dtlsTransport)
...@@ -928,11 +879,12 @@ namespace RTC ...@@ -928,11 +879,12 @@ namespace RTC
MS_DEBUG("DTLS failed"); MS_DEBUG("DTLS failed");
ClosePorts();
// Notify. // Notify.
eventData[k_dtlsState] = v_failed; eventData[k_dtlsState] = v_failed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
// This is a fatal error so close the transport.
Close();
} }
void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport) void Transport::onDTLSClosed(RTC::DTLSTransport* dtlsTransport)
...@@ -944,13 +896,14 @@ namespace RTC ...@@ -944,13 +896,14 @@ namespace RTC
Json::Value eventData; Json::Value eventData;
MS_DEBUG("DTLS remotely disconnected"); MS_DEBUG("DTLS remotely closed");
ClosePorts();
// Notify. // Notify.
eventData[k_dtlsState] = v_closed; eventData[k_dtlsState] = v_closed;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData); this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
// This is a fatal error so close the transport.
Close();
} }
void Transport::onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len) void Transport::onOutgoingDTLSData(RTC::DTLSTransport* dtlsTransport, const MS_BYTE* data, size_t len)
......
...@@ -343,7 +343,7 @@ namespace RTC ...@@ -343,7 +343,7 @@ namespace RTC
MS_PORT port; MS_PORT port;
std::string ip; std::string ip;
Utils::IP::GetAddressInfo(addr, &family, ip, &port); Utils::IP::GetAddressInfo(addr, &family, ip, &port);
MS_DEBUG("%zu bytes datagram received [local: %s : %u | remote: %s : %u]", len, GetLocalIP().c_str(), (unsigned int)GetLocalPort(), ip.c_str(), (unsigned int)port); MS_DEBUG("%zu bytes datagram received [local: %s : %u, remote: %s : %u]", len, GetLocalIP().c_str(), (unsigned int)GetLocalPort(), ip.c_str(), (unsigned int)port);
} }
// Check if it's STUN. // Check if it's STUN.
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论