提交 23e26f80 authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Move some `DataConsumer` method handling from `Transport` to `DataConsumer` itself

上级 195d27d1
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
namespace RTC namespace RTC
{ {
// Define class here such that we can use it even though we don't know what it looks like yet
// (this is to avoid circular dependencies).
class SctpAssociation;
class DataConsumer : public Channel::ChannelSocket::RequestHandler, class DataConsumer : public Channel::ChannelSocket::RequestHandler,
public PayloadChannel::PayloadChannelSocket::RequestHandler public PayloadChannel::PayloadChannelSocket::RequestHandler
{ {
...@@ -45,6 +49,7 @@ namespace RTC ...@@ -45,6 +49,7 @@ namespace RTC
DataConsumer( DataConsumer(
const std::string& id, const std::string& id,
const std::string& dataProducerId, const std::string& dataProducerId,
RTC::SctpAssociation* sctpAssociation,
RTC::DataConsumer::Listener* listener, RTC::DataConsumer::Listener* listener,
json& data, json& data,
size_t maxMessageSize); size_t maxMessageSize);
...@@ -94,6 +99,7 @@ namespace RTC ...@@ -94,6 +99,7 @@ namespace RTC
private: private:
// Passed by argument. // Passed by argument.
RTC::SctpAssociation* sctpAssociation{ nullptr };
RTC::DataConsumer::Listener* listener{ nullptr }; RTC::DataConsumer::Listener* listener{ nullptr };
size_t maxMessageSize{ 0u }; size_t maxMessageSize{ 0u };
// Others. // Others.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "Channel/ChannelNotifier.hpp" #include "Channel/ChannelNotifier.hpp"
#include "RTC/SctpAssociation.hpp"
namespace RTC namespace RTC
{ {
...@@ -15,10 +16,12 @@ namespace RTC ...@@ -15,10 +16,12 @@ namespace RTC
DataConsumer::DataConsumer( DataConsumer::DataConsumer(
const std::string& id, const std::string& id,
const std::string& dataProducerId, const std::string& dataProducerId,
RTC::SctpAssociation* sctpAssociation,
RTC::DataConsumer::Listener* listener, RTC::DataConsumer::Listener* listener,
json& data, json& data,
size_t maxMessageSize) size_t maxMessageSize)
: id(id), dataProducerId(dataProducerId), listener(listener), maxMessageSize(maxMessageSize) : id(id), dataProducerId(dataProducerId), sctpAssociation(sctpAssociation), listener(listener),
maxMessageSize(maxMessageSize)
{ {
MS_TRACE(); MS_TRACE();
...@@ -153,8 +156,35 @@ namespace RTC ...@@ -153,8 +156,35 @@ namespace RTC
break; break;
} }
case Channel::ChannelRequest::MethodId::DATA_CONSUMER_GET_BUFFERED_AMOUNT:
{
if (this->GetType() != RTC::DataConsumer::Type::SCTP)
{
MS_THROW_TYPE_ERROR("invalid DataConsumer type");
}
if (!this->sctpAssociation)
{
MS_THROW_ERROR("no SCTP association present");
}
// Create status response.
json data = json::object();
data["bufferedAmount"] = this->sctpAssociation->GetSctpBufferedAmount();
request->Accept(data);
break;
}
case Channel::ChannelRequest::MethodId::DATA_CONSUMER_SET_BUFFERED_AMOUNT_LOW_THRESHOLD: case Channel::ChannelRequest::MethodId::DATA_CONSUMER_SET_BUFFERED_AMOUNT_LOW_THRESHOLD:
{ {
if (this->GetType() != DataConsumer::Type::SCTP)
{
MS_THROW_TYPE_ERROR("invalid DataConsumer type");
}
auto jsonThresholdIt = request->data.find("threshold"); auto jsonThresholdIt = request->data.find("threshold");
if (jsonThresholdIt == request->data.end() || !jsonThresholdIt->is_number_unsigned()) if (jsonThresholdIt == request->data.end() || !jsonThresholdIt->is_number_unsigned())
...@@ -200,6 +230,16 @@ namespace RTC ...@@ -200,6 +230,16 @@ namespace RTC
{ {
case PayloadChannel::PayloadChannelRequest::MethodId::DATA_CONSUMER_SEND: case PayloadChannel::PayloadChannelRequest::MethodId::DATA_CONSUMER_SEND:
{ {
if (this->GetType() != RTC::DataConsumer::Type::SCTP)
{
MS_THROW_TYPE_ERROR("invalid DataConsumer type");
}
if (!this->sctpAssociation)
{
MS_THROW_ERROR("no SCTP association present");
}
auto jsonPpidIt = request->data.find("ppid"); auto jsonPpidIt = request->data.find("ppid");
if (jsonPpidIt == request->data.end() || !Utils::Json::IsPositiveInteger(*jsonPpidIt)) if (jsonPpidIt == request->data.end() || !Utils::Json::IsPositiveInteger(*jsonPpidIt))
......
...@@ -1197,7 +1197,7 @@ namespace RTC ...@@ -1197,7 +1197,7 @@ namespace RTC
// This may throw. // This may throw.
auto* dataConsumer = new RTC::DataConsumer( auto* dataConsumer = new RTC::DataConsumer(
dataConsumerId, dataProducerId, this, request->data, this->maxMessageSize); dataConsumerId, dataProducerId, this->sctpAssociation, this, request->data, this->maxMessageSize);
// Verify the type of the DataConsumer. // Verify the type of the DataConsumer.
switch (dataConsumer->GetType()) switch (dataConsumer->GetType())
...@@ -1486,50 +1486,12 @@ namespace RTC ...@@ -1486,50 +1486,12 @@ namespace RTC
case Channel::ChannelRequest::MethodId::DATA_CONSUMER_DUMP: case Channel::ChannelRequest::MethodId::DATA_CONSUMER_DUMP:
case Channel::ChannelRequest::MethodId::DATA_CONSUMER_GET_STATS: case Channel::ChannelRequest::MethodId::DATA_CONSUMER_GET_STATS:
{
// This may throw.
RTC::DataConsumer* dataConsumer = GetDataConsumerFromInternal(request->internal);
dataConsumer->HandleRequest(request);
break;
}
case Channel::ChannelRequest::MethodId::DATA_CONSUMER_GET_BUFFERED_AMOUNT: case Channel::ChannelRequest::MethodId::DATA_CONSUMER_GET_BUFFERED_AMOUNT:
{
// This may throw.
RTC::DataConsumer* dataConsumer = GetDataConsumerFromInternal(request->internal);
if (dataConsumer->GetType() != RTC::DataConsumer::Type::SCTP)
{
MS_THROW_TYPE_ERROR("invalid DataConsumer type");
}
if (!this->sctpAssociation)
{
MS_THROW_ERROR("no SCTP association present");
}
// Create status response.
json data = json::object();
data["bufferedAmount"] = this->sctpAssociation->GetSctpBufferedAmount();
request->Accept(data);
break;
}
case Channel::ChannelRequest::MethodId::DATA_CONSUMER_SET_BUFFERED_AMOUNT_LOW_THRESHOLD: case Channel::ChannelRequest::MethodId::DATA_CONSUMER_SET_BUFFERED_AMOUNT_LOW_THRESHOLD:
{ {
// This may throw. // This may throw.
RTC::DataConsumer* dataConsumer = GetDataConsumerFromInternal(request->internal); RTC::DataConsumer* dataConsumer = GetDataConsumerFromInternal(request->internal);
if (dataConsumer->GetType() != RTC::DataConsumer::Type::SCTP)
{
MS_THROW_TYPE_ERROR("invalid DataConsumer type");
}
dataConsumer->HandleRequest(request); dataConsumer->HandleRequest(request);
break; break;
...@@ -1553,16 +1515,6 @@ namespace RTC ...@@ -1553,16 +1515,6 @@ namespace RTC
// This may throw. // This may throw.
RTC::DataConsumer* dataConsumer = GetDataConsumerFromInternal(request->internal); RTC::DataConsumer* dataConsumer = GetDataConsumerFromInternal(request->internal);
if (dataConsumer->GetType() != RTC::DataConsumer::Type::SCTP)
{
MS_THROW_TYPE_ERROR("invalid DataConsumer type");
}
if (!this->sctpAssociation)
{
MS_THROW_ERROR("no SCTP association present");
}
dataConsumer->HandleRequest(request); dataConsumer->HandleRequest(request);
break; break;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论