提交 bf309e06 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Worker: start createPeer method.

上级 473d74b9
...@@ -17,7 +17,7 @@ public: ...@@ -17,7 +17,7 @@ public:
~Loop(); ~Loop();
private: private:
void HandleCreateRoomRequest(Channel::Request* request); RTC::Room* GetRoomFromRequest(Channel::Request* request, unsigned int* roomId);
void Close(); void Close();
/* Methods inherited from SignalsHandler::Listener. */ /* Methods inherited from SignalsHandler::Listener. */
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "RTC/Peer.h" #include "RTC/Peer.h"
#include "RTC/RTPPacket.h" #include "RTC/RTPPacket.h"
#include "RTC/RTCPPacket.h" #include "RTC/RTCPPacket.h"
#include "Channel/Request.h"
#include <unordered_map> #include <unordered_map>
#include <json/json.h> #include <json/json.h>
...@@ -18,6 +19,8 @@ namespace RTC ...@@ -18,6 +19,8 @@ namespace RTC
Room(unsigned int roomId); Room(unsigned int roomId);
virtual ~Room(); virtual ~Room();
void HandleCreatePeerRequest(Channel::Request* request);
void Close(); void Close();
/* Pure virtual methods inherited from RTC::Peer::Listener. */ /* Pure virtual methods inherited from RTC::Peer::Listener. */
......
...@@ -39,44 +39,31 @@ Loop::~Loop() ...@@ -39,44 +39,31 @@ Loop::~Loop()
MS_TRACE(); MS_TRACE();
} }
void Loop::HandleCreateRoomRequest(Channel::Request* request) RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roomId = nullptr)
{ {
MS_TRACE(); MS_TRACE();
auto jsonRoomId = request->data["roomId"]; auto jsonRoomId = request->data["roomId"];
if (!jsonRoomId.isUInt()) if (!jsonRoomId.isUInt())
{ MS_THROW_ERROR("Request has no numeric .roomId field");
MS_ERROR("Request has no numeric .roomId field");
request->Reject(500, "Request has no numeric .roomId field"); // If given, fill roomId.
return; if (roomId)
} *roomId = jsonRoomId.asUInt();
unsigned int roomId = jsonRoomId.asUInt(); auto it = this->rooms.find(jsonRoomId.asUInt());
auto it = this->rooms.find(roomId);
RTC::Room* room;
if (it != this->rooms.end()) if (it != this->rooms.end())
{ {
MS_ERROR("Room with roomId '%u' already exists", roomId); RTC::Room* room = it->second;
request->Reject(500, "Room already exists"); return room;
return;
} }
else
room = RTC::Room::Factory(roomId, request->data);
if (!room)
{ {
MS_ERROR("failed to create a Room"); return nullptr;
request->Reject(500, "failed to create a Room");
return;
} }
this->rooms[roomId] = room;
request->Accept();
} }
void Loop::Close() void Loop::Close()
...@@ -158,7 +145,43 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -158,7 +145,43 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
{ {
MS_DEBUG("'createRoom' method"); MS_DEBUG("'createRoom' method");
this->HandleCreateRoomRequest(request); RTC::Room* room;
unsigned int roomId;
try
{
room = GetRoomFromRequest(request, &roomId);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (room)
{
MS_ERROR("Room already exists");
request->Reject(500, "Room already exists");
return;
}
room = RTC::Room::Factory(roomId, request->data);
if (!room)
{
MS_ERROR("failed to create a Room");
request->Reject(500, "failed to create a Room");
return;
}
this->rooms[roomId] = room;
request->Accept();
break; break;
} }
...@@ -166,7 +189,30 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -166,7 +189,30 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
{ {
MS_DEBUG("'createPeer' method"); MS_DEBUG("'createPeer' method");
request->Reject(500, "lalalala failed to create a Peer because yes!"); RTC::Room* room;
try
{
room = GetRoomFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!room)
{
MS_ERROR("Room does not exist");
request->Reject(500, "Room does not exist");
return;
}
room->HandleCreatePeerRequest(request);
break; break;
} }
......
...@@ -49,6 +49,14 @@ namespace RTC ...@@ -49,6 +49,14 @@ namespace RTC
MS_TRACE(); MS_TRACE();
} }
void Room::HandleCreatePeerRequest(Channel::Request* request)
{
MS_TRACE();
// TODO: jeje
request->Accept();
}
void Room::Close() void Room::Close()
{ {
MS_TRACE(); MS_TRACE();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论