提交 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 ...@@ -25,8 +25,7 @@ namespace RTC
/* Pure virtual methods inherited from ::TcpServer. */ /* Pure virtual methods inherited from ::TcpServer. */
public: public:
void UserOnTcpConnectionAlloc(::TcpConnection** connection) override; void UserOnTcpConnectionAlloc() override;
bool UserOnNewTcpConnection(::TcpConnection* connection) override;
void UserOnTcpConnectionClosed(::TcpConnection* connection) override; void UserOnTcpConnectionClosed(::TcpConnection* connection) override;
private: private:
......
...@@ -25,13 +25,15 @@ public: ...@@ -25,13 +25,15 @@ public:
uint16_t GetLocalPort() const; uint16_t GetLocalPort() const;
size_t GetNumConnections() const; size_t GetNumConnections() const;
protected:
void AcceptTcpConnection(TcpConnection* connection);
private: private:
bool SetLocalAddress(); bool SetLocalAddress();
/* 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() = 0;
virtual bool UserOnNewTcpConnection(TcpConnection* connection) = 0;
virtual void UserOnTcpConnectionClosed(TcpConnection* connection) = 0; virtual void UserOnTcpConnectionClosed(TcpConnection* connection) = 0;
/* Callbacks fired by UV events. */ /* Callbacks fired by UV events. */
......
...@@ -29,15 +29,7 @@ namespace RTC ...@@ -29,15 +29,7 @@ namespace RTC
RTC::PortManager::UnbindTcp(this->localIp, this->localPort); RTC::PortManager::UnbindTcp(this->localIp, this->localPort);
} }
void TcpServer::UserOnTcpConnectionAlloc(::TcpConnection** connection) void TcpServer::UserOnTcpConnectionAlloc()
{
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*/)
{ {
MS_TRACE(); MS_TRACE();
...@@ -46,10 +38,14 @@ namespace RTC ...@@ -46,10 +38,14 @@ namespace RTC
{ {
MS_ERROR("cannot handle more than %zu connections", MaxTcpConnectionsPerServer); 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) void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection)
......
...@@ -12,9 +12,7 @@ inline static void onConnection(uv_stream_t* handle, int status) ...@@ -12,9 +12,7 @@ inline static void onConnection(uv_stream_t* handle, int status)
{ {
auto* server = static_cast<TcpServer*>(handle->data); auto* server = static_cast<TcpServer*>(handle->data);
if (server == nullptr) if (server)
return;
server->OnUvConnection(status); server->OnUvConnection(status);
} }
...@@ -97,6 +95,48 @@ void TcpServer::Dump() const ...@@ -97,6 +95,48 @@ void TcpServer::Dump() const
MS_DUMP("</TcpServer>"); 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() bool TcpServer::SetLocalAddress()
{ {
MS_TRACE(); MS_TRACE();
...@@ -129,8 +169,6 @@ inline void TcpServer::OnUvConnection(int status) ...@@ -129,8 +169,6 @@ inline void TcpServer::OnUvConnection(int status)
if (this->closed) if (this->closed)
return; return;
int err;
if (status != 0) if (status != 0)
{ {
MS_ERROR("error while receiving a new TCP connection: %s", uv_strerror(status)); MS_ERROR("error while receiving a new TCP connection: %s", uv_strerror(status));
...@@ -138,49 +176,8 @@ inline void TcpServer::OnUvConnection(int status) ...@@ -138,49 +176,8 @@ inline void TcpServer::OnUvConnection(int status)
return; return;
} }
// Notify the subclass so it provides an allocated derived class of TCPConnection. // Notify the subclass about a new TCP connection attempt.
TcpConnection* connection = nullptr; UserOnTcpConnectionAlloc();
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;
} }
inline void TcpServer::OnTcpConnectionClosed(TcpConnection* connection) inline void TcpServer::OnTcpConnectionClosed(TcpConnection* connection)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论