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

Worker: improve code

上级 6435c1b8
......@@ -4,4 +4,5 @@
/worker/cldoc/
/worker/mediasoup-tester.sh
/TODO
/make-trace.sh
/run.js
......@@ -23,17 +23,13 @@ namespace Channel
closePeer
};
public:
static Channel::Request* Factory(Channel::UnixStreamSocket* channel, Json::Value& json);
private:
static std::unordered_map<std::string, MethodId> string2MethodId;
public:
Request(Channel::UnixStreamSocket* channel, unsigned int id, std::string& method, MethodId methodId, Json::Value& data);
Request(Channel::UnixStreamSocket* channel, Json::Value& json);
virtual ~Request();
// void Accept(Json::Value data = Json::Value(Json::nullValue));
void Accept();
void Accept(Json::Value &data);
void Reject(unsigned int status, std::string& reason);
......
#define MS_CLASS "Channel::Request"
#include "Channel/Request.h"
#include "MediaSoupError.h"
#include "Logger.h"
namespace Channel
......@@ -20,72 +21,34 @@ namespace Channel
{ "closePeer", Request::MethodId::closePeer }
};
/* Class methods. */
/* Instance methods. */
Channel::Request* Request::Factory(Channel::UnixStreamSocket* channel, Json::Value& json)
Request::Request(Channel::UnixStreamSocket* channel, Json::Value& json) :
channel(channel)
{
MS_TRACE();
unsigned int id;
std::string method;
MethodId methodId;
Json::Value data;
// Check fields.
if (json["id"].isUInt())
{
id = json["id"].asUInt();
}
else
{
MS_ERROR("json has no numeric .id field");
return nullptr;
}
MS_THROW_ERROR("json has no numeric .id field");
if (json["method"].isString())
{
method = json["method"].asString();
}
else
{
MS_ERROR("json has no string .method field");
return nullptr;
}
MS_THROW_ERROR("json has no string .method field");
auto it = Request::string2MethodId.find(method);
if (it != Request::string2MethodId.end())
{
methodId = it->second;
}
this->methodId = it->second;
else
{
MS_ERROR("unknwon .method '%s'", method.c_str());
return nullptr;
}
MS_THROW_ERROR("unknwon .method '%s'", method.c_str());
if (json["data"].isObject())
data = json["data"];
this->data = json["data"];
else
data = Json::Value(Json::objectValue);
return new Request(channel, id, method, methodId, data);
}
/* Instance methods. */
Request::Request(Channel::UnixStreamSocket* channel, unsigned int id, std::string& method, MethodId methodId, Json::Value& data) :
channel(channel),
id(id),
method(method),
methodId(methodId),
data(data)
{
MS_TRACE();
this->data = Json::Value(Json::objectValue);
}
Request::~Request()
......
......@@ -158,7 +158,16 @@ namespace Channel
if (reader.parse((const char*)json_start, (const char*)json_start + json_len, json))
{
Channel::Request* request = Request::Factory(this, json);
Channel::Request* request = nullptr;
try
{
request = new Channel::Request(this, json);
}
catch (const MediaSoupError &error)
{
MS_ERROR("discarding wrong Channel request");
}
if (request)
{
......@@ -168,10 +177,6 @@ namespace Channel
// Delete the Request.
delete request;
}
else
{
MS_ERROR("discarding wrong Channel request");
}
}
else
{
......
......@@ -42,7 +42,6 @@ namespace RTC
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
......@@ -51,7 +50,6 @@ namespace RTC
MS_ERROR("Peer already exists");
request->Reject(500, "Peer already exists");
return;
}
......
#!/usr/bin/env bash
ROOT_DIR="../../"
RAGEL_FILE="src/Utils/IP.rl"
RAGEL_COMPILE_TYPE="-G2"
PRODUCTION=false
set -e
which ragel >/dev/null
if [ $? -ne 0 ] ; then
echo ">>> ERROR: ragel binary not found, cannot compile the Ragel grammar" >&2
......@@ -18,10 +15,8 @@ else
ragel -v
fi
cd $ROOT_DIR
cpp_file="${RAGEL_FILE%.rl}.cpp"
echo ">>> INFO: compiling $RAGEL_FILE:"
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论