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

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

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