diff --git a/worker/include/Channel/ChannelNotification.hpp b/worker/include/Channel/ChannelNotification.hpp index 2aa940656d..7afa5d0c6f 100644 --- a/worker/include/Channel/ChannelNotification.hpp +++ b/worker/include/Channel/ChannelNotification.hpp @@ -17,8 +17,9 @@ namespace Channel static absl::flat_hash_map event2String; public: - explicit ChannelNotification(const FBS::Notification::Notification* notification); + ChannelNotification() = default; ~ChannelNotification() = default; + void Receive(const FBS::Notification::Notification* notification); public: // Passed by argument. diff --git a/worker/include/Channel/ChannelRequest.hpp b/worker/include/Channel/ChannelRequest.hpp index 2df2d5dea2..472a2aeb91 100644 --- a/worker/include/Channel/ChannelRequest.hpp +++ b/worker/include/Channel/ChannelRequest.hpp @@ -22,16 +22,16 @@ namespace Channel public: static absl::flat_hash_map 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 void Accept(FBS::Response::Body type, flatbuffers::Offset& body) @@ -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 = @@ -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; diff --git a/worker/include/Channel/ChannelSocket.hpp b/worker/include/Channel/ChannelSocket.hpp index 83935b8b81..1bf556f485 100644 --- a/worker/include/Channel/ChannelSocket.hpp +++ b/worker/include/Channel/ChannelSocket.hpp @@ -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 diff --git a/worker/src/Channel/ChannelNotification.cpp b/worker/src/Channel/ChannelNotification.cpp index 9facac2b7c..0708fdf5d7 100644 --- a/worker/src/Channel/ChannelNotification.cpp +++ b/worker/src/Channel/ChannelNotification.cpp @@ -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(); diff --git a/worker/src/Channel/ChannelRequest.cpp b/worker/src/Channel/ChannelRequest.cpp index 82830bef7c..4d68517df3 100644 --- a/worker/src/Channel/ChannelRequest.cpp +++ b/worker/src/Channel/ChannelRequest.cpp @@ -7,10 +7,6 @@ namespace Channel { - /* Static variables. */ - - thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{}; - /* Class variables. */ // clang-format off @@ -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); diff --git a/worker/src/Channel/ChannelSocket.cpp b/worker/src/Channel/ChannelSocket.cpp index 144712501b..82ed0fd746 100644 --- a/worker/src/Channel/ChannelSocket.cpp +++ b/worker/src/Channel/ChannelSocket.cpp @@ -196,43 +196,35 @@ namespace Channel if (message->data_type() == FBS::Message::Body::Request) { - ChannelRequest* request; - try { - request = new ChannelRequest(this, message->data_as()); + this->request.Receive(message->data_as()); // 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()); + this->notification.Receive(message->data_as()); // Notify the listener. - this->listener->HandleNotification(notification); + this->listener->HandleNotification(¬ification); } catch (const MediaSoupError& error) { MS_ERROR("notification failed: %s", error.what()); } - - delete notification; } else { @@ -277,43 +269,35 @@ namespace Channel if (message->data_type() == FBS::Message::Body::Request) { - ChannelRequest* request; - try { - request = new ChannelRequest(this, message->data_as()); + this->request.Receive(message->data_as()); // 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()); + this->notification.Receive(message->data_as()); // Notify the listener. - this->listener->HandleNotification(notification); + this->listener->HandleNotification(¬ification); } catch (const MediaSoupError& error) { MS_ERROR("notification failed: %s", error.what()); } - - delete notification; } else {