提交 abdc9ff0 authored 作者: José Luis Millán's avatar José Luis Millán

UdpSocket/TcpConnection: Add RecvBytes() and SentBytes() methods

上级 744ad61b
......@@ -23,6 +23,8 @@ namespace RTC
public:
void Send(const uint8_t* data, size_t len);
size_t GetRecvBytes() const;
size_t GetSentBytes() const;
/* Pure virtual methods inherited from ::TcpConnection. */
public:
......@@ -33,7 +35,19 @@ namespace RTC
Listener* listener{ nullptr };
// Others.
size_t frameStart{ 0 }; // Where the latest frame starts.
size_t recvBytes{ 0 };
size_t sentBytes{ 0 };
};
inline size_t TcpConnection::GetRecvBytes() const
{
return this->recvBytes;
}
inline size_t TcpConnection::GetSentBytes() const
{
return this->sentBytes;
}
} // namespace RTC
#endif
......@@ -30,6 +30,8 @@ namespace RTC
Protocol GetProtocol() const;
const struct sockaddr* GetLocalAddress() const;
const struct sockaddr* GetRemoteAddress() const;
size_t GetRecvBytes() const;
size_t GetSentBytes() const;
private:
// Passed by argument.
......@@ -107,6 +109,22 @@ namespace RTC
else
return this->tcpConnection->GetPeerAddress();
}
inline size_t TransportTuple::GetRecvBytes() const
{
if (this->protocol == Protocol::UDP)
return this->udpSocket->GetRecvBytes();
else
return this->tcpConnection->GetRecvBytes();
}
inline size_t TransportTuple::GetSentBytes() const
{
if (this->protocol == Protocol::UDP)
return this->udpSocket->GetSentBytes();
else
return this->tcpConnection->GetSentBytes();
}
} // namespace RTC
#endif
......@@ -39,6 +39,8 @@ public:
int GetLocalFamily() const;
const std::string& GetLocalIP() const;
uint16_t GetLocalPort() const;
size_t GetRecvBytes() const;
size_t GetSentBytes() const;
private:
bool SetLocalAddress();
......@@ -60,6 +62,8 @@ private:
uv_udp_t* uvHandle{ nullptr };
// Others.
bool isClosing{ false };
size_t recvBytes{ 0 };
size_t sentBytes{ 0 };
protected:
struct sockaddr_storage localAddr
......@@ -101,4 +105,14 @@ inline uint16_t UdpSocket::GetLocalPort() const
return this->localPort;
}
inline size_t UdpSocket::GetRecvBytes() const
{
return this->recvBytes;
}
inline size_t UdpSocket::GetSentBytes() const
{
return this->sentBytes;
}
#endif
......@@ -63,9 +63,12 @@ namespace RTC
{
const uint8_t* packet = this->buffer + this->frameStart + 2;
// Notify the listener.
// Update received bytes and notify the listener.
if (packetLen != 0)
{
this->recvBytes += packetLen;
this->listener->OnPacketRecv(this, packet, packetLen);
}
// If there is no more space available in the buffer and that is because
// the latest parsed frame filled it, then empty the full buffer.
......@@ -140,6 +143,9 @@ namespace RTC
{
MS_TRACE();
// Update sent bytes.
this->sentBytes += len;
// Write according to Framing RFC 4571.
uint8_t frameLen[2];
......
......@@ -21,6 +21,8 @@ namespace RTC
static const Json::StaticString JsonStringProtocol{ "protocol" };
static const Json::StaticString JsonStringUdp{ "udp" };
static const Json::StaticString JsonStringTcp{ "tcp" };
static const Json::StaticString JsonStringRecvBytes{ "recvBytes" };
static const Json::StaticString JsonStringSentBytes{ "sentBytes" };
Json::Value json(Json::objectValue);
int ipFamily;
......@@ -38,6 +40,8 @@ namespace RTC
Utils::IP::GetAddressInfo(this->GetRemoteAddress(), &ipFamily, ip, &port);
json[JsonStringRemoteIp] = ip;
json[JsonStringRemotePort] = Json::UInt{ port };
json[JsonStringRecvBytes] = Json::UInt64{ GetRecvBytes() };
json[JsonStringSentBytes] = Json::UInt64{ GetSentBytes() };
return json;
}
......
......@@ -204,12 +204,18 @@ void UdpSocket::Send(const uint8_t* data, size_t len, const struct sockaddr* add
// Entire datagram was sent. Done.
if (sent == static_cast<int>(len))
{
// Update sent bytes.
this->sentBytes += sent;
return;
}
if (sent >= 0)
{
MS_WARN_DEV("datagram truncated (just %d of %zu bytes were sent)", sent, len);
// Update sent bytes.
this->sentBytes += sent;
return;
}
// Error,
......@@ -243,6 +249,11 @@ void UdpSocket::Send(const uint8_t* data, size_t len, const struct sockaddr* add
// Delete the UvSendData struct (which includes the uv_req_t and the store char[]).
std::free(sendData);
}
else
{
// Update sent bytes.
this->sentBytes += len;
}
}
void UdpSocket::Send(const uint8_t* data, size_t len, const std::string& ip, uint16_t port)
......@@ -346,6 +357,9 @@ inline void UdpSocket::OnUvRecv(
// Data received.
if (nread > 0)
{
// Update received bytes.
this->recvBytes += nread;
// Notify the subclass.
UserOnUdpDatagramRecv(reinterpret_cast<uint8_t*>(buf->base), nread, addr);
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论