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

Cleanup and optimize JSON usage.

上级 9bc4d37a
......@@ -38,7 +38,10 @@ class Peer extends EventEmitter
*/
close(error, dontSendChannel)
{
logger.debug('close() [error:%s]', error);
if (!error)
logger.debug('close()');
else
logger.error('close() [error:%s]', error);
if (this._closed)
return;
......
......@@ -35,7 +35,10 @@ class Room extends EventEmitter
*/
close(error, dontSendChannel)
{
logger.debug('close() [error:%s]', error);
if (!error)
logger.debug('close()');
else
logger.error('close() [error:%s]', error);
if (this._closed)
return;
......
......@@ -114,7 +114,10 @@ class Server extends EventEmitter
*/
close(error)
{
logger.debug('close() [error:%s]', error);
if (!error)
logger.debug('close()');
else
logger.error('close() [error:%s]', error);
if (this._closed)
return;
......
......@@ -40,7 +40,10 @@ class Transport extends EventEmitter
*/
close(error, dontSendChannel)
{
logger.debug('close() [error:%s]', error);
if (!error)
logger.debug('close()');
else
logger.error('close() [error:%s]', error);
if (this._closed)
return;
......
......@@ -88,7 +88,10 @@ class Worker extends EventEmitter
close(error)
{
logger.debug('close() [error:%s]', error);
if (!error)
logger.debug('close()');
else
logger.error('close() [error:%s]', error);
// Close every Room
this._rooms.forEach((room) => room.close(undefined, true));
......
......@@ -8,6 +8,7 @@ class DepLibUV
public:
static void ClassInit();
static void ClassDestroy();
static void PrintVersion();
static void RunLoop();
static uv_loop_t* GetLoop();
......
......@@ -10,7 +10,8 @@
class Loop :
public SignalsHandler::Listener,
public Channel::UnixStreamSocket::Listener
public Channel::UnixStreamSocket::Listener,
public RTC::Room::Listener
{
public:
Loop(Channel::UnixStreamSocket* channel);
......@@ -30,6 +31,10 @@ public:
virtual void onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request* request) override;
virtual void onChannelUnixStreamSocketRemotelyClosed(Channel::UnixStreamSocket* channel) override;
/* Methods inherited from RTC::Room::Listener. */
public:
virtual void onRoomClosed(RTC::Room* room) override;
private:
// Passed by argument.
Channel::UnixStreamSocket* channel = nullptr;
......
......@@ -32,7 +32,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::Transport::Listener. */
public:
// TODO
virtual void onTransportClosed(RTC::Transport* transport) override;
public:
// Passed by argument.
......
......@@ -9,10 +9,18 @@
namespace RTC
{
class Room : public RTC::Peer::Listener
class Room :
public RTC::Peer::Listener
{
public:
Room(unsigned int roomId, Json::Value& data);
class Listener
{
public:
virtual void onRoomClosed(RTC::Room* room) = 0;
};
public:
Room(Listener* listener, unsigned int roomId, Json::Value& data);
virtual ~Room();
void Close();
......@@ -31,6 +39,8 @@ namespace RTC
unsigned int roomId;
private:
// Passed by argument.
Listener* listener = nullptr;
// Others.
typedef std::unordered_map<std::string, RTC::Peer*> Peers;
Peers peers;
......
......@@ -25,7 +25,7 @@ namespace RTC
class Listener
{
public:
// TODO
virtual void onTransportClosed(RTC::Transport* transport) = 0;
};
public:
......@@ -35,18 +35,12 @@ namespace RTC
RTCP = 2
};
public:
typedef struct IceParameters
{
std::string usernameFragment;
std::string password;
} IceParameters;
public:
Transport(Listener* listener, unsigned int transportId, Json::Value& data, Transport* rtpTransport = nullptr);
virtual ~Transport();
void Close();
Json::Value Dump();
Json::Value toJson();
std::string& GetIceUsernameFragment();
std::string& GetIcePassword();
......@@ -88,17 +82,19 @@ namespace RTC
bool hasIPv4tcp = false;
bool hasIPv6tcp = false;
public:
// Passed by argument.
unsigned int transportId;
private:
// Passed by argument.
Listener* listener = nullptr;
unsigned int transportId;
RTC::Transport::IceComponent iceComponent;
// Allocated by this.
RTC::IceServer* iceServer = nullptr;
std::vector<RTC::UDPSocket*> udpSockets;
std::vector<RTC::TCPServer*> tcpServers;
// Others.
IceParameters iceLocalParameters;
std::vector<IceCandidate> iceLocalCandidates;
bool allocated = false;
};
......
......@@ -32,17 +32,21 @@ namespace Channel
{
MS_TRACE();
if (json["id"].isUInt())
this->id = json["id"].asUInt();
static const Json::StaticString k_id("id");
static const Json::StaticString k_method("method");
static const Json::StaticString k_data("data");
if (json[k_id].isUInt())
this->id = json[k_id].asUInt();
else
MS_THROW_ERROR("json has no numeric .id field");
if (json["method"].isString())
this->method = json["method"].asString();
if (json[k_method].isString())
this->method = json[k_method].asString();
else
MS_THROW_ERROR("json has no string .method field");
auto it = Request::string2MethodId.find(method);
auto it = Request::string2MethodId.find(this->method);
if (it != Request::string2MethodId.end())
{
......@@ -52,11 +56,11 @@ namespace Channel
{
Reject(405, "method not allowed");
MS_THROW_ERROR("unknown .method '%s'", method.c_str());
MS_THROW_ERROR("unknown .method '%s'", this->method.c_str());
}
if (json["data"].isObject())
this->data = json["data"];
if (json[k_data].isObject())
this->data = json[k_data];
else
this->data = Json::Value(Json::objectValue);
}
......@@ -80,15 +84,19 @@ namespace Channel
MS_ASSERT(!this->replied, "Request already replied");
this->replied = true;
static const Json::StaticString k_id("id");
static const Json::StaticString k_status("status");
static const Json::StaticString k_data("data");
Json::Value json;
json["id"] = this->id;
json["status"] = 200;
json[k_id] = this->id;
json[k_status] = 200;
if (data.isObject())
json["data"] = data;
json[k_data] = data;
else
json["data"] = empty_data;
json[k_data] = empty_data;
this->channel->Send(json);
}
......@@ -114,13 +122,17 @@ namespace Channel
MS_ASSERT(status >= 400 && status <= 599, "status must be between 400 and 500");
static const Json::StaticString k_id("id");
static const Json::StaticString k_status("status");
static const Json::StaticString k_reason("reason");
Json::Value json;
json["id"] = this->id;
json["status"] = status;
json[k_id] = this->id;
json[k_status] = status;
if (reason)
json["reason"] = reason;
json[k_reason] = reason;
this->channel->Send(json);
}
......
......@@ -34,6 +34,13 @@ void DepLibUV::ClassDestroy()
delete DepLibUV::loop;
}
void DepLibUV::PrintVersion()
{
MS_TRACE();
MS_DEBUG("loaded libuv version: %s", uv_version_string());
}
void DepLibUV::RunLoop()
{
MS_TRACE();
......
......@@ -43,7 +43,9 @@ RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, unsigned int* roo
{
MS_TRACE();
auto jsonRoomId = request->data["roomId"];
static const Json::StaticString k_roomId("roomId");
auto jsonRoomId = request->data[k_roomId];
if (!jsonRoomId.isUInt())
MS_THROW_ERROR("Request has not numeric .roomId field");
......@@ -92,10 +94,14 @@ void Loop::Close()
this->signalsHandler->Close();
// Close all the Rooms.
for (auto& kv : this->rooms)
// NOTE: Upon Room closure the onRoomClosed() method is called which
// removes it from the map, so this is the safe way to iterate the mao
// and remove elements.
for (auto it = this->rooms.begin(); it != this->rooms.end();)
{
RTC::Room* room = kv.second;
RTC::Room* room = it->second;
it = this->rooms.erase(it);
room->Close();
}
......@@ -142,19 +148,20 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
{
case Channel::Request::MethodId::dumpWorker:
{
static const Json::StaticString k_rooms("rooms");
Json::Value json(Json::objectValue);
Json::Value jsonWorker(Json::objectValue);
Json::Value jsonRooms(Json::objectValue);
Json::Value json_worker(Json::objectValue);
Json::Value json_rooms(Json::objectValue);
for (auto& kv : this->rooms)
{
auto room = kv.second;
jsonRooms[std::to_string(room->roomId)] = room->Dump();
json_rooms[std::to_string(room->roomId)] = room->Dump();
}
jsonWorker["rooms"] = jsonRooms;
json[Logger::id] = jsonWorker;
json_worker[k_rooms] = json_rooms;
json[Logger::id] = json_worker;
request->Accept(json);
......@@ -192,7 +199,7 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
try
{
room = new RTC::Room(roomId, request->data);
room = new RTC::Room(this, roomId, request->data);
}
catch (const MediaSoupError &error)
{
......@@ -233,9 +240,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
room->Close();
// TODO: Instead of this, the Room should fire an onRoomClosed() here.
this->rooms.erase(roomId);
MS_DEBUG("Room closed [roomId:%u]", roomId);
request->Accept();
......@@ -295,3 +299,10 @@ void Loop::onChannelUnixStreamSocketRemotelyClosed(Channel::UnixStreamSocket* so
Close();
}
void Loop::onRoomClosed(RTC::Room* room)
{
MS_TRACE();
this->rooms.erase(room->roomId);
}
......@@ -27,31 +27,43 @@ namespace RTC
Json::Value IceCandidate::toJson()
{
static const Json::StaticString k_foundation("foundation");
static const Json::StaticString k_priority("priority");
static const Json::StaticString k_ip("ip");
static const Json::StaticString k_port("port");
static const Json::StaticString k_type("type");
static const Json::StaticString v_host("host");
static const Json::StaticString k_protocol("protocol");
static const Json::StaticString v_udp("udp");
static const Json::StaticString v_tcp("tcp");
static const Json::StaticString k_tcpType("tcpType");
static const Json::StaticString v_passive("passive");
Json::Value data;
data["foundation"] = this->foundation;
data["priority"] = (unsigned int)this->priority;
data["ip"] = this->ip;
data["port"] = this->port;
data[k_foundation] = this->foundation;
data[k_priority] = (unsigned int)this->priority;
data[k_ip] = this->ip;
data[k_port] = this->port;
switch (this->type)
{
case CandidateType::HOST:
data["type"] = "host";
data[k_type] = v_host;
break;
}
switch (this->protocol)
{
case Protocol::UDP:
data["protocol"] = "udp";
data[k_protocol] = v_udp;
break;
case Protocol::TCP:
data["protocol"] = "tcp";
data[k_protocol] = v_tcp;
switch (this->tcpType)
{
case TcpCandidateType::PASSIVE:
data["tcpType"] = "passive";
data[k_tcpType] = v_passive;
break;
}
break;
......
......@@ -28,10 +28,14 @@ namespace RTC
MS_TRACE();
// Close all the Transports.
for (auto& kv : this->transports)
// NOTE: Upon Transport closure the onTransportClosed() method is called which
// removes it from the map, so this is the safe way to iterate the mao
// and remove elements.
for (auto it = this->transports.begin(); it != this->transports.end();)
{
RTC::Transport* transport = kv.second;
RTC::Transport* transport = it->second;
it = this->transports.erase(it);
transport->Close();
}
......@@ -45,9 +49,18 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_transports("transports");
Json::Value json(Json::objectValue);
Json::Value json_transports(Json::objectValue);
// TODO
for (auto& kv : this->transports)
{
RTC::Transport* transport = kv.second;
json_transports[std::to_string(transport->transportId)] = transport->Dump();
}
json[k_transports] = json_transports;
return json;
}
......@@ -125,7 +138,9 @@ namespace RTC
return;
}
auto jsonRtpTransportId = request->data["rtpTransportId"];
static const Json::StaticString k_rtpTransportId("rtpTransportId");
auto jsonRtpTransportId = request->data[k_rtpTransportId];
if (!jsonRtpTransportId.isUInt())
{
......@@ -170,8 +185,30 @@ namespace RTC
case Channel::Request::MethodId::closeTransport:
{
// TODO
RTC::Transport* transport;
unsigned int transportId;
try
{
transport = GetTransportFromRequest(request, &transportId);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!transport)
{
MS_ERROR("Transport does not exist");
request->Reject(500, "Transport does not exist");
return;
}
transport->Close();
MS_DEBUG("Transport closed [transportId:%u]", transportId);
request->Accept();
break;
......@@ -188,7 +225,9 @@ namespace RTC
{
MS_TRACE();
auto jsonTransportId = request->data["transportId"];
static const Json::StaticString k_transportId("transportId");
auto jsonTransportId = request->data[k_transportId];
if (!jsonTransportId.isUInt())
MS_THROW_ERROR("Request has not numeric .transportId field");
......@@ -210,4 +249,11 @@ namespace RTC
return nullptr;
}
}
void Peer::onTransportClosed(RTC::Transport* transport)
{
MS_TRACE();
this->transports.erase(transport->transportId);
}
}
......@@ -8,8 +8,9 @@ namespace RTC
{
/* Instance methods. */
Room::Room(unsigned int roomId, Json::Value& data) :
roomId(roomId)
Room::Room(Listener* listener, unsigned int roomId, Json::Value& data) :
roomId(roomId),
listener(listener)
{
MS_TRACE();
......@@ -31,12 +32,15 @@ namespace RTC
// and remove elements.
for (auto it = this->peers.begin(); it != this->peers.end();)
{
auto peer = it->second;
RTC::Peer* peer = it->second;
it = this->peers.erase(it);
peer->Close();
}
// Notify the listener.
this->listener->onRoomClosed(this);
delete this;
}
......@@ -44,17 +48,18 @@ namespace RTC
{
MS_TRACE();
static const Json::StaticString k_peers("peers");
Json::Value json(Json::objectValue);
Json::Value jsonPeers(Json::objectValue);
Json::Value json_peers(Json::objectValue);
for (auto& kv : this->peers)
{
auto peer = kv.second;
RTC::Peer* peer = kv.second;
jsonPeers[peer->peerId] = peer->Dump();
json_peers[peer->peerId] = peer->Dump();
}
json["peers"] = jsonPeers;
json[k_peers] = json_peers;
return json;
}
......@@ -175,7 +180,9 @@ namespace RTC
{
MS_TRACE();
auto jsonPeerId = request->data["peerId"];
static const Json::StaticString k_peerId("peerId");
auto jsonPeerId = request->data[k_peerId];
if (!jsonPeerId.isString())
MS_THROW_ERROR("Request has not string .peerId field");
......
......@@ -21,13 +21,13 @@ namespace RTC
/* Instance methods. */
Transport::Transport(Listener* listener, unsigned int transportId, Json::Value& data, Transport* rtpTransport) :
listener(listener),
transportId(transportId)
transportId(transportId),
listener(listener)
{
MS_TRACE();
static const Json::StaticString udp("udp");
static const Json::StaticString tcp("tcp");
static const Json::StaticString k_udp("udp");
static const Json::StaticString k_tcp("tcp");
bool try_IPv4_udp = false;
bool try_IPv6_udp = false;
......@@ -43,11 +43,11 @@ namespace RTC
Utils::Crypto::GetRandomString(16),
Utils::Crypto::GetRandomString(32));
if (data[udp].isBool())
try_IPv4_udp = try_IPv6_udp = data[udp].asBool();
if (data[k_udp].isBool())
try_IPv4_udp = try_IPv6_udp = data[k_udp].asBool();
if (data[tcp].isBool())
try_IPv4_tcp = try_IPv6_tcp = data[tcp].asBool();
if (data[k_tcp].isBool())
try_IPv4_tcp = try_IPv6_tcp = data[k_tcp].asBool();
}
// RTCP transport associated to a given RTP transport.
else
......@@ -64,10 +64,6 @@ namespace RTC
try_IPv6_tcp = rtpTransport->hasIPv6tcp;
}
// Fill IceParameters.
this->iceLocalParameters.usernameFragment = this->iceServer->GetUsernameFragment();
this->iceLocalParameters.password = this->iceServer->GetPassword();
// Open a IPv4 UDP socket.
if (try_IPv4_udp && Settings::configuration.hasIPv4)
{
......@@ -208,32 +204,48 @@ namespace RTC
server->Close();
if (this->allocated)
{
// Notify the listener.
this->listener->onTransportClosed(this);
delete this;
}
}
Json::Value Transport::toJson()
Json::Value Transport::Dump()
{
MS_TRACE();
return toJson();
}
Json::Value Transport::toJson()
{
MS_TRACE();
static const Json::StaticString iceLocalParameters("iceLocalParameters");
static const Json::StaticString iceLocalCandidates("iceLocalCandidates");
static const Json::StaticString k_iceComponent("iceComponent");
static const Json::StaticString k_iceLocalParameters("iceLocalParameters");
static const Json::StaticString k_usernameFragment("usernameFragment");
static const Json::StaticString k_password("password");
static const Json::StaticString k_iceLocalCandidates("iceLocalCandidates");
static const Json::StaticString v_RTP("RTP");
static const Json::StaticString v_RTCP("RTCP");
Json::Value data;
if (this->iceComponent == IceComponent::RTP)
data["iceComponent"] = "RTP";
data[k_iceComponent] = v_RTP;
else
data["iceComponent"] = "RTCP";
data[k_iceComponent] = v_RTCP;
data[iceLocalParameters]["usernameFragment"] = this->iceLocalParameters.usernameFragment;
data[iceLocalParameters]["password"] = this->iceLocalParameters.password;
data[k_iceLocalParameters][k_usernameFragment] = this->iceServer->GetUsernameFragment();
data[k_iceLocalParameters][k_password] = this->iceServer->GetPassword();
data[iceLocalCandidates] = Json::arrayValue;
data[k_iceLocalCandidates] = Json::arrayValue;
for (auto iceCandidate : this->iceLocalCandidates)
{
data[iceLocalCandidates].append(iceCandidate.toJson());
data[k_iceLocalCandidates].append(iceCandidate.toJson());
}
return data;
......
......@@ -49,8 +49,6 @@ void SignalsHandler::AddSignal(int signum, std::string name)
// Enter the UV handle into the vector.
this->uvHandles.push_back(uvHandle);
MS_DEBUG("signal %s added", name.c_str());
}
void SignalsHandler::Close()
......
......@@ -41,9 +41,6 @@ int main(int argc, char* argv[], char** envp)
Logger::Init(id, channel);
// Print libuv version.
MS_DEBUG("loaded libuv version: %s", uv_version_string());
try
{
Settings::SetConfiguration(argc, argv);
......@@ -100,6 +97,8 @@ void init()
ignoreSignals();
DepLibUV::PrintVersion();
// Initialize static stuff.
DepOpenSSL::ClassInit();
DepLibSRTP::ClassInit();
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论