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

Improve syntax

上级 7d62fea1
......@@ -6,8 +6,9 @@ const netstring = require('netstring');
const logger = require('../logger')('Channel');
const Request = require('./Request');
// netstring length for a 65536 bytes payload
const MESSAGE_MAX_SIZE = 65543;
// Max time waiting for a response from the worker subprocess
const MESSAGE_MAX_SIZE = 65536;
const REQUEST_TIMEOUT = 5000;
class Channel extends EventEmitter
......
......@@ -26,12 +26,12 @@ namespace Channel
void Reject(unsigned int status, const char* reason = nullptr);
public:
// Passed by argument:
// Passed by argument.
Channel::UnixStreamSocket* channel = nullptr;
unsigned int id;
std::string method;
Json::Value data;
// Others:
// Others.
bool replied = false;
};
}
......
......@@ -36,9 +36,9 @@ namespace Channel
virtual void userOnUnixStreamSocketClosed(bool is_closed_by_peer) override;
private:
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
// Others:
// Others.
size_t msgStart = 0; // Where the latest message starts.
};
}
......
......@@ -5,6 +5,8 @@
#include "handles/SignalsHandler.h"
#include "Channel/UnixStreamSocket.h"
#include "Channel/Request.h"
#include "RTC/Room.h"
#include <unordered_map>
class Loop :
public SignalsHandler::Listener,
......@@ -14,7 +16,7 @@ public:
Loop();
~Loop();
// TODO: IMHO this can be private.
private:
void Close();
/* Methods inherited from SignalsHandler::Listener. */
......@@ -33,6 +35,8 @@ private:
Channel::UnixStreamSocket* channel = nullptr;
// Others.
bool closed = false;
typedef std::unordered_map<unsigned int, RTC::Room*> Rooms;
Rooms rooms;
};
#endif
......@@ -94,14 +94,14 @@ namespace RTC
virtual void onTimer(Timer* timer) override;
private:
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
// Allocated by this:
// Allocated by this.
SSL* ssl = nullptr;
BIO* sslBioFromNetwork = nullptr; // The BIO from which ssl reads.
BIO* sslBioFromNetwork = nullptr; // The BIO from which ssl reads.
BIO* sslBioToNetwork = nullptr; // The BIO in which ssl writes.
Timer* timer = nullptr;
// Others:
// Others.
RTC::DTLSRole role;
RTC::FingerprintHash remoteFingerprintHash = RTC::FingerprintHash::NONE;
std::string remoteFingerprint;
......@@ -140,6 +140,6 @@ namespace RTC
{
return this->isRunning;
}
} // namespace RTC
}
#endif
......@@ -12,6 +12,6 @@ namespace RTC
SHA384,
SHA512
};
} // namespace RTC
}
#endif
......@@ -35,10 +35,10 @@ namespace RTC
void Close();
private:
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
void* userData = nullptr;
// Others:
// Others.
std::string localUsername;
std::string localPassword;
};
......@@ -68,6 +68,6 @@ namespace RTC
{
return this->userData;
}
} // namespace RTC
}
#endif
......@@ -8,7 +8,7 @@
namespace RTC
{
class Peer : RTC::Transport::Listener
class Peer : public RTC::Transport::Listener
{
public:
class Listener
......@@ -34,10 +34,10 @@ namespace RTC
virtual void onRTCPPacket(RTC::Transport* transport, RTC::RTCPPacket* packet) override;
private:
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
void* userData = nullptr;
// Others:
// Others.
RTC::Transport* transport = nullptr;
};
......@@ -54,6 +54,6 @@ namespace RTC
{
return this->userData;
}
} // namespace RTC
}
#endif
......@@ -36,7 +36,7 @@ namespace RTC
size_t GetLength();
private:
// Passed by argument:
// Passed by argument.
CommonHeader* header = nullptr;
MS_BYTE* raw = nullptr;
size_t length = 0;
......@@ -75,6 +75,6 @@ namespace RTC
{
return this->length;
}
} // namespace RTC
}
#endif
......@@ -66,7 +66,7 @@ namespace RTC
void Dump();
private:
// Passed by argument:
// Passed by argument.
Header* header = nullptr;
MS_BYTE* csrcList = nullptr;
ExtensionHeader* extensionHeader = nullptr;
......@@ -75,7 +75,7 @@ namespace RTC
MS_BYTE payloadPadding = 0;
MS_BYTE* raw = nullptr; // Allocated when Serialize().
size_t length = 0;
// Others:
// Others.
bool isSerialized = false;
};
......@@ -195,6 +195,6 @@ namespace RTC
{
return this->payloadLength;
}
} // namespace RTC
}
#endif
......@@ -8,7 +8,7 @@
namespace RTC
{
class Room : RTC::Peer::Listener
class Room : public RTC::Peer::Listener
{
public:
Room();
......@@ -22,10 +22,10 @@ namespace RTC
virtual void onRTCPPacket(RTC::Peer* peer, RTC::RTCPPacket* packet) override;
private:
// Others:
// Others.
typedef std::vector<RTC::Peer*> Peers;
Peers peers;
};
} // namespace RTC
}
#endif
......@@ -9,6 +9,6 @@ namespace RTC
AES_CM_128_HMAC_SHA1_80 = 1,
AES_CM_128_HMAC_SHA1_32
};
} // namespace RTC
}
#endif
......@@ -38,7 +38,7 @@ namespace RTC
void Close();
private:
// Allocated by this:
// Allocated by this.
srtp_t session = nullptr;
// Others:
err_status_t lastError = (err_status_t)0;
......@@ -56,6 +56,6 @@ namespace RTC
return error_string;
}
} // namespace RTC
}
#endif
......@@ -93,13 +93,13 @@ namespace RTC
void Dump();
private:
// Passed by argument:
// Passed by argument.
Class klass; // 2 bytes.
Method method; // 2 bytes.
const MS_BYTE* transactionId = nullptr; // 12 bytes.
MS_BYTE* raw = nullptr; // Allocated when Serialize().
size_t length = 0; // The full message size (including header).
// Others:
// Others.
bool isSerialized = false;
// STUN attributes.
std::string username; // Less than 513 bytes.
......@@ -269,6 +269,6 @@ namespace RTC
{
return this->hasFingerprint;
}
} // namespace RTC
}
#endif
......@@ -20,6 +20,6 @@ namespace RTC
MS_4BYTES ssrc = 0;
std::unordered_set<MS_BYTE> payloads;
};
} // namespace RTC
}
#endif
......@@ -29,11 +29,11 @@ namespace RTC
virtual void userOnTCPConnectionRead() override;
private:
// Passed by argument:
// Passed by argument.
Reader* reader = nullptr;
// Others:
// Others.
size_t frameStart = 0; // Where the latest frame starts.
};
} // namespace RTC
}
#endif
......@@ -49,7 +49,7 @@ namespace RTC
virtual void userOnTCPServerClosed() override;
private:
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
RTC::TCPConnection::Reader* reader = nullptr;
};
......@@ -62,6 +62,6 @@ namespace RTC
this->listener = listener;
this->reader = reader;
}
} // namespace RTC
}
#endif
......@@ -141,17 +141,17 @@ namespace RTC
virtual void onDTLSApplicationData(RTC::DTLSHandler* dtlsHandler, const MS_BYTE* data, size_t len) override;
private:
// Allocated by this:
// Allocated by this.
RTC::ICEServer* iceServer = nullptr;
RTC::DTLSHandler* dtlsHandler = nullptr;
RTC::SRTPSession* srtpRecvSession = nullptr;
RTC::SRTPSession* srtpSendSession = nullptr;
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
std::vector<RTC::UDPSocket*> udpSockets;
std::vector<RTC::TCPServer*> tcpServers;
void* userData = nullptr;
// Others:
// Others.
int flags = 0;
std::list<RTC::TransportSource> sources;
RTC::TransportSource* sendingSource = nullptr;
......@@ -326,6 +326,6 @@ namespace RTC
return false;
}
} // namespace RTC
}
#endif
......@@ -32,12 +32,12 @@ namespace RTC
void Dump();
private:
// Passed by argument:
// Passed by argument.
RTC::UDPSocket* udpSocket = nullptr;
struct sockaddr* udpRemoteAddr = nullptr;
sockaddr_storage udpRemoteAddrStorage;
RTC::TCPConnection* tcpConnection = nullptr;
// Others:
// Others.
Type type;
};
......@@ -118,6 +118,6 @@ namespace RTC
if (this->type == Type::TCP)
this->tcpConnection->Close();
}
} // namespace RTC
}
#endif
......@@ -49,7 +49,7 @@ namespace RTC
virtual void userOnUDPSocketClosed() override;
private:
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
};
......@@ -60,6 +60,6 @@ namespace RTC
{
this->listener = listener;
}
} // namespace RTC
}
#endif
......@@ -26,10 +26,10 @@ public:
void onUvSignal(int signum);
private:
// Allocated by this:
// Allocated by this.
typedef std::vector<uv_signal_t*> UvHandles;
UvHandles uvHandles;
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
};
......
......@@ -70,9 +70,9 @@ private:
bool hasError = false;
protected:
// Allocated by this:
// Allocated by this.
MS_BYTE* buffer = nullptr;
// Passed by argument:
// Passed by argument.
size_t bufferSize = 0;
// Others.
size_t bufferDataLen = 0;
......
......@@ -47,11 +47,11 @@ public:
virtual void onTCPConnectionClosed(TCPConnection* connection, bool is_closed_by_peer);
private:
// Allocated by this (may be passed by argument):
// Allocated by this (may be passed by argument).
uv_tcp_t* uvHandle = nullptr;
// Passed by argument:
// Passed by argument.
void* userData = nullptr;
// Others:
// Others.
typedef std::unordered_set<TCPConnection*> TCPConnections;
TCPConnections connections;
bool isClosing = false;
......
......@@ -25,9 +25,9 @@ public:
void onUvTimer();
private:
// Allocated by this:
// Allocated by this.
uv_timer_t* uvHandle = nullptr;
// Passed by argument:
// Passed by argument.
Listener* listener = nullptr;
};
......
......@@ -55,11 +55,11 @@ protected:
virtual void userOnUDPSocketClosed() = 0;
private:
// Allocated by this (may be passed by argument):
// Allocated by this (may be passed by argument).
uv_udp_t* uvHandle = nullptr;
// Passed by argument:
// Passed by argument.
void* userData = nullptr;
// Others:
// Others.
bool isClosing = false;
protected:
......
......@@ -51,11 +51,11 @@ private:
bool hasError = false;
protected:
// Allocated by this:
// Allocated by this.
MS_BYTE* buffer = nullptr;
// Passed by argument:
// Passed by argument.
size_t bufferSize = 0;
// Others:
// Others.
size_t bufferDataLen = 0;
};
......
......@@ -8,7 +8,8 @@
#include <cmath> // std::ceil()
#include <cstdio> // sprintf()
#define MESSAGE_MAX_SIZE 65536
// netstring length for a 65536 bytes payload
#define MESSAGE_MAX_SIZE 65543
namespace Channel
{
......@@ -23,6 +24,8 @@ namespace Channel
listener(listener)
{
MS_TRACE();
MS_ERROR("---- MAX NS: %lu", netstring_buffer_size(MESSAGE_MAX_SIZE));
}
UnixStreamSocket::~UnixStreamSocket()
......@@ -115,23 +118,23 @@ namespace Channel
return;
case NETSTRING_ERROR_TOO_LONG:
MS_THROW_ERROR("NETSTRING_ERROR_TOO_LONG");
MS_ERROR("NETSTRING_ERROR_TOO_LONG");
break;
case NETSTRING_ERROR_NO_COLON:
MS_THROW_ERROR("NETSTRING_ERROR_NO_COLON");
MS_ERROR("NETSTRING_ERROR_NO_COLON");
break;
case NETSTRING_ERROR_NO_COMMA:
MS_THROW_ERROR("NETSTRING_ERROR_NO_COMMA");
MS_ERROR("NETSTRING_ERROR_NO_COMMA");
break;
case NETSTRING_ERROR_LEADING_ZERO:
MS_THROW_ERROR("NETSTRING_ERROR_LEADING_ZERO");
MS_ERROR("NETSTRING_ERROR_LEADING_ZERO");
break;
case NETSTRING_ERROR_NO_LENGTH:
MS_THROW_ERROR("NETSTRING_ERROR_NO_LENGTH");
MS_ERROR("NETSTRING_ERROR_NO_LENGTH");
break;
}
......@@ -149,25 +152,26 @@ namespace Channel
Json::Value json;
Json::Reader reader;
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))
{
MS_THROW_ERROR("invalid JSON");
}
Channel::Request* request = Request::Factory(this, json);
Channel::Request* request = Request::Factory(this, json);
if (request)
{
// Notify the listener.
this->listener->onChannelRequest(this, request);
if (request)
{
// Notify the listener.
this->listener->onChannelRequest(this, request);
// Delete the Request.
delete request;
// Delete the Request.
delete request;
}
else
{
MS_ERROR("discarding wrong Channel request");
}
}
else
{
// TODO: throw?
MS_ERROR("discarding wrong Channel request");
MS_ERROR("invalid JSON");
}
// If there is no more space available in the buffer and that is because
......
......@@ -60,8 +60,16 @@ void Loop::Close()
// Close the SignalsHandler.
this->signalsHandler->Close();
// Close the Channel socket
// Close the Channel socket.
this->channel->Close();
// Close all the Rooms.
for (auto& kv : this->rooms)
{
RTC::Room* room = kv.second;
room->Close();
}
}
void Loop::onSignal(SignalsHandler* signalsHandler, int signum)
......
......@@ -185,4 +185,4 @@ namespace RTC
delete this;
}
} // namespace RTC
}
......@@ -36,4 +36,4 @@ namespace RTC
{
MS_TRACE();
}
} // namespace RTC
}
......@@ -233,4 +233,4 @@ namespace RTC
MS_DEBUG("- Payload size: %zu bytes", GetPayloadLength());
MS_DEBUG("[/RTPPacket]");
}
} // namespace RTC
}
......@@ -20,17 +20,16 @@ namespace RTC
MS_TRACE();
// TMP
// for (int i = 1; i <= NUM_PEERS; i++)
// {
// RTC::Peer* peer = new RTC::Peer(this);
for (int i = 1; i <= NUM_PEERS; i++)
{
RTC::Peer* peer = new RTC::Peer(this);
// TODO: this is not being freed
int* data = new int(i);
peer->SetUserData((void*)data);
// // TODO: this is not being freed
// int* data = new int(i);
// peer->SetUserData((void*)data);
this->peers.push_back(peer);
}
// this->peers.push_back(peer);
// }
}
Room::~Room()
......@@ -42,6 +41,7 @@ namespace RTC
{
MS_TRACE();
// Close all the Peers.
for (auto peer : this->peers)
peer->Close();
......@@ -114,4 +114,4 @@ namespace RTC
// dst_peer->SendRTCPPacket(packet);
// }
}
} // namespace RTC
}
......@@ -213,4 +213,4 @@ namespace RTC
delete this;
}
} // namespace RTC
}
......@@ -629,4 +629,4 @@ namespace RTC
MS_DEBUG("- has Fingerprint");
MS_DEBUG("[/STUNMessage]");
}
} // namespace RTC
}
......@@ -21,7 +21,6 @@ namespace RTC
{
MS_TRACE();
// TODO: sure I want this?
delete this;
}
} // namespace RTC
}
......@@ -163,7 +163,7 @@ namespace RTC
// Exit the parsing loop.
break;
}
} // while()
}
}
void TCPConnection::Send(const MS_BYTE* data, size_t len)
......@@ -178,4 +178,4 @@ namespace RTC
Write(frame_len, 2, data, len);
}
} // namespace RTC
}
......@@ -365,4 +365,4 @@ namespace RTC
else if (this->localAddr.ss_family == AF_INET6)
RTC::TCPServer::availableIPv6Ports[this->localPort] = true;
}
} // namespace RTC
}
......@@ -658,4 +658,4 @@ namespace RTC
// TMP
dtlsHandler->SendApplicationData(data, len);
}
} // namespace RTC
}
......@@ -39,4 +39,4 @@ namespace RTC
}
}
}
} // namespace RTC
}
......@@ -386,4 +386,4 @@ namespace RTC
else if (this->localAddr.ss_family == AF_INET6)
RTC::UDPSocket::availableIPv6Ports[this->localPort] = true;
}
} // namespace RTC
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论