提交 5c2208d5 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Fix TCP leak (#325).

上级 2f434d7e
...@@ -18,6 +18,7 @@ namespace RTC ...@@ -18,6 +18,7 @@ namespace RTC
public: public:
TcpConnection(Listener* listener, size_t bufferSize); TcpConnection(Listener* listener, size_t bufferSize);
~TcpConnection() override;
public: public:
void Send(const uint8_t* data, size_t len); void Send(const uint8_t* data, size_t len);
......
...@@ -16,7 +16,7 @@ namespace RTC ...@@ -16,7 +16,7 @@ namespace RTC
{ {
public: public:
virtual void OnRtcTcpConnectionClosed( virtual void OnRtcTcpConnectionClosed(
RTC::TcpServer* tcpServer, RTC::TcpConnection* connection, bool isClosedByPeer) = 0; RTC::TcpServer* tcpServer, RTC::TcpConnection* connection) = 0;
}; };
public: public:
...@@ -26,8 +26,8 @@ namespace RTC ...@@ -26,8 +26,8 @@ namespace RTC
/* Pure virtual methods inherited from ::TcpServer. */ /* Pure virtual methods inherited from ::TcpServer. */
public: public:
void UserOnTcpConnectionAlloc(::TcpConnection** connection) override; void UserOnTcpConnectionAlloc(::TcpConnection** connection) override;
void UserOnNewTcpConnection(::TcpConnection* connection) override; bool UserOnNewTcpConnection(::TcpConnection* connection) override;
void UserOnTcpConnectionClosed(::TcpConnection* connection, bool isClosedByPeer) override; void UserOnTcpConnectionClosed(::TcpConnection* connection) override;
private: private:
// Passed by argument. // Passed by argument.
......
...@@ -77,8 +77,7 @@ namespace RTC ...@@ -77,8 +77,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::TcpServer::Listener. */ /* Pure virtual methods inherited from RTC::TcpServer::Listener. */
public: public:
void OnRtcTcpConnectionClosed( void OnRtcTcpConnectionClosed(RTC::TcpServer* tcpServer, RTC::TcpConnection* connection) override;
RTC::TcpServer* tcpServer, RTC::TcpConnection* connection, bool isClosedByPeer) override;
/* Pure virtual methods inherited from RTC::TcpConnection::Listener. */ /* Pure virtual methods inherited from RTC::TcpConnection::Listener. */
public: public:
......
...@@ -18,7 +18,7 @@ public: ...@@ -18,7 +18,7 @@ public:
virtual ~Listener() = default; virtual ~Listener() = default;
public: public:
virtual void OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) = 0; virtual void OnTcpConnectionClosed(TcpConnection* connection) = 0;
}; };
public: public:
...@@ -52,6 +52,7 @@ public: ...@@ -52,6 +52,7 @@ public:
void Write(const uint8_t* data, size_t len); void Write(const uint8_t* data, size_t len);
void Write(const uint8_t* data1, size_t len1, const uint8_t* data2, size_t len2); void Write(const uint8_t* data1, size_t len1, const uint8_t* data2, size_t len2);
void Write(const std::string& data); void Write(const std::string& data);
void ErrorReceiving();
const struct sockaddr* GetLocalAddress() const; const struct sockaddr* GetLocalAddress() const;
int GetLocalFamily() const; int GetLocalFamily() const;
const std::string& GetLocalIp() const; const std::string& GetLocalIp() const;
......
...@@ -31,8 +31,8 @@ private: ...@@ -31,8 +31,8 @@ private:
/* Pure virtual methods that must be implemented by the subclass. */ /* Pure virtual methods that must be implemented by the subclass. */
protected: protected:
virtual void UserOnTcpConnectionAlloc(TcpConnection** connection) = 0; virtual void UserOnTcpConnectionAlloc(TcpConnection** connection) = 0;
virtual void UserOnNewTcpConnection(TcpConnection* connection) = 0; virtual bool UserOnNewTcpConnection(TcpConnection* connection) = 0;
virtual void UserOnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) = 0; virtual void UserOnTcpConnectionClosed(TcpConnection* connection) = 0;
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
public: public:
...@@ -40,7 +40,7 @@ public: ...@@ -40,7 +40,7 @@ public:
/* Methods inherited from TcpConnection::Listener. */ /* Methods inherited from TcpConnection::Listener. */
public: public:
void OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) override; void OnTcpConnectionClosed(TcpConnection* connection) override;
protected: protected:
struct sockaddr_storage localAddr; struct sockaddr_storage localAddr;
......
...@@ -21,6 +21,11 @@ namespace RTC ...@@ -21,6 +21,11 @@ namespace RTC
MS_TRACE(); MS_TRACE();
} }
TcpConnection::~TcpConnection()
{
MS_TRACE();
}
void TcpConnection::UserOnTcpConnectionRead() void TcpConnection::UserOnTcpConnectionRead()
{ {
MS_TRACE(); MS_TRACE();
...@@ -135,7 +140,7 @@ namespace RTC ...@@ -135,7 +140,7 @@ namespace RTC
"connection"); "connection");
// Close the socket. // Close the socket.
Close(); ::TcpConnection::ErrorReceiving();
} }
} }
// The buffer is not full. // The buffer is not full.
...@@ -161,6 +166,6 @@ namespace RTC ...@@ -161,6 +166,6 @@ namespace RTC
uint8_t frameLen[2]; uint8_t frameLen[2];
Utils::Byte::Set2Bytes(frameLen, 0, len); Utils::Byte::Set2Bytes(frameLen, 0, len);
Write(frameLen, 2, data, len); ::TcpConnection::Write(frameLen, 2, data, len);
} }
} // namespace RTC } // namespace RTC
...@@ -37,20 +37,24 @@ namespace RTC ...@@ -37,20 +37,24 @@ namespace RTC
*connection = new RTC::TcpConnection(this->connListener, 65536); *connection = new RTC::TcpConnection(this->connListener, 65536);
} }
void TcpServer::UserOnNewTcpConnection(::TcpConnection* connection) bool TcpServer::UserOnNewTcpConnection(::TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
// Allow just MaxTcpConnectionsPerServer. if (GetNumConnections() >= MaxTcpConnectionsPerServer)
if (GetNumConnections() > MaxTcpConnectionsPerServer) {
delete connection; MS_ERROR("cannot handle more than %zu connections", MaxTcpConnectionsPerServer);
return false;
}
return true;
} }
void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection, bool isClosedByPeer) void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
this->listener->OnRtcTcpConnectionClosed( this->listener->OnRtcTcpConnectionClosed(this, static_cast<RTC::TcpConnection*>(connection));
this, static_cast<RTC::TcpConnection*>(connection), isClosedByPeer);
} }
} // namespace RTC } // namespace RTC
...@@ -1408,13 +1408,12 @@ namespace RTC ...@@ -1408,13 +1408,12 @@ namespace RTC
} }
inline void WebRtcTransport::OnRtcTcpConnectionClosed( inline void WebRtcTransport::OnRtcTcpConnectionClosed(
RTC::TcpServer* /*tcpServer*/, RTC::TcpConnection* connection, bool isClosedByPeer) RTC::TcpServer* /*tcpServer*/, RTC::TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
RTC::TransportTuple tuple(connection); RTC::TransportTuple tuple(connection);
if (isClosedByPeer)
this->iceServer->RemoveTuple(&tuple); this->iceServer->RemoveTuple(&tuple);
} }
......
...@@ -124,9 +124,6 @@ void TcpConnection::Close() ...@@ -124,9 +124,6 @@ void TcpConnection::Close()
{ {
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose)); uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
} }
// Notify the listener.
this->listener->OnTcpConnectionClosed(this, this->isClosedByPeer);
} }
void TcpConnection::Dump() const void TcpConnection::Dump() const
...@@ -221,6 +218,9 @@ void TcpConnection::Write(const uint8_t* data, size_t len) ...@@ -221,6 +218,9 @@ void TcpConnection::Write(const uint8_t* data, size_t len)
Close(); Close();
// Notify the listener.
this->listener->OnTcpConnectionClosed(this);
return; return;
} }
...@@ -288,6 +288,9 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data ...@@ -288,6 +288,9 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
Close(); Close();
// Notify the listener.
this->listener->OnTcpConnectionClosed(this);
return; return;
} }
...@@ -327,6 +330,15 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data ...@@ -327,6 +330,15 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
MS_ABORT("uv_write() failed: %s", uv_strerror(err)); MS_ABORT("uv_write() failed: %s", uv_strerror(err));
} }
void TcpConnection::ErrorReceiving()
{
MS_ERROR();
Close();
this->listener->OnTcpConnectionClosed(this);
}
bool TcpConnection::SetPeerAddress() bool TcpConnection::SetPeerAddress()
{ {
MS_TRACE(); MS_TRACE();
...@@ -406,6 +418,9 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/) ...@@ -406,6 +418,9 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
// Close server side of the connection. // Close server side of the connection.
Close(); Close();
// Notify the listener.
this->listener->OnTcpConnectionClosed(this);
} }
// Some error. // Some error.
else else
...@@ -416,6 +431,9 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/) ...@@ -416,6 +431,9 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
// Close server side of the connection. // Close server side of the connection.
Close(); Close();
// Notify the listener.
this->listener->OnTcpConnectionClosed(this);
} }
} }
...@@ -432,4 +450,7 @@ inline void TcpConnection::OnUvWriteError(int error) ...@@ -432,4 +450,7 @@ inline void TcpConnection::OnUvWriteError(int error)
MS_WARN_DEV("write error, closing the connection: %s", uv_strerror(error)); MS_WARN_DEV("write error, closing the connection: %s", uv_strerror(error));
Close(); Close();
// Notify the listener.
this->listener->OnTcpConnectionClosed(this);
} }
...@@ -77,11 +77,8 @@ void TcpServer::Close() ...@@ -77,11 +77,8 @@ void TcpServer::Close()
MS_DEBUG_DEV("closing %zu active connections", this->connections.size()); MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
for (auto it = this->connections.begin(); it != this->connections.end();) for (auto* connection : this->connections)
{ {
auto* connection = *it;
it = this->connections.erase(it);
delete connection; delete connection;
} }
...@@ -166,38 +163,36 @@ inline void TcpServer::OnUvConnection(int status) ...@@ -166,38 +163,36 @@ inline void TcpServer::OnUvConnection(int status)
if (err != 0) if (err != 0)
MS_ABORT("uv_accept() failed: %s", uv_strerror(err)); MS_ABORT("uv_accept() failed: %s", uv_strerror(err));
// Insert the TcpConnection in the set.
this->connections.insert(connection);
// Start receiving data. // Start receiving data.
try try
{ {
// NOTE: This may throw.
connection->Start(); connection->Start();
// Notify the subclass.
UserOnNewTcpConnection(connection);
} }
catch (const MediaSoupError& error) catch (const MediaSoupError& error)
{ {
MS_ERROR("cannot start the TCP connection, closing the connection: %s", error.what());
delete connection; delete connection;
} }
// Notify the subclass and delete the connection if not accepted by the subclass.
if (UserOnNewTcpConnection(connection))
this->connections.insert(connection);
else
delete connection;
} }
inline void TcpServer::OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) inline void TcpServer::OnTcpConnectionClosed(TcpConnection* connection)
{ {
MS_TRACE(); MS_TRACE();
MS_DEBUG_DEV("TCP connection closed"); MS_DEBUG_DEV("TCP connection closed");
// Remove the TcpConnection from the set. // Remove the TcpConnection from the set.
size_t numErased = this->connections.erase(connection); this->connections.erase(connection);
// If the closed connection was not present in the set, do nothing else.
if (numErased == 0)
return;
// Notify the subclass. // Notify the subclass.
UserOnTcpConnectionClosed(connection, isClosedByPeer); UserOnTcpConnectionClosed(connection);
// Delete it.
delete connection;
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论