Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1422 follow up, Reuse ChannelRequest and ChannelNotification #1423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion worker/include/Channel/ChannelNotification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace Channel
static absl::flat_hash_map<FBS::Notification::Event, const char*> event2String;

public:
explicit ChannelNotification(const FBS::Notification::Notification* notification);
ChannelNotification() = default;
~ChannelNotification() = default;
void Receive(const FBS::Notification::Notification* notification);

public:
// Passed by argument.
Expand Down
9 changes: 5 additions & 4 deletions worker/include/Channel/ChannelRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ namespace Channel

public:
static absl::flat_hash_map<FBS::Request::Method, const char*> method2String;
thread_local static flatbuffers::FlatBufferBuilder bufferBuilder;

public:
ChannelRequest(Channel::ChannelSocket* channel, const FBS::Request::Request* request);
ChannelRequest(Channel::ChannelSocket* channel) : channel(channel){};
~ChannelRequest() = default;

flatbuffers::FlatBufferBuilder& GetBufferBuilder()
{
return ChannelRequest::bufferBuilder;
return this->bufferBuilder;
}
void Receive(const FBS::Request::Request* request);
void Accept();
template<class Body>
void Accept(FBS::Response::Body type, flatbuffers::Offset<Body>& body)
Expand All @@ -40,7 +40,7 @@ namespace Channel

this->replied = true;

auto& builder = ChannelRequest::bufferBuilder;
auto& builder = this->bufferBuilder;
auto response = FBS::Response::CreateResponse(builder, this->id, true, type, body.Union());

auto message =
Expand All @@ -62,6 +62,7 @@ namespace Channel
Channel::ChannelSocket* channel{ nullptr };
const FBS::Request::Request* data{ nullptr };
// Others.
flatbuffers::FlatBufferBuilder bufferBuilder{};
uint32_t id{ 0u };
Method method;
const char* methodCStr;
Expand Down
2 changes: 2 additions & 0 deletions worker/include/Channel/ChannelSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ namespace Channel
ChannelWriteCtx channelWriteCtx{ nullptr };
uv_async_t* uvReadHandle{ nullptr };
flatbuffers::FlatBufferBuilder bufferBuilder{};
Channel::ChannelRequest request{ this };
Channel::ChannelNotification notification{};
};
} // namespace Channel

Expand Down
2 changes: 1 addition & 1 deletion worker/src/Channel/ChannelNotification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Channel

/* Instance methods. */

ChannelNotification::ChannelNotification(const FBS::Notification::Notification* notification)
void ChannelNotification::Receive(const FBS::Notification::Notification* notification)
{
MS_TRACE();

Expand Down
14 changes: 5 additions & 9 deletions worker/src/Channel/ChannelRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

namespace Channel
{
/* Static variables. */

thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{};

/* Class variables. */

// clang-format off
Expand Down Expand Up @@ -93,14 +89,14 @@ namespace Channel
/**
* msg contains the request flatbuffer.
*/
ChannelRequest::ChannelRequest(Channel::ChannelSocket* channel, const FBS::Request::Request* request)
: channel(channel)
void ChannelRequest::Receive(const FBS::Request::Request* request)
{
MS_TRACE();

this->data = request;
this->id = request->id();
this->method = request->method();
this->data = request;
this->id = request->id();
this->method = request->method();
this->replied = false;

auto methodCStrIt = ChannelRequest::method2String.find(this->method);

Expand Down
40 changes: 12 additions & 28 deletions worker/src/Channel/ChannelSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,43 +196,35 @@ namespace Channel

if (message->data_type() == FBS::Message::Body::Request)
{
ChannelRequest* request;

try
{
request = new ChannelRequest(this, message->data_as<FBS::Request::Request>());
this->request.Receive(message->data_as<FBS::Request::Request>());

// Notify the listener.
this->listener->HandleRequest(request);
this->listener->HandleRequest(&request);
}
catch (const MediaSoupTypeError& error)
{
request->TypeError(error.what());
this->request.TypeError(error.what());
}
catch (const MediaSoupError& error)
{
request->Error(error.what());
this->request.Error(error.what());
}

delete request;
}
else if (message->data_type() == FBS::Message::Body::Notification)
{
ChannelNotification* notification;

try
{
notification = new ChannelNotification(message->data_as<FBS::Notification::Notification>());
this->notification.Receive(message->data_as<FBS::Notification::Notification>());

// Notify the listener.
this->listener->HandleNotification(notification);
this->listener->HandleNotification(&notification);
}
catch (const MediaSoupError& error)
{
MS_ERROR("notification failed: %s", error.what());
}

delete notification;
}
else
{
Expand Down Expand Up @@ -277,43 +269,35 @@ namespace Channel

if (message->data_type() == FBS::Message::Body::Request)
{
ChannelRequest* request;

try
{
request = new ChannelRequest(this, message->data_as<FBS::Request::Request>());
this->request.Receive(message->data_as<FBS::Request::Request>());

// Notify the listener.
this->listener->HandleRequest(request);
this->listener->HandleRequest(&request);
}
catch (const MediaSoupTypeError& error)
{
request->TypeError(error.what());
this->request.TypeError(error.what());
}
catch (const MediaSoupError& error)
{
request->Error(error.what());
this->request.Error(error.what());
}

delete request;
}
else if (message->data_type() == FBS::Message::Body::Notification)
{
ChannelNotification* notification;

try
{
notification = new ChannelNotification(message->data_as<FBS::Notification::Notification>());
this->notification.Receive(message->data_as<FBS::Notification::Notification>());

// Notify the listener.
this->listener->HandleNotification(notification);
this->listener->HandleNotification(&notification);
}
catch (const MediaSoupError& error)
{
MS_ERROR("notification failed: %s", error.what());
}

delete notification;
}
else
{
Expand Down
Loading