提交 3f08055c authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Move `DataProducer` notification handling from `DirectTransport` to `DataProducer` itself

上级 555e433d
......@@ -4,13 +4,16 @@
#include "common.hpp"
#include "Channel/ChannelRequest.hpp"
#include "Channel/ChannelSocket.hpp"
#include "PayloadChannel/PayloadChannelSocket.hpp"
#include "RTC/RTCP/Packet.hpp"
#include "RTC/SctpDictionaries.hpp"
#include <nlohmann/json.hpp>
#include <string>
namespace RTC
{
class DataProducer : public Channel::ChannelSocket::RequestHandler
class DataProducer : public Channel::ChannelSocket::RequestHandler,
public PayloadChannel::PayloadChannelSocket::NotificationHandler
{
public:
class Listener
......@@ -19,6 +22,7 @@ namespace RTC
virtual ~Listener() = default;
public:
virtual void OnDataProducerReceiveData(RTC::DataProducer* producer, size_t len) = 0;
virtual void OnDataProducerMessageReceived(
RTC::DataProducer* dataProducer, uint32_t ppid, const uint8_t* msg, size_t len) = 0;
};
......@@ -31,7 +35,8 @@ namespace RTC
};
public:
DataProducer(const std::string& id, RTC::DataProducer::Listener* listener, json& data);
DataProducer(
const std::string& id, size_t maxMessageSize, RTC::DataProducer::Listener* listener, json& data);
virtual ~DataProducer();
public:
......@@ -51,12 +56,17 @@ namespace RTC
public:
void HandleRequest(Channel::ChannelRequest* request) override;
/* Methods inherited from PayloadChannel::PayloadChannelSocket::NotificationHandler. */
public:
void HandleNotification(PayloadChannel::Notification* notification) override;
public:
// Passed by argument.
const std::string id;
private:
// Passed by argument.
size_t maxMessageSize{ 0u };
RTC::DataProducer::Listener* listener{ nullptr };
// Others.
Type type;
......
......@@ -223,6 +223,10 @@ namespace RTC
/* Pure virtual methods inherited from RTC::DataProducer::Listener. */
public:
void OnDataProducerReceiveData(RTC::DataProducer* /*dataProducer*/, size_t len) override
{
this->DataReceived(len);
}
void OnDataProducerMessageReceived(
RTC::DataProducer* dataProducer, uint32_t ppid, const uint8_t* msg, size_t len) override;
......
......@@ -11,8 +11,9 @@ namespace RTC
{
/* Instance methods. */
DataProducer::DataProducer(const std::string& id, RTC::DataProducer::Listener* listener, json& data)
: id(id), listener(listener)
DataProducer::DataProducer(
const std::string& id, size_t maxMessageSize, RTC::DataProducer::Listener* listener, json& data)
: id(id), maxMessageSize(maxMessageSize), listener(listener)
{
MS_TRACE();
......@@ -145,6 +146,51 @@ namespace RTC
}
}
void DataProducer::HandleNotification(PayloadChannel::Notification* notification)
{
MS_TRACE();
switch (notification->eventId)
{
case PayloadChannel::Notification::EventId::DATA_PRODUCER_SEND:
{
auto jsonPpidIt = notification->data.find("ppid");
if (jsonPpidIt == notification->data.end() || !Utils::Json::IsPositiveInteger(*jsonPpidIt))
{
MS_THROW_TYPE_ERROR("invalid ppid");
}
auto ppid = jsonPpidIt->get<uint32_t>();
const auto* msg = notification->payload;
auto len = notification->payloadLen;
if (len > this->maxMessageSize)
{
MS_WARN_TAG(
message,
"given message exceeds maxMessageSize value [maxMessageSize:%zu, len:%zu]",
len,
this->maxMessageSize);
break;
}
this->ReceiveMessage(ppid, msg, len);
// Increase receive transmission.
this->listener->OnDataProducerReceiveData(this, len);
break;
}
default:
{
MS_ERROR("unknown event '%s'", notification->event.c_str());
}
}
}
void DataProducer::ReceiveMessage(uint32_t ppid, const uint8_t* msg, size_t len)
{
MS_TRACE();
......
......@@ -102,32 +102,7 @@ namespace RTC
// This may throw.
RTC::DataProducer* dataProducer = GetDataProducerFromInternal(notification->internal);
auto jsonPpidIt = notification->data.find("ppid");
if (jsonPpidIt == notification->data.end() || !Utils::Json::IsPositiveInteger(*jsonPpidIt))
{
MS_THROW_TYPE_ERROR("invalid ppid");
}
auto ppid = jsonPpidIt->get<uint32_t>();
const auto* msg = notification->payload;
auto len = notification->payloadLen;
if (len > this->maxMessageSize)
{
MS_WARN_TAG(
message,
"given message exceeds maxMessageSize value [maxMessageSize:%zu, len:%zu]",
len,
this->maxMessageSize);
return;
}
dataProducer->ReceiveMessage(ppid, msg, len);
// Increase receive transmission.
RTC::Transport::DataReceived(len);
dataProducer->HandleNotification(notification);
break;
}
......
......@@ -1092,7 +1092,8 @@ namespace RTC
SetNewDataProducerIdFromInternal(request->internal, dataProducerId);
// This may throw.
auto* dataProducer = new RTC::DataProducer(dataProducerId, this, request->data);
auto* dataProducer =
new RTC::DataProducer(dataProducerId, this->maxMessageSize, this, request->data);
// Verify the type of the DataProducer.
switch (dataProducer->GetType())
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论