提交 6455d98b authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Router: code cleanup

上级 1da8fe23
...@@ -48,10 +48,12 @@ namespace RTC ...@@ -48,10 +48,12 @@ namespace RTC
void HandleRequest(Channel::Request* request); void HandleRequest(Channel::Request* request);
private: private:
RTC::Transport* GetTransportFromRequest( uint32_t GetNewTransportIdFromRequest(Channel::Request* request) const;
Channel::Request* request, uint32_t* transportId = nullptr) const; RTC::Transport* GetTransportFromRequest(Channel::Request* request) const;
RTC::Producer* GetProducerFromRequest(Channel::Request* request, uint32_t* producerId = nullptr) const; uint32_t GetNewProducerIdFromRequest(Channel::Request* request) const;
RTC::Consumer* GetConsumerFromRequest(Channel::Request* request, uint32_t* consumerId = nullptr) const; RTC::Producer* GetProducerFromRequest(Channel::Request* request) const;
uint32_t GetNewConsumerIdFromRequest(Channel::Request* request) const;
RTC::Consumer* GetConsumerFromRequest(Channel::Request* request) const;
/* Pure virtual methods inherited from RTC::Transport::Listener. */ /* Pure virtual methods inherited from RTC::Transport::Listener. */
public: public:
......
...@@ -180,12 +180,11 @@ namespace RTC ...@@ -180,12 +180,11 @@ namespace RTC
static const Json::StaticString JsonStringPreferUdp{ "preferUdp" }; static const Json::StaticString JsonStringPreferUdp{ "preferUdp" };
static const Json::StaticString JsonStringPreferTcp{ "preferTcp" }; static const Json::StaticString JsonStringPreferTcp{ "preferTcp" };
RTC::Transport* transport;
uint32_t transportId; uint32_t transportId;
try try
{ {
transport = GetTransportFromRequest(request, &transportId); transportId = GetNewTransportIdFromRequest(request);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
{ {
...@@ -194,13 +193,6 @@ namespace RTC ...@@ -194,13 +193,6 @@ namespace RTC
return; return;
} }
if (transport != nullptr)
{
request->Reject("Transport already exists");
return;
}
RTC::Transport::TransportOptions options; RTC::Transport::TransportOptions options;
if (request->data[JsonStringUdp].isBool()) if (request->data[JsonStringUdp].isBool())
...@@ -216,8 +208,11 @@ namespace RTC ...@@ -216,8 +208,11 @@ namespace RTC
if (request->data[JsonStringPreferTcp].isBool()) if (request->data[JsonStringPreferTcp].isBool())
options.preferTcp = request->data[JsonStringPreferTcp].asBool(); options.preferTcp = request->data[JsonStringPreferTcp].asBool();
RTC::Transport* transport;
try try
{ {
// NOTE: This may throw.
transport = new RTC::Transport(this, this->notifier, transportId, options); transport = new RTC::Transport(this, this->notifier, transportId, options);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
...@@ -227,6 +222,7 @@ namespace RTC ...@@ -227,6 +222,7 @@ namespace RTC
return; return;
} }
// Insert into the map.
this->transports[transportId] = transport; this->transports[transportId] = transport;
MS_DEBUG_DEV("Transport created [transportId:%" PRIu32 "]", transportId); MS_DEBUG_DEV("Transport created [transportId:%" PRIu32 "]", transportId);
...@@ -247,13 +243,11 @@ namespace RTC ...@@ -247,13 +243,11 @@ namespace RTC
static const Json::StaticString JsonStringCodecPayloadTypes{ "codecPayloadTypes" }; static const Json::StaticString JsonStringCodecPayloadTypes{ "codecPayloadTypes" };
static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" }; static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" };
RTC::Producer* producer;
RTC::Transport* transport;
uint32_t producerId; uint32_t producerId;
try try
{ {
producer = GetProducerFromRequest(request, &producerId); producerId = GetNewProducerIdFromRequest(request);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
{ {
...@@ -262,12 +256,7 @@ namespace RTC ...@@ -262,12 +256,7 @@ namespace RTC
return; return;
} }
if (producer != nullptr) RTC::Transport* transport;
{
request->Reject("Producer already exists");
return;
}
try try
{ {
...@@ -280,13 +269,6 @@ namespace RTC ...@@ -280,13 +269,6 @@ namespace RTC
return; return;
} }
if (transport == nullptr)
{
request->Reject("Transport does not exist");
return;
}
if (!request->data[JsonStringKind].isString()) if (!request->data[JsonStringKind].isString())
{ {
request->Reject("missing data.kind"); request->Reject("missing data.kind");
...@@ -374,7 +356,7 @@ namespace RTC ...@@ -374,7 +356,7 @@ namespace RTC
paused = request->data[JsonStringPaused].asBool(); paused = request->data[JsonStringPaused].asBool();
// Create a Producer instance. // Create a Producer instance.
producer = new RTC::Producer( RTC::Producer* producer = new RTC::Producer(
this->notifier, producerId, kind, transport, rtpParameters, rtpMapping, paused); this->notifier, producerId, kind, transport, rtpParameters, rtpMapping, paused);
// Add us as listener. // Add us as listener.
...@@ -410,13 +392,11 @@ namespace RTC ...@@ -410,13 +392,11 @@ namespace RTC
{ {
static const Json::StaticString JsonStringKind{ "kind" }; static const Json::StaticString JsonStringKind{ "kind" };
RTC::Consumer* consumer;
RTC::Producer* producer;
uint32_t consumerId; uint32_t consumerId;
try try
{ {
consumer = GetConsumerFromRequest(request, &consumerId); consumerId = GetNewConsumerIdFromRequest(request);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
{ {
...@@ -425,12 +405,7 @@ namespace RTC ...@@ -425,12 +405,7 @@ namespace RTC
return; return;
} }
if (consumer != nullptr) RTC::Producer* producer;
{
request->Reject("Consumer already exists");
return;
}
try try
{ {
...@@ -443,13 +418,6 @@ namespace RTC ...@@ -443,13 +418,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
if (!request->data[JsonStringKind].isString()) if (!request->data[JsonStringKind].isString())
{ {
request->Reject("missing data.kind"); request->Reject("missing data.kind");
...@@ -477,7 +445,8 @@ namespace RTC ...@@ -477,7 +445,8 @@ namespace RTC
return; return;
} }
consumer = new RTC::Consumer(this->notifier, consumerId, kind, producer->producerId); RTC::Consumer* consumer =
new RTC::Consumer(this->notifier, consumerId, kind, producer->producerId);
// If the Producer is paused tell it to the new Consumer. // If the Producer is paused tell it to the new Consumer.
if (producer->IsPaused()) if (producer->IsPaused())
...@@ -545,20 +514,33 @@ namespace RTC ...@@ -545,20 +514,33 @@ namespace RTC
return; return;
} }
if (transport == nullptr) transport->Destroy();
request->Accept();
break;
}
case Channel::Request::MethodId::TRANSPORT_DUMP:
{
RTC::Transport* transport;
try
{
transport = GetTransportFromRequest(request);
}
catch (const MediaSoupError& error)
{ {
request->Reject("Transport does not exist"); request->Reject(error.what());
return; return;
} }
transport->Destroy(); transport->HandleRequest(request);
request->Accept();
break; break;
} }
case Channel::Request::MethodId::TRANSPORT_DUMP:
case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS: case Channel::Request::MethodId::TRANSPORT_SET_REMOTE_DTLS_PARAMETERS:
{ {
static const Json::StaticString JsonStringRole{ "role" }; static const Json::StaticString JsonStringRole{ "role" };
...@@ -581,13 +563,6 @@ namespace RTC ...@@ -581,13 +563,6 @@ namespace RTC
return; return;
} }
if (transport == nullptr)
{
request->Reject("Transport does not exist");
return;
}
if (!request->data[JsonStringFingerprints].isArray()) if (!request->data[JsonStringFingerprints].isArray())
{ {
request->Reject("missing data.fingerprints"); request->Reject("missing data.fingerprints");
...@@ -690,13 +665,6 @@ namespace RTC ...@@ -690,13 +665,6 @@ namespace RTC
return; return;
} }
if (transport == nullptr)
{
request->Reject("Transport does not exist");
return;
}
if (!request->data[JsonStringBitrate].isUInt()) if (!request->data[JsonStringBitrate].isUInt())
{ {
request->Reject("missing data.bitrate"); request->Reject("missing data.bitrate");
...@@ -731,13 +699,6 @@ namespace RTC ...@@ -731,13 +699,6 @@ namespace RTC
return; return;
} }
if (transport == nullptr)
{
request->Reject("Transport does not exist");
return;
}
std::string usernameFragment = Utils::Crypto::GetRandomString(16); std::string usernameFragment = Utils::Crypto::GetRandomString(16);
std::string password = Utils::Crypto::GetRandomString(32); std::string password = Utils::Crypto::GetRandomString(32);
...@@ -768,14 +729,8 @@ namespace RTC ...@@ -768,14 +729,8 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
producer->Destroy(); producer->Destroy();
request->Accept(); request->Accept();
break; break;
...@@ -796,13 +751,6 @@ namespace RTC ...@@ -796,13 +751,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
producer->HandleRequest(request); producer->HandleRequest(request);
break; break;
...@@ -825,13 +773,6 @@ namespace RTC ...@@ -825,13 +773,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
if (!request->data[JsonStringRtpParameters].isObject()) if (!request->data[JsonStringRtpParameters].isObject())
{ {
request->Reject("missing data.rtpParameters"); request->Reject("missing data.rtpParameters");
...@@ -876,13 +817,6 @@ namespace RTC ...@@ -876,13 +817,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
producer->Pause(); producer->Pause();
request->Accept(); request->Accept();
...@@ -905,13 +839,6 @@ namespace RTC ...@@ -905,13 +839,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
producer->Resume(); producer->Resume();
request->Accept(); request->Accept();
...@@ -936,13 +863,6 @@ namespace RTC ...@@ -936,13 +863,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
if (!request->data[JsonStringEnabled].isBool()) if (!request->data[JsonStringEnabled].isBool())
{ {
request->Reject("Request has invalid data.enabled"); request->Reject("Request has invalid data.enabled");
...@@ -976,13 +896,6 @@ namespace RTC ...@@ -976,13 +896,6 @@ namespace RTC
return; return;
} }
if (producer == nullptr)
{
request->Reject("Producer does not exist");
return;
}
if (!request->data[JsonStringEnabled].isBool()) if (!request->data[JsonStringEnabled].isBool())
{ {
request->Reject("Request has invalid data.enabled"); request->Reject("Request has invalid data.enabled");
...@@ -1014,14 +927,8 @@ namespace RTC ...@@ -1014,14 +927,8 @@ namespace RTC
return; return;
} }
if (consumer == nullptr)
{
request->Reject("Consumer does not exist");
return;
}
consumer->Destroy(); consumer->Destroy();
request->Accept(); request->Accept();
break; break;
...@@ -1032,7 +939,6 @@ namespace RTC ...@@ -1032,7 +939,6 @@ namespace RTC
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" }; static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
RTC::Consumer* consumer; RTC::Consumer* consumer;
RTC::Transport* transport;
try try
{ {
...@@ -1045,12 +951,7 @@ namespace RTC ...@@ -1045,12 +951,7 @@ namespace RTC
return; return;
} }
if (consumer == nullptr) RTC::Transport* transport;
{
request->Reject("Consumer does not exist");
return;
}
try try
{ {
...@@ -1063,13 +964,6 @@ namespace RTC ...@@ -1063,13 +964,6 @@ namespace RTC
return; return;
} }
if (transport == nullptr)
{
request->Reject("Transport does not exist");
return;
}
if (!request->data[JsonStringRtpParameters].isObject()) if (!request->data[JsonStringRtpParameters].isObject())
{ {
request->Reject("missing data.rtpParameters"); request->Reject("missing data.rtpParameters");
...@@ -1116,13 +1010,6 @@ namespace RTC ...@@ -1116,13 +1010,6 @@ namespace RTC
return; return;
} }
if (consumer == nullptr)
{
request->Reject("Consumer does not exist");
return;
}
consumer->HandleRequest(request); consumer->HandleRequest(request);
break; break;
...@@ -1143,13 +1030,6 @@ namespace RTC ...@@ -1143,13 +1030,6 @@ namespace RTC
return; return;
} }
if (consumer == nullptr)
{
request->Reject("Consumer does not exist");
return;
}
consumer->Pause(); consumer->Pause();
request->Accept(); request->Accept();
...@@ -1172,13 +1052,6 @@ namespace RTC ...@@ -1172,13 +1052,6 @@ namespace RTC
return; return;
} }
if (consumer == nullptr)
{
request->Reject("Consumer does not exist");
return;
}
consumer->Resume(); consumer->Resume();
request->Accept(); request->Accept();
...@@ -1195,7 +1068,12 @@ namespace RTC ...@@ -1195,7 +1068,12 @@ namespace RTC
} }
} }
RTC::Transport* Router::GetTransportFromRequest(Channel::Request* request, uint32_t* transportId) const /**
* Looks for internal.transportId numeric field and ensures no Transport exists
* with such a id.
* NOTE: It may throw if invalid request or Transport already exists.
*/
uint32_t Router::GetNewTransportIdFromRequest(Channel::Request* request) const
{ {
MS_TRACE(); MS_TRACE();
...@@ -1206,21 +1084,47 @@ namespace RTC ...@@ -1206,21 +1084,47 @@ namespace RTC
if (!jsonTransportId.isUInt()) if (!jsonTransportId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.transportId"); MS_THROW_ERROR("Request has not numeric internal.transportId");
if (transportId != nullptr) uint32_t transportId = jsonTransportId.asUInt();
*transportId = jsonTransportId.asUInt();
auto it = this->transports.find(jsonTransportId.asUInt()); if (this->transports.find(transportId) != this->transports.end())
if (it != this->transports.end()) MS_THROW_ERROR("a Transport with same transportId already exists");
{
auto* transport = it->second;
return transport; return transportId;
} }
return nullptr; /**
* Looks for internal.transportId numeric field and returns the corresponding
* Transport.
* NOTE: It may throw if invalid request or Transport does not exist.
*/
RTC::Transport* Router::GetTransportFromRequest(Channel::Request* request) const
{
MS_TRACE();
static const Json::StaticString JsonStringTransportId{ "transportId" };
auto jsonTransportId = request->internal[JsonStringTransportId];
if (!jsonTransportId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.transportId");
uint32_t transportId = jsonTransportId.asUInt();
auto it = this->transports.find(transportId);
if (it == this->transports.end())
MS_THROW_ERROR("Transport not found");
auto* transport = it->second;
return transport;
} }
RTC::Producer* Router::GetProducerFromRequest(Channel::Request* request, uint32_t* producerId) const /**
* Looks for internal.producerId numeric field and ensures no Producer exists
* with such a id.
* NOTE: It may throw if invalid request or Producer already exists.
*/
uint32_t Router::GetNewProducerIdFromRequest(Channel::Request* request) const
{ {
MS_TRACE(); MS_TRACE();
...@@ -1231,21 +1135,47 @@ namespace RTC ...@@ -1231,21 +1135,47 @@ namespace RTC
if (!jsonProducerId.isUInt()) if (!jsonProducerId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.producerId"); MS_THROW_ERROR("Request has not numeric internal.producerId");
if (producerId != nullptr) uint32_t producerId = jsonProducerId.asUInt();
*producerId = jsonProducerId.asUInt();
auto it = this->producers.find(jsonProducerId.asUInt()); if (this->producers.find(producerId) != this->producers.end())
if (it != this->producers.end()) MS_THROW_ERROR("a Producer with same producerId already exists");
{
auto* producer = it->second;
return producer; return producerId;
} }
/**
* Looks for internal.producerId numeric field and returns the corresponding
* Producer.
* NOTE: It may throw if invalid request or Producer does not exist.
*/
RTC::Producer* Router::GetProducerFromRequest(Channel::Request* request) const
{
MS_TRACE();
static const Json::StaticString JsonStringProducerId{ "producerId" };
auto jsonProducerId = request->internal[JsonStringProducerId];
return nullptr; if (!jsonProducerId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.producerId");
uint32_t producerId = jsonProducerId.asUInt();
auto it = this->producers.find(producerId);
if (it == this->producers.end())
MS_THROW_ERROR("Producer not found");
auto* producer = it->second;
return producer;
} }
RTC::Consumer* Router::GetConsumerFromRequest(Channel::Request* request, uint32_t* consumerId) const /**
* Looks for internal.consumerId numeric field and ensures no Consumer exists
* with such a id.
* NOTE: It may throw if invalid request or Consumer already exists.
*/
uint32_t Router::GetNewConsumerIdFromRequest(Channel::Request* request) const
{ {
MS_TRACE(); MS_TRACE();
...@@ -1256,18 +1186,39 @@ namespace RTC ...@@ -1256,18 +1186,39 @@ namespace RTC
if (!jsonConsumerId.isUInt()) if (!jsonConsumerId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.consumerId"); MS_THROW_ERROR("Request has not numeric internal.consumerId");
if (consumerId != nullptr) uint32_t consumerId = jsonConsumerId.asUInt();
*consumerId = jsonConsumerId.asUInt();
auto it = this->consumers.find(jsonConsumerId.asUInt()); if (this->consumers.find(consumerId) != this->consumers.end())
if (it != this->consumers.end()) MS_THROW_ERROR("a Consumer with same consumerId already exists");
{
auto* consumer = it->second;
return consumer; return consumerId;
} }
/**
* Looks for internal.consumerId numeric field and returns the corresponding
* Consumer.
* NOTE: It may throw if invalid request or Consumer does not exist.
*/
RTC::Consumer* Router::GetConsumerFromRequest(Channel::Request* request) const
{
MS_TRACE();
static const Json::StaticString JsonStringConsumerId{ "consumerId" };
auto jsonConsumerId = request->internal[JsonStringConsumerId];
if (!jsonConsumerId.isUInt())
MS_THROW_ERROR("Request has not numeric internal.consumerId");
uint32_t consumerId = jsonConsumerId.asUInt();
auto it = this->consumers.find(consumerId);
if (it == this->consumers.end())
MS_THROW_ERROR("Consumer not found");
auto* consumer = it->second;
return nullptr; return consumer;
} }
void Router::OnTransportClosed(RTC::Transport* transport) void Router::OnTransportClosed(RTC::Transport* transport)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论