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