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

Worker: improve code

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