提交 48837cbc authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

TCP: working on #333:

- Do not create a ::TcpConnection pointer that later is filled with a RTC::TcpConnection
- Instead make the ::TcpServer announce a new TCP connection attempt to its children
  RTC::TcpServer and let the later accept it by calling a method in the parent.
- This seems to fix the valgrind logs "Invalid write of size X".
上级 9fb96956
......@@ -25,8 +25,7 @@ namespace RTC
/* Pure virtual methods inherited from ::TcpServer. */
public:
void UserOnTcpConnectionAlloc(::TcpConnection** connection) override;
bool UserOnNewTcpConnection(::TcpConnection* connection) override;
void UserOnTcpConnectionAlloc() override;
void UserOnTcpConnectionClosed(::TcpConnection* connection) override;
private:
......
......@@ -25,13 +25,15 @@ public:
uint16_t GetLocalPort() const;
size_t GetNumConnections() const;
protected:
void AcceptTcpConnection(TcpConnection* connection);
private:
bool SetLocalAddress();
/* Pure virtual methods that must be implemented by the subclass. */
protected:
virtual void UserOnTcpConnectionAlloc(TcpConnection** connection) = 0;
virtual bool UserOnNewTcpConnection(TcpConnection* connection) = 0;
virtual void UserOnTcpConnectionAlloc() = 0;
virtual void UserOnTcpConnectionClosed(TcpConnection* connection) = 0;
/* Callbacks fired by UV events. */
......
......@@ -29,15 +29,7 @@ namespace RTC
RTC::PortManager::UnbindTcp(this->localIp, this->localPort);
}
void TcpServer::UserOnTcpConnectionAlloc(::TcpConnection** connection)
{
MS_TRACE();
// Allocate a new RTC::TcpConnection for the TcpServer to handle it.
*connection = new RTC::TcpConnection(this->connListener, 65536);
}
bool TcpServer::UserOnNewTcpConnection(::TcpConnection* /*connection*/)
void TcpServer::UserOnTcpConnectionAlloc()
{
MS_TRACE();
......@@ -46,10 +38,14 @@ namespace RTC
{
MS_ERROR("cannot handle more than %zu connections", MaxTcpConnectionsPerServer);
return false;
return;
}
return true;
// Allocate a new RTC::TcpConnection for the TcpServer to handle it.
auto* connection = new RTC::TcpConnection(this->connListener, 65536);
// Accept it.
AcceptTcpConnection(connection);
}
void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection)
......
......@@ -12,10 +12,8 @@ inline static void onConnection(uv_stream_t* handle, int status)
{
auto* server = static_cast<TcpServer*>(handle->data);
if (server == nullptr)
return;
server->OnUvConnection(status);
if (server)
server->OnUvConnection(status);
}
inline static void onClose(uv_handle_t* handle)
......@@ -97,6 +95,48 @@ void TcpServer::Dump() const
MS_DUMP("</TcpServer>");
}
void TcpServer::AcceptTcpConnection(TcpConnection* connection)
{
MS_TRACE();
MS_ASSERT(connection != nullptr, "TcpConnection pointer was not allocated by the user");
try
{
connection->Setup(this, &(this->localAddr), this->localIp, this->localPort);
}
catch (const MediaSoupError& error)
{
delete connection;
return;
}
// Accept the connection.
int err = uv_accept(
reinterpret_cast<uv_stream_t*>(this->uvHandle),
reinterpret_cast<uv_stream_t*>(connection->GetUvHandle()));
if (err != 0)
MS_ABORT("uv_accept() failed: %s", uv_strerror(err));
// Start receiving data.
try
{
// NOTE: This may throw.
connection->Start();
}
catch (const MediaSoupError& error)
{
delete connection;
return;
}
// Store it.
this->connections.insert(connection);
}
bool TcpServer::SetLocalAddress()
{
MS_TRACE();
......@@ -129,8 +169,6 @@ inline void TcpServer::OnUvConnection(int status)
if (this->closed)
return;
int err;
if (status != 0)
{
MS_ERROR("error while receiving a new TCP connection: %s", uv_strerror(status));
......@@ -138,49 +176,8 @@ inline void TcpServer::OnUvConnection(int status)
return;
}
// Notify the subclass so it provides an allocated derived class of TCPConnection.
TcpConnection* connection = nullptr;
UserOnTcpConnectionAlloc(&connection);
MS_ASSERT(connection != nullptr, "TcpConnection pointer was not allocated by the user");
try
{
connection->Setup(this, &(this->localAddr), this->localIp, this->localPort);
}
catch (const MediaSoupError& error)
{
delete connection;
return;
}
// Accept the connection.
err = uv_accept(
reinterpret_cast<uv_stream_t*>(this->uvHandle),
reinterpret_cast<uv_stream_t*>(connection->GetUvHandle()));
if (err != 0)
MS_ABORT("uv_accept() failed: %s", uv_strerror(err));
// Start receiving data.
try
{
// NOTE: This may throw.
connection->Start();
}
catch (const MediaSoupError& error)
{
delete connection;
return;
}
// Notify the subclass and delete the connection if not accepted by the subclass.
if (UserOnNewTcpConnection(connection))
this->connections.insert(connection);
else
delete connection;
// Notify the subclass about a new TCP connection attempt.
UserOnTcpConnectionAlloc();
}
inline void TcpServer::OnTcpConnectionClosed(TcpConnection* connection)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论