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

Fix TCP leak (#325).

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