Unverified 提交 6c8d051d authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

C++: Remove Destroy() methods (#256)

上级 c4be3bef
......@@ -20,8 +20,6 @@ namespace Channel
public:
explicit UnixStreamSocket(int fd);
private:
~UnixStreamSocket() override;
public:
......@@ -42,7 +40,6 @@ namespace Channel
Json::CharReader* jsonReader{ nullptr };
Json::StreamWriter* jsonWriter{ nullptr };
size_t msgStart{ 0 }; // Where the latest message starts.
bool closed{ false };
};
} // namespace Channel
......
......@@ -34,12 +34,9 @@ namespace RTC
uint32_t consumerId,
RTC::Media::Kind kind,
uint32_t sourceProducerId);
private:
virtual ~Consumer();
public:
void Destroy();
Json::Value ToJson() const;
Json::Value GetStats() const;
void AddListener(RTC::ConsumerListener* listener);
......
......@@ -81,17 +81,13 @@ namespace RTC
std::string& remoteCert) = 0;
// The DTLS connection has been closed as the result of an error (such as a
// DTLS alert or a failure to validate the remote fingerprint).
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void OnDtlsFailed(const RTC::DtlsTransport* dtlsTransport) = 0;
// The DTLS connection has been closed due to receipt of a close_notify alert.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void OnDtlsClosed(const RTC::DtlsTransport* dtlsTransport) = 0;
// Need to send DTLS data to the peer.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void OnOutgoingDtlsData(
const RTC::DtlsTransport* dtlsTransport, const uint8_t* data, size_t len) = 0;
// DTLS application data received.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void OnDtlsApplicationData(
const RTC::DtlsTransport* dtlsTransport, const uint8_t* data, size_t len) = 0;
};
......@@ -122,12 +118,9 @@ namespace RTC
public:
explicit DtlsTransport(Listener* listener);
private:
~DtlsTransport() override;
public:
void Destroy();
void Dump() const;
void Run(Role localRole);
bool SetRemoteFingerprint(Fingerprint fingerprint);
......
......@@ -42,7 +42,6 @@ namespace RTC
public:
IceServer(Listener* listener, const std::string& usernameFragment, const std::string& password);
void Destroy();
void ProcessStunMessage(RTC::StunMessage* msg, RTC::TransportTuple* tuple);
const std::string& GetUsernameFragment() const;
const std::string& GetPassword() const;
......
......@@ -57,13 +57,9 @@ namespace RTC
RTC::RtpParameters& rtpParameters,
struct RtpMapping& rtpMapping,
bool paused);
~Producer();
public:
// Must be public because Router needs to call it.
virtual ~Producer();
public:
void Destroy();
Json::Value ToJson() const;
Json::Value GetStats() const;
void AddListener(RTC::ProducerListener* listener);
......
......@@ -38,12 +38,9 @@ namespace RTC
public:
Router(Listener* listener, Channel::Notifier* notifier, uint32_t routerId);
private:
virtual ~Router();
public:
void Destroy();
Json::Value ToJson() const;
void HandleRequest(Channel::Request* request);
......
......@@ -31,12 +31,9 @@ namespace RTC
public:
SrtpSession(Type type, Profile profile, uint8_t* key, size_t keyLen);
private:
~SrtpSession();
public:
void Destroy();
bool EncryptRtp(const uint8_t** data, size_t* len);
bool DecryptSrtp(const uint8_t* data, size_t* len);
bool EncryptRtcp(const uint8_t** data, size_t* len);
......
......@@ -18,9 +18,6 @@ namespace RTC
public:
TcpConnection(Listener* listener, size_t bufferSize);
private:
~TcpConnection() override = default;
public:
void Send(const uint8_t* data, size_t len);
size_t GetRecvBytes() const;
......
......@@ -39,16 +39,13 @@ namespace RTC
public:
TcpServer(Listener* listener, RTC::TcpConnection::Listener* connListener, int addressFamily);
private:
~TcpServer() override = default;
~TcpServer() override;
/* Pure virtual methods inherited from ::TcpServer. */
public:
void UserOnTcpConnectionAlloc(::TcpConnection** connection) override;
void UserOnNewTcpConnection(::TcpConnection* connection) override;
void UserOnTcpConnectionClosed(::TcpConnection* connection, bool isClosedByPeer) override;
void UserOnTcpServerClosed() override;
private:
// Passed by argument.
......
......@@ -67,12 +67,10 @@ namespace RTC
public:
Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId);
protected:
virtual ~Transport();
public:
void Destroy();
void Close();
virtual Json::Value ToJson() const = 0;
virtual Json::Value GetStats() const = 0;
void HandleProducer(RTC::Producer* producer);
......@@ -132,9 +130,10 @@ namespace RTC
Channel::Notifier* notifier{ nullptr };
// Allocated by this.
Timer* rtcpTimer{ nullptr };
// Allocated (Mirroring).
RTC::UdpSocket* mirrorSocket{ nullptr };
RTC::TransportTuple* mirrorTuple{ nullptr };
// Others.
bool closed{ false };
// Others (Producers and Consumers).
std::unordered_set<RTC::Producer*> producers;
std::unordered_set<RTC::Consumer*> consumers;
......
......@@ -39,14 +39,11 @@ namespace RTC
public:
UdpSocket(Listener* listener, int addressFamily);
UdpSocket(Listener* listener, const std::string& ip);
private:
~UdpSocket() override = default;
~UdpSocket() override;
/* Pure virtual methods inherited from ::UdpSocket. */
public:
void UserOnUdpDatagramRecv(const uint8_t* data, size_t len, const struct sockaddr* addr) override;
void UserOnUdpSocketClosed() override;
private:
// Passed by argument.
......
......@@ -42,9 +42,7 @@ namespace RTC
Channel::Notifier* notifier,
uint32_t transportId,
Options& options);
private:
~WebRtcTransport();
~WebRtcTransport() override;
public:
Json::Value ToJson() const override;
......
......@@ -19,12 +19,10 @@ public:
public:
explicit SignalsHandler(Listener* listener);
private:
~SignalsHandler() = default;
~SignalsHandler();
public:
void Destroy();
void Close();
void AddSignal(int signum, const std::string& name);
/* Callbacks fired by UV events. */
......@@ -36,6 +34,8 @@ private:
Listener* listener{ nullptr };
// Allocated by this.
std::vector<uv_signal_t*> uvHandles;
// Others.
bool closed{ false };
};
#endif
......@@ -37,19 +37,17 @@ public:
explicit TcpConnection(size_t bufferSize);
TcpConnection& operator=(const TcpConnection&) = delete;
TcpConnection(const TcpConnection&) = delete;
protected:
virtual ~TcpConnection();
public:
void Destroy();
void Close();
virtual void Dump() const;
void Setup(
Listener* listener,
struct sockaddr_storage* localAddr,
const std::string& localIP,
uint16_t localPort);
bool IsClosing() const;
bool IsClosed() const;
uv_tcp_t* GetUvHandle() const;
void Start();
void Write(const uint8_t* data, size_t len);
......@@ -71,8 +69,6 @@ public:
void OnUvReadAlloc(size_t suggestedSize, uv_buf_t* buf);
void OnUvRead(ssize_t nread, const uv_buf_t* buf);
void OnUvWriteError(int error);
void OnUvShutdown(uv_shutdown_t* req, int status);
void OnUvClosed();
/* Pure virtual methods that must be implemented by the subclass. */
protected:
......@@ -85,7 +81,7 @@ private:
uv_tcp_t* uvHandle{ nullptr };
// Others.
struct sockaddr_storage* localAddr{ nullptr };
bool isClosing{ false };
bool closed{ false };
bool isClosedByPeer{ false };
bool hasError{ false };
......@@ -105,9 +101,9 @@ protected:
/* Inline methods. */
inline bool TcpConnection::IsClosing() const
inline bool TcpConnection::IsClosed() const
{
return this->isClosing;
return this->closed;
}
inline uv_tcp_t* TcpConnection::GetUvHandle() const
......
......@@ -15,14 +15,11 @@ public:
* uvHandle must be an already initialized and binded uv_tcp_t pointer.
*/
TcpServer(uv_tcp_t* uvHandle, int backlog);
protected:
~TcpServer() override;
virtual ~TcpServer() override;
public:
void Destroy();
void Close();
virtual void Dump() const;
bool IsClosing() const;
const struct sockaddr* GetLocalAddress() const;
int GetLocalFamily() const;
const std::string& GetLocalIP() const;
......@@ -37,12 +34,10 @@ protected:
virtual void UserOnTcpConnectionAlloc(TcpConnection** connection) = 0;
virtual void UserOnNewTcpConnection(TcpConnection* connection) = 0;
virtual void UserOnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer) = 0;
virtual void UserOnTcpServerClosed() = 0;
/* Callbacks fired by UV events. */
public:
void OnUvConnection(int status);
void OnUvClosed();
/* Methods inherited from TcpConnection::Listener. */
public:
......@@ -53,7 +48,7 @@ private:
uv_tcp_t* uvHandle{ nullptr };
// Others.
std::unordered_set<TcpConnection*> connections;
bool isClosing{ false };
bool closed{ false };
protected:
struct sockaddr_storage localAddr;
......@@ -63,11 +58,6 @@ protected:
/* Inline methods. */
inline bool TcpServer::IsClosing() const
{
return this->isClosing;
}
inline size_t TcpServer::GetNumConnections() const
{
return this->connections.size();
......
......@@ -20,12 +20,10 @@ public:
explicit Timer(Listener* listener);
Timer& operator=(const Timer&) = delete;
Timer(const Timer&) = delete;
private:
~Timer() = default;
~Timer();
public:
void Destroy();
void Close();
void Start(uint64_t timeout, uint64_t repeat = 0);
void Stop();
void Reset();
......@@ -42,6 +40,7 @@ private:
// Allocated by this.
uv_timer_t* uvHandle{ nullptr };
// Others.
bool closed{ false };
uint64_t timeout{ 0 };
uint64_t repeat{ 0 };
};
......
......@@ -24,12 +24,10 @@ public:
explicit UdpSocket(uv_udp_t* uvHandle);
UdpSocket& operator=(const UdpSocket&) = delete;
UdpSocket(const UdpSocket&) = delete;
protected:
virtual ~UdpSocket();
public:
void Destroy();
void Close();
virtual void Dump() const;
void Send(const uint8_t* data, size_t len, const struct sockaddr* addr);
void Send(const std::string& data, const struct sockaddr* addr);
......@@ -50,18 +48,16 @@ public:
void OnUvRecvAlloc(size_t suggestedSize, uv_buf_t* buf);
void OnUvRecv(ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned int flags);
void OnUvSendError(int error);
void OnUvClosed();
/* Pure virtual methods that must be implemented by the subclass. */
protected:
virtual void UserOnUdpDatagramRecv(const uint8_t* data, size_t len, const struct sockaddr* addr) = 0;
virtual void UserOnUdpSocketClosed() = 0;
private:
// Allocated by this (may be passed by argument).
uv_udp_t* uvHandle{ nullptr };
// Others.
bool isClosing{ false };
bool closed{ false };
size_t recvBytes{ 0 };
size_t sentBytes{ 0 };
......
......@@ -20,13 +20,11 @@ public:
UnixStreamSocket(int fd, size_t bufferSize);
UnixStreamSocket& operator=(const UnixStreamSocket&) = delete;
UnixStreamSocket(const UnixStreamSocket&) = delete;
protected:
virtual ~UnixStreamSocket();
public:
void Destroy();
bool IsClosing() const;
void Close();
bool IsClosed() const;
void Write(const uint8_t* data, size_t len);
void Write(const std::string& data);
......@@ -35,8 +33,6 @@ public:
void OnUvReadAlloc(size_t suggestedSize, uv_buf_t* buf);
void OnUvRead(ssize_t nread, const uv_buf_t* buf);
void OnUvWriteError(int error);
void OnUvShutdown(uv_shutdown_t* req, int status);
void OnUvClosed();
/* Pure virtual methods that must be implemented by the subclass. */
protected:
......@@ -47,7 +43,7 @@ private:
// Allocated by this.
uv_pipe_t* uvHandle{ nullptr };
// Others.
bool isClosing{ false };
bool closed{ false };
bool isClosedByPeer{ false };
bool hasError{ false };
......@@ -62,9 +58,9 @@ protected:
/* Inline methods. */
inline bool UnixStreamSocket::IsClosing() const
inline bool UnixStreamSocket::IsClosed() const
{
return this->isClosing;
return this->closed;
}
inline void UnixStreamSocket::Write(const std::string& data)
......
......@@ -73,7 +73,7 @@ namespace Channel
void UnixStreamSocket::Send(Json::Value& msg)
{
if (this->closed)
if (IsClosed())
return;
// MS_TRACE_STD();
......@@ -117,7 +117,7 @@ namespace Channel
void UnixStreamSocket::SendLog(char* nsPayload, size_t nsPayloadLen)
{
if (this->closed)
if (IsClosed())
return;
// MS_TRACE_STD();
......@@ -154,7 +154,7 @@ namespace Channel
void UnixStreamSocket::SendBinary(const uint8_t* nsPayload, size_t nsPayloadLen)
{
if (this->closed)
if (IsClosed())
return;
size_t nsNumLen;
......@@ -194,7 +194,7 @@ namespace Channel
// Be ready to parse more than a single message in a single TCP chunk.
while (true)
{
if (IsClosing())
if (IsClosed())
return;
size_t readLen = this->bufferDataLen - this->msgStart;
......@@ -329,12 +329,8 @@ namespace Channel
{
MS_TRACE_STD();
this->closed = true;
// Notify the listener.
if (isClosedByPeer)
{
// Notify the listener.
this->listener->OnChannelUnixStreamSocketRemotelyClosed(this);
}
}
} // namespace Channel
......@@ -39,11 +39,6 @@ namespace RTC
delete this->rtpStream;
delete this->rtpMonitor;
}
void Consumer::Destroy()
{
MS_TRACE();
for (auto& listener : this->listeners)
{
......@@ -51,8 +46,6 @@ namespace RTC
}
this->notifier->Emit(this->consumerId, "close");
delete this;
}
Json::Value Consumer::ToJson() const
......
......@@ -579,19 +579,6 @@ namespace RTC
{
MS_TRACE();
if (this->ssl != nullptr)
{
SSL_free(this->ssl);
this->ssl = nullptr;
this->sslBioFromNetwork = nullptr;
this->sslBioToNetwork = nullptr;
}
}
void DtlsTransport::Destroy()
{
MS_TRACE();
if (IsRunning())
{
// Send close alert to the peer.
......@@ -599,10 +586,16 @@ namespace RTC
SendPendingOutgoingDtlsData();
}
// Destroy the DTLS timer.
this->timer->Destroy();
if (this->ssl != nullptr)
{
SSL_free(this->ssl);
this->ssl = nullptr;
this->sslBioFromNetwork = nullptr;
this->sslBioToNetwork = nullptr;
}
delete this;
// Close the DTLS timer.
delete this->timer;
}
void DtlsTransport::Dump() const
......
......@@ -27,13 +27,6 @@ namespace RTC
this->password.c_str());
}
void IceServer::Destroy()
{
MS_TRACE();
delete this;
}
void IceServer::ProcessStunMessage(RTC::StunMessage* msg, RTC::TransportTuple* tuple)
{
MS_TRACE();
......
......@@ -31,7 +31,7 @@ namespace RTC
MS_TRACE();
// Close the timer.
this->timer->Destroy();
delete this->timer;
}
// Returns true if this is a found nacked packet. False otherwise.
......
......@@ -42,8 +42,8 @@ namespace RTC
}
catch (const MediaSoupError& error)
{
// Destroy UdpSocket since ~PlainRtpTransport() will not be called.
this->udpSocket->Destroy();
// Close UdpSocket since ~PlainRtpTransport() will not be called.
delete this->udpSocket;
throw;
}
......@@ -84,9 +84,7 @@ namespace RTC
MS_TRACE();
delete this->tuple;
if (this->udpSocket != nullptr)
this->udpSocket->Destroy();
delete this->udpSocket;
}
Json::Value PlainRtpTransport::ToJson() const
......
......@@ -57,21 +57,14 @@ namespace RTC
delete rtpStream;
}
}
void Producer::Destroy()
{
MS_TRACE();
// Close the RTP key frame request block timer.
delete this->keyFrameRequestBlockTimer;
for (auto& listener : this->listeners)
{
listener->OnProducerClosed(this);
}
// Close the RTP key frame request block timer.
this->keyFrameRequestBlockTimer->Destroy();
delete this;
}
Json::Value Producer::ToJson() const
......
......@@ -31,11 +31,6 @@ namespace RTC
}
Router::~Router()
{
MS_TRACE();
}
void Router::Destroy()
{
MS_TRACE();
......@@ -45,7 +40,7 @@ namespace RTC
auto* producer = it->second;
it = this->producers.erase(it);
producer->Destroy();
delete producer;
}
// Close all the Consumers.
......@@ -54,27 +49,25 @@ namespace RTC
auto* consumer = it->second;
it = this->consumers.erase(it);
consumer->Destroy();
delete consumer;
}
// Close all the Transports.
// NOTE: It is critical to close Transports after Producers/Consumers
// because their Destroy() method fires an event in the Transport.
// because their destructor fires an event in the Transport.
for (auto it = this->transports.begin(); it != this->transports.end();)
{
auto* transport = it->second;
it = this->transports.erase(it);
transport->Destroy();
delete transport;
}
// Close the audio level timer.
this->audioLevelsTimer->Destroy();
delete this->audioLevelsTimer;
// Notify the listener.
this->listener->OnRouterClosed(this);
delete this;
}
Json::Value Router::ToJson() const
......@@ -601,7 +594,7 @@ namespace RTC
return;
}
transport->Destroy();
delete transport;
MS_DEBUG_DEV("Transport closed [transportId:%" PRIu32 "]", transport->transportId);
......@@ -999,7 +992,7 @@ namespace RTC
return;
}
producer->Destroy();
delete producer;
MS_DEBUG_DEV("Producer closed [producerId:%" PRIu32 "]", producer->producerId);
......@@ -1171,7 +1164,7 @@ namespace RTC
return;
}
consumer->Destroy();
delete consumer;
request->Accept();
......@@ -1611,6 +1604,7 @@ namespace RTC
this->producers.erase(producer->producerId);
// Remove the Producer from the map.
// NOTE: It may not exist if it failed before being inserted into the maps.
if (this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end())
{
// Iterate the map and close all the Consumers associated to it.
......@@ -1621,7 +1615,7 @@ namespace RTC
auto* consumer = *it;
it = consumers.erase(it);
consumer->Destroy();
delete consumer;
}
// Finally delete the Producer entry in the map.
......
......@@ -32,7 +32,7 @@ namespace RTC
MS_TRACE();
// Close the status check timer.
this->statusCheckTimer->Destroy();
delete this->statusCheckTimer;
}
Json::Value RtpStream::ToJson()
......@@ -260,8 +260,6 @@ namespace RTC
MS_TRACE();
if (timer == this->statusCheckTimer)
{
CheckStatus();
}
}
} // namespace RTC
......@@ -115,13 +115,6 @@ namespace RTC
}
}
void SrtpSession::Destroy()
{
MS_TRACE();
delete this;
}
bool SrtpSession::EncryptRtp(const uint8_t** data, size_t* len)
{
MS_TRACE();
......
......@@ -47,9 +47,9 @@ namespace RTC
while (true)
{
// We may receive multiple packets in the same TCP chunk. If one of them is
// a DTLS Close Alert this would be closed (Destroy() called) so we cannot call
// a DTLS Close Alert this would be closed (Close() called) so we cannot call
// our listeners anymore.
if (IsClosing())
if (IsClosed())
return;
size_t dataLen = this->bufferDataLen - this->frameStart;
......@@ -125,7 +125,7 @@ namespace RTC
"connection");
// Close the socket.
Destroy();
Close();
}
}
// The buffer is not full.
......@@ -151,7 +151,6 @@ namespace RTC
uint8_t frameLen[2];
Utils::Byte::Set2Bytes(frameLen, 0, len);
Write(frameLen, 2, data, len);
}
} // namespace RTC
......@@ -229,6 +229,17 @@ namespace RTC
MS_TRACE();
}
TcpServer::~TcpServer()
{
MS_TRACE();
// Mark the port as available again.
if (this->localAddr.ss_family == AF_INET)
RTC::TcpServer::availableIPv4Ports[this->localPort] = true;
else if (this->localAddr.ss_family == AF_INET6)
RTC::TcpServer::availableIPv6Ports[this->localPort] = true;
}
void TcpServer::UserOnTcpConnectionAlloc(::TcpConnection** connection)
{
MS_TRACE();
......@@ -243,31 +254,14 @@ namespace RTC
// Allow just MaxTcpConnectionsPerServer.
if (GetNumConnections() > MaxTcpConnectionsPerServer)
connection->Destroy();
connection->Close();
}
void TcpServer::UserOnTcpConnectionClosed(::TcpConnection* connection, bool isClosedByPeer)
{
MS_TRACE();
// Notify the listener.
// NOTE: Don't do it if closing (since at this point the listener is already freed).
// At the end, this is just called if the connection was remotely closed.
if (!IsClosing())
{
this->listener->OnRtcTcpConnectionClosed(
this, dynamic_cast<RTC::TcpConnection*>(connection), isClosedByPeer);
}
}
void TcpServer::UserOnTcpServerClosed()
{
MS_TRACE();
// Mark the port as available again.
if (this->localAddr.ss_family == AF_INET)
RTC::TcpServer::availableIPv4Ports[this->localPort] = true;
else if (this->localAddr.ss_family == AF_INET6)
RTC::TcpServer::availableIPv6Ports[this->localPort] = true;
this->listener->OnRtcTcpConnectionClosed(
this, dynamic_cast<RTC::TcpConnection*>(connection), isClosedByPeer);
}
} // namespace RTC
......@@ -30,13 +30,26 @@ namespace RTC
{
MS_TRACE();
if (!this->closed)
Close();
}
void Transport::Close()
{
MS_TRACE();
if (this->closed)
return;
this->closed = true;
// Close all the handled Producers.
for (auto it = this->producers.begin(); it != this->producers.end();)
{
auto* producer = *it;
it = this->producers.erase(it);
producer->Destroy();
delete producer;
}
// Disable all the handled Consumers.
......@@ -48,22 +61,22 @@ namespace RTC
consumer->RemoveListener(this);
}
// Destroy the RTCP timer.
if (this->rtcpTimer != nullptr)
this->rtcpTimer->Destroy();
}
// Close the RTCP timer.
delete this->rtcpTimer;
void Transport::Destroy()
{
MS_TRACE();
// Delete mirror tuple.
if (this->mirrorTuple != nullptr)
delete this->mirrorTuple;
// Notify.
this->notifier->Emit(this->transportId, "close");
// Delete mirror socket.
if (this->mirrorSocket != nullptr)
delete this->mirrorSocket;
// Notify the listener.
this->listener->OnTransportClosed(this);
delete this;
// Notify.
this->notifier->Emit(this->transportId, "close");
}
void Transport::StartMirroring(MirroringOptions& options)
......@@ -132,9 +145,7 @@ namespace RTC
void Transport::StopMirroring()
{
delete this->mirrorTuple;
if (this->mirrorSocket != nullptr)
this->mirrorSocket->Destroy();
delete this->mirrorSocket;
this->mirrorTuple = nullptr;
this->mirrorSocket = nullptr;
......
......@@ -11,7 +11,7 @@
/* Static methods for UV callbacks. */
static inline void onErrorClose(uv_handle_t* handle)
static inline void onClose(uv_handle_t* handle)
{
delete handle;
}
......@@ -188,7 +188,7 @@ namespace RTC
iteratingPort,
uv_strerror(err));
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onClose));
// If bind() fails due to "too many open files" stop here.
if (err == UV_EMFILE)
......@@ -235,6 +235,17 @@ namespace RTC
MS_TRACE();
}
UdpSocket::~UdpSocket()
{
MS_TRACE();
// Mark the port as available again.
if (this->localAddr.ss_family == AF_INET)
RTC::UdpSocket::availableIPv4Ports[this->localPort] = true;
else if (this->localAddr.ss_family == AF_INET6)
RTC::UdpSocket::availableIPv6Ports[this->localPort] = true;
}
void UdpSocket::UserOnUdpDatagramRecv(const uint8_t* data, size_t len, const struct sockaddr* addr)
{
MS_TRACE();
......@@ -249,15 +260,4 @@ namespace RTC
// Notify the reader.
this->listener->OnPacketRecv(this, data, len, addr);
}
void UdpSocket::UserOnUdpSocketClosed()
{
MS_TRACE();
// Mark the port as available again.
if (this->localAddr.ss_family == AF_INET)
RTC::UdpSocket::availableIPv4Ports[this->localPort] = true;
else if (this->localAddr.ss_family == AF_INET6)
RTC::UdpSocket::availableIPv6Ports[this->localPort] = true;
}
} // namespace RTC
......@@ -163,22 +163,16 @@ namespace RTC
// Ensure there is at least one IP:port binding.
if (this->udpSockets.empty() && this->tcpServers.empty())
{
// NOTE: We must manually delete above allocated objects. We cannot call `delete this`
// here since it would call the parent ~Transport destructor, and it would be called
// again after throwing the exception here.
//
// See: https://github.com/versatica/mediasoup/issues/222
this->iceServer->Destroy();
delete this->iceServer;
for (auto* socket : this->udpSockets)
{
socket->Destroy();
delete socket;
}
for (auto* server : this->tcpServers)
{
server->Destroy();
delete server;
}
MS_THROW_ERROR("could not open any IP:port");
......@@ -198,31 +192,29 @@ namespace RTC
{
MS_TRACE();
if (this->srtpRecvSession != nullptr)
this->srtpRecvSession->Destroy();
if (this->srtpSendSession != nullptr)
this->srtpSendSession->Destroy();
if (this->dtlsTransport != nullptr)
this->dtlsTransport->Destroy();
// It's important deleting DTLS transport first since it will generate a
// DTLS alert to be sent.
delete this->dtlsTransport;
if (this->iceServer != nullptr)
this->iceServer->Destroy();
delete this->iceServer;
for (auto* socket : this->udpSockets)
{
socket->Destroy();
delete socket;
}
this->udpSockets.clear();
for (auto* server : this->tcpServers)
{
server->Destroy();
delete server;
}
this->tcpServers.clear();
this->selectedTuple = nullptr;
if (this->srtpRecvSession != nullptr)
delete this->srtpRecvSession;
if (this->srtpSendSession != nullptr)
delete this->srtpSendSession;
}
Json::Value WebRtcTransport::ToJson() const
......@@ -1128,12 +1120,12 @@ namespace RTC
// Close it if it was already set and update it.
if (this->srtpSendSession != nullptr)
{
this->srtpSendSession->Destroy();
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
}
if (this->srtpRecvSession != nullptr)
{
this->srtpRecvSession->Destroy();
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
}
......@@ -1156,7 +1148,7 @@ namespace RTC
{
MS_ERROR("error creating SRTP receiving session: %s", error.what());
this->srtpSendSession->Destroy();
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
}
......@@ -1191,7 +1183,7 @@ namespace RTC
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
// This is a fatal error so close the transport.
Destroy();
Close();
}
void WebRtcTransport::OnDtlsClosed(const RTC::DtlsTransport* /*dtlsTransport*/)
......@@ -1210,7 +1202,7 @@ namespace RTC
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
// This is a fatal error so close the transport.
Destroy();
Close();
}
void WebRtcTransport::OnOutgoingDtlsData(
......
......@@ -39,6 +39,9 @@ Worker::Worker(Channel::UnixStreamSocket* channel) : channel(channel)
Worker::~Worker()
{
MS_TRACE();
if (!this->closed)
Close();
}
void Worker::Close()
......@@ -46,17 +49,12 @@ void Worker::Close()
MS_TRACE();
if (this->closed)
{
MS_ERROR("already closed");
return;
}
this->closed = true;
// Close the SignalsHandler.
if (this->signalsHandler != nullptr)
this->signalsHandler->Destroy();
delete this->signalsHandler;
// Close all the Routers.
// NOTE: Upon Router closure the onRouterClosed() method is called, which
......@@ -67,15 +65,14 @@ void Worker::Close()
RTC::Router* router = it->second;
it = this->routers.erase(it);
router->Destroy();
delete router;
}
// Delete the Notifier.
delete this->notifier;
// Close the Channel socket.
if (this->channel != nullptr)
this->channel->Destroy();
delete this->channel;
}
RTC::Router* Worker::GetRouterFromRequest(Channel::Request* request, uint32_t* routerId)
......@@ -241,7 +238,8 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
return;
}
router->Destroy();
delete router;
request->Accept();
break;
......@@ -322,8 +320,6 @@ void Worker::OnChannelUnixStreamSocketRemotelyClosed(Channel::UnixStreamSocket*
// abruptly died (SIGKILL?) so we must die.
MS_ERROR_STD("Channel remotely closed, killing myself");
this->channel = nullptr;
Close();
}
......
......@@ -25,10 +25,36 @@ SignalsHandler::SignalsHandler(Listener* listener) : listener(listener)
MS_TRACE();
}
SignalsHandler::~SignalsHandler()
{
MS_TRACE();
if (!this->closed)
Close();
}
void SignalsHandler::Close()
{
MS_TRACE();
if (this->closed)
return;
this->closed = true;
for (auto uvHandle : uvHandles)
{
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onClose));
}
}
void SignalsHandler::AddSignal(int signum, const std::string& name)
{
MS_TRACE();
if (this->closed)
MS_THROW_ERROR("closed");
int err;
auto uvHandle = new uv_signal_t;
......@@ -50,19 +76,6 @@ void SignalsHandler::AddSignal(int signum, const std::string& name)
this->uvHandles.push_back(uvHandle);
}
void SignalsHandler::Destroy()
{
MS_TRACE();
for (auto uvHandle : uvHandles)
{
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onClose));
}
// And delete this.
delete this;
}
inline void SignalsHandler::OnUvSignal(int signum)
{
MS_TRACE();
......
......@@ -34,14 +34,19 @@ inline static void onWrite(uv_write_t* req, int status)
connection->OnUvWriteError(status);
}
inline static void onShutdown(uv_shutdown_t* req, int status)
inline static void onClose(uv_handle_t* handle)
{
static_cast<TcpConnection*>(req->data)->OnUvShutdown(req, status);
delete handle;
}
inline static void onClose(uv_handle_t* handle)
inline static void onShutdown(uv_shutdown_t* req, int status)
{
static_cast<TcpConnection*>(handle->data)->OnUvClosed();
auto* handle = req->handle;
delete req;
// Now do close the handle.
uv_close(reinterpret_cast<uv_handle_t*>(handle), static_cast<uv_close_cb>(onClose));
}
/* Instance methods. */
......@@ -60,46 +65,22 @@ TcpConnection::~TcpConnection()
{
MS_TRACE();
delete this->uvHandle;
delete[] this->buffer;
}
void TcpConnection::Setup(
Listener* listener, struct sockaddr_storage* localAddr, const std::string& localIP, uint16_t localPort)
{
MS_TRACE();
int err;
// Set the UV handle.
err = uv_tcp_init(DepLibUV::GetLoop(), this->uvHandle);
if (err != 0)
{
delete this->uvHandle;
this->uvHandle = nullptr;
MS_THROW_ERROR("uv_tcp_init() failed: %s", uv_strerror(err));
}
if (!this->closed)
Close();
// Set the listener.
this->listener = listener;
// Set the local address.
this->localAddr = localAddr;
this->localIP = localIP;
this->localPort = localPort;
delete[] this->buffer;
}
void TcpConnection::Destroy()
void TcpConnection::Close()
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
int err;
this->isClosing = true;
this->closed = true;
// Don't read more.
err = uv_read_stop(reinterpret_cast<uv_stream_t*>(this->uvHandle));
......@@ -123,6 +104,9 @@ void TcpConnection::Destroy()
{
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
......@@ -134,15 +118,41 @@ void TcpConnection::Dump() const
static_cast<uint16_t>(this->localPort),
this->peerIP.c_str(),
static_cast<uint16_t>(this->peerPort),
(!this->isClosing) ? "open" : "closed");
(!this->closed) ? "open" : "closed");
MS_DEBUG_DEV("</TcpConnection>");
}
void TcpConnection::Setup(
Listener* listener, struct sockaddr_storage* localAddr, const std::string& localIP, uint16_t localPort)
{
MS_TRACE();
// Set the listener.
this->listener = listener;
// Set the local address.
this->localAddr = localAddr;
this->localIP = localIP;
this->localPort = localPort;
int err;
// Set the UV handle.
err = uv_tcp_init(DepLibUV::GetLoop(), this->uvHandle);
if (err != 0)
{
delete this->uvHandle;
this->uvHandle = nullptr;
MS_THROW_ERROR("uv_tcp_init() failed: %s", uv_strerror(err));
}
}
void TcpConnection::Start()
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
int err;
......@@ -163,7 +173,7 @@ void TcpConnection::Write(const uint8_t* data, size_t len)
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
if (len == 0)
......@@ -195,7 +205,7 @@ void TcpConnection::Write(const uint8_t* data, size_t len)
{
MS_WARN_DEV("uv_try_write() failed, closing the connection: %s", uv_strerror(written));
Destroy();
Close();
return;
}
......@@ -228,7 +238,7 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
if (len1 == 0 && len2 == 0)
......@@ -262,7 +272,8 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
{
MS_WARN_DEV("uv_try_write() failed, closing the connection: %s", uv_strerror(written));
Destroy();
Close();
return;
}
......@@ -331,6 +342,9 @@ inline void TcpConnection::OnUvReadAlloc(size_t /*suggestedSize*/, uv_buf_t* buf
{
MS_TRACE();
if (this->closed)
return;
// If this is the first call to onUvReadAlloc() then allocate the receiving buffer now.
if (this->buffer == nullptr)
this->buffer = new uint8_t[this->bufferSize];
......@@ -354,7 +368,7 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
if (nread == 0)
......@@ -377,7 +391,7 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
this->isClosedByPeer = true;
// Close server side of the connection.
Destroy();
Close();
}
// Some error.
else
......@@ -387,7 +401,7 @@ inline void TcpConnection::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
this->hasError = true;
// Close server side of the connection.
Destroy();
Close();
}
}
......@@ -395,7 +409,7 @@ inline void TcpConnection::OnUvWriteError(int error)
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
if (error != UV_EPIPE && error != UV_ENOTCONN)
......@@ -403,31 +417,5 @@ inline void TcpConnection::OnUvWriteError(int error)
MS_WARN_DEV("write error, closing the connection: %s", uv_strerror(error));
Destroy();
}
inline void TcpConnection::OnUvShutdown(uv_shutdown_t* req, int status)
{
MS_TRACE();
delete req;
if (status != 0)
{
MS_WARN_DEV("shutdown error: %s", uv_strerror(status));
}
// Now do close the handle.
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
}
inline void TcpConnection::OnUvClosed()
{
MS_TRACE();
// Notify the listener.
this->listener->OnTcpConnectionClosed(this, this->isClosedByPeer);
// And delete this.
delete this;
Close();
}
......@@ -15,11 +15,6 @@ inline static void onConnection(uv_stream_t* handle, int status)
}
inline static void onClose(uv_handle_t* handle)
{
static_cast<TcpServer*>(handle->data)->OnUvClosed();
}
inline static void onErrorClose(uv_handle_t* handle)
{
delete handle;
}
......@@ -76,7 +71,7 @@ TcpServer::TcpServer(const std::string& ip, uint16_t port, int backlog)
default:
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("invalid binding IP '%s'", ip.c_str());
......@@ -87,7 +82,7 @@ TcpServer::TcpServer(const std::string& ip, uint16_t port, int backlog)
err = uv_tcp_bind(this->uvHandle, reinterpret_cast<const struct sockaddr*>(&bindAddr), flags);
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("uv_tcp_bind() failed: %s", uv_strerror(err));
}
......@@ -97,14 +92,14 @@ TcpServer::TcpServer(const std::string& ip, uint16_t port, int backlog)
static_cast<uv_connection_cb>(onConnection));
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("uv_listen() failed: %s", uv_strerror(err));
}
// Set local address.
if (!SetLocalAddress())
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("error setting local IP and port");
}
}
......@@ -123,14 +118,14 @@ TcpServer::TcpServer(uv_tcp_t* uvHandle, int backlog) : uvHandle(uvHandle)
static_cast<uv_connection_cb>(onConnection));
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("uv_listen() failed: %s", uv_strerror(err));
}
// Set local address.
if (!SetLocalAddress())
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("error setting local IP and port");
}
}
......@@ -139,33 +134,30 @@ TcpServer::~TcpServer()
{
MS_TRACE();
delete this->uvHandle;
if (!this->closed)
Close();
}
void TcpServer::Destroy()
void TcpServer::Close()
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
this->isClosing = true;
this->closed = true;
// If there are no connections then close now.
if (this->connections.empty())
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
}
// Otherwise close all the connections (but not the TCP server).
else
MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
for (auto it = this->connections.begin(); it != this->connections.end();)
{
MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
auto* connection = *it;
for (auto connection : this->connections)
{
connection->Destroy();
}
it = this->connections.erase(it);
delete connection;
}
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
}
void TcpServer::Dump() const
......@@ -175,7 +167,7 @@ void TcpServer::Dump() const
" [TCP, local:%s :%" PRIu16 ", status:%s, connections:%zu]",
this->localIP.c_str(),
static_cast<uint16_t>(this->localPort),
(!this->isClosing) ? "open" : "closed",
(!this->closed) ? "open" : "closed",
this->connections.size());
MS_DEBUG_DEV("</TcpServer>");
}
......@@ -210,7 +202,7 @@ inline void TcpServer::OnUvConnection(int status)
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
int err;
......@@ -253,59 +245,31 @@ inline void TcpServer::OnUvConnection(int status)
try
{
connection->Start();
// Notify the subclass.
UserOnNewTcpConnection(connection);
}
catch (const MediaSoupError& error)
{
MS_ERROR("cannot run the TCP connection, closing the connection: %s", error.what());
connection->Destroy();
// NOTE: Don't return here so the user won't be notified about a TCP connection
// closure for which there was not a previous creation event.
delete connection;
}
// Notify the subclass.
UserOnNewTcpConnection(connection);
}
inline void TcpServer::OnUvClosed()
void TcpServer::OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer)
{
MS_TRACE();
// Notify the subclass.
UserOnTcpServerClosed();
// And delete this.
delete this;
}
inline void TcpServer::OnTcpConnectionClosed(TcpConnection* connection, bool isClosedByPeer)
{
MS_TRACE();
// NOTE:
// Worst scenario is that in which this is the latest connection,
// which is remotely closed (no TcpServer.Destroy() was called) and the user
// call TcpServer.Destroy() on UserOnTcpConnectionClosed() callback, so Destroy()
// is called with zero connections and calls uv_close(), but then
// onTcpConnectionClosed() continues and finds that isClosing is true and
// there are zero connections, so calls uv_close() again and get a crash.
//
// SOLUTION:
// Check isClosing value *before* onTcpConnectionClosed() callback.
bool wasClosing = this->isClosing;
MS_DEBUG_DEV("TCP connection closed");
// Remove the TcpConnection from the set.
this->connections.erase(connection);
size_t numErased = this->connections.erase(connection);
// If the closed connection was not present in the set, do nothing else.
if (numErased == 0)
return;
// Notify the subclass.
UserOnTcpConnectionClosed(connection, isClosedByPeer);
// Check if the server was closing connections, and if this is the last
// connection then close the server now.
if (wasClosing && this->connections.empty())
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
}
......@@ -39,20 +39,33 @@ Timer::Timer(Listener* listener) : listener(listener)
}
}
void Timer::Destroy()
Timer::~Timer()
{
MS_TRACE();
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
if (!this->closed)
Close();
}
void Timer::Close()
{
MS_TRACE();
if (this->closed)
return;
// Delete this.
delete this;
this->closed = true;
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
}
void Timer::Start(uint64_t timeout, uint64_t repeat)
{
MS_TRACE();
if (this->closed)
MS_THROW_ERROR("closed");
this->timeout = timeout;
this->repeat = repeat;
......@@ -70,6 +83,9 @@ void Timer::Stop()
{
MS_TRACE();
if (this->closed)
MS_THROW_ERROR("closed");
int err;
err = uv_timer_stop(this->uvHandle);
......@@ -81,6 +97,9 @@ void Timer::Reset()
{
MS_TRACE();
if (this->closed)
MS_THROW_ERROR("closed");
int err;
if (uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) == 0)
......@@ -98,6 +117,9 @@ void Timer::Restart()
{
MS_TRACE();
if (this->closed)
MS_THROW_ERROR("closed");
int err;
if (uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) != 0)
......
......@@ -39,11 +39,6 @@ inline static void onSend(uv_udp_send_t* req, int status)
}
inline static void onClose(uv_handle_t* handle)
{
static_cast<UdpSocket*>(handle->data)->OnUvClosed();
}
inline static void onErrorClose(uv_handle_t* handle)
{
delete handle;
}
......@@ -100,7 +95,7 @@ UdpSocket::UdpSocket(const std::string& ip, uint16_t port)
default:
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("invalid binding IP '%s'", ip.c_str());
......@@ -111,7 +106,7 @@ UdpSocket::UdpSocket(const std::string& ip, uint16_t port)
err = uv_udp_bind(this->uvHandle, reinterpret_cast<const struct sockaddr*>(&bindAddr), flags);
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("uv_udp_bind() failed: %s", uv_strerror(err));
}
......@@ -119,14 +114,14 @@ UdpSocket::UdpSocket(const std::string& ip, uint16_t port)
this->uvHandle, static_cast<uv_alloc_cb>(onAlloc), static_cast<uv_udp_recv_cb>(onRecv));
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("uv_udp_recv_start() failed: %s", uv_strerror(err));
}
// Set local address.
if (!SetLocalAddress())
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("error setting local IP and port");
}
}
......@@ -143,14 +138,14 @@ UdpSocket::UdpSocket(uv_udp_t* uvHandle) : uvHandle(uvHandle)
this->uvHandle, static_cast<uv_alloc_cb>(onAlloc), static_cast<uv_udp_recv_cb>(onRecv));
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("uv_udp_recv_start() failed: %s", uv_strerror(err));
}
// Set local address.
if (!SetLocalAddress())
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR("error setting local IP and port");
}
}
......@@ -159,19 +154,20 @@ UdpSocket::~UdpSocket()
{
MS_TRACE();
delete this->uvHandle;
if (!this->closed)
Close();
}
void UdpSocket::Destroy()
void UdpSocket::Close()
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
int err;
this->isClosing = true;
this->closed = true;
// Don't read more.
err = uv_udp_recv_stop(this->uvHandle);
......@@ -188,7 +184,7 @@ void UdpSocket::Dump() const
" [UDP, local:%s :%" PRIu16 ", status:%s]",
this->localIP.c_str(),
static_cast<uint16_t>(this->localPort),
(!this->isClosing) ? "open" : "closed");
(!this->closed) ? "open" : "closed");
MS_DEBUG_DEV("</UdpSocket>");
}
......@@ -196,7 +192,7 @@ void UdpSocket::Send(const uint8_t* data, size_t len, const struct sockaddr* add
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
if (len == 0)
......@@ -271,7 +267,7 @@ void UdpSocket::Send(const uint8_t* data, size_t len, const std::string& ip, uin
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
int err;
......@@ -357,7 +353,7 @@ inline void UdpSocket::OnUvRecv(
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
// NOTE: libuv calls twice to alloc & recv when a datagram is received, the
......@@ -393,19 +389,8 @@ inline void UdpSocket::OnUvSendError(int /*error*/)
{
MS_TRACE();
if (this->isClosing)
if (this->closed)
return;
MS_DEBUG_DEV("send error: %s", uv_strerror(error));
}
inline void UdpSocket::OnUvClosed()
{
MS_TRACE();
// Notify the subclass.
UserOnUdpSocketClosed();
// And delete this.
delete this;
}
......@@ -38,19 +38,19 @@ inline static void onWrite(uv_write_t* req, int status)
socket->OnUvWriteError(status);
}
inline static void onShutdown(uv_shutdown_t* req, int status)
{
static_cast<UnixStreamSocket*>(req->data)->OnUvShutdown(req, status);
}
inline static void onClose(uv_handle_t* handle)
{
static_cast<UnixStreamSocket*>(handle->data)->OnUvClosed();
delete handle;
}
inline static void onErrorClose(uv_handle_t* handle)
inline static void onShutdown(uv_shutdown_t* req, int status)
{
delete handle;
auto* handle = req->handle;
delete req;
// Now do close the handle.
uv_close(reinterpret_cast<uv_handle_t*>(handle), static_cast<uv_close_cb>(onClose));
}
/* Instance methods. */
......@@ -76,7 +76,7 @@ UnixStreamSocket::UnixStreamSocket(int fd, size_t bufferSize) : bufferSize(buffe
err = uv_pipe_open(this->uvHandle, fd);
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR_STD("uv_pipe_open() failed: %s", uv_strerror(err));
}
......@@ -88,7 +88,7 @@ UnixStreamSocket::UnixStreamSocket(int fd, size_t bufferSize) : bufferSize(buffe
static_cast<uv_read_cb>(onRead));
if (err != 0)
{
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onErrorClose));
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
MS_THROW_ERROR_STD("uv_read_start() failed: %s", uv_strerror(err));
}
......@@ -100,20 +100,22 @@ UnixStreamSocket::~UnixStreamSocket()
{
MS_TRACE_STD();
delete this->uvHandle;
if (!this->closed)
Close();
delete[] this->buffer;
}
void UnixStreamSocket::Destroy()
void UnixStreamSocket::Close()
{
MS_TRACE_STD();
if (this->isClosing)
if (this->closed)
return;
int err;
this->isClosing = true;
this->closed = true;
// Don't read more.
err = uv_read_stop(reinterpret_cast<uv_stream_t*>(this->uvHandle));
......@@ -140,7 +142,7 @@ void UnixStreamSocket::Destroy()
void UnixStreamSocket::Write(const uint8_t* data, size_t len)
{
if (this->isClosing)
if (this->closed)
return;
if (len == 0)
......@@ -172,7 +174,10 @@ void UnixStreamSocket::Write(const uint8_t* data, size_t len)
{
MS_ERROR_STD("uv_try_write() failed, closing the socket: %s", uv_strerror(written));
Destroy();
Close();
// Notify the subclass.
UserOnUnixStreamSocketClosed(this->isClosedByPeer);
return;
}
......@@ -202,6 +207,9 @@ inline void UnixStreamSocket::OnUvReadAlloc(size_t /*suggestedSize*/, uv_buf_t*
{
MS_TRACE_STD();
if (this->closed)
return;
// If this is the first call to onUvReadAlloc() then allocate the receiving buffer now.
if (this->buffer == nullptr)
this->buffer = new uint8_t[this->bufferSize];
......@@ -225,6 +233,9 @@ inline void UnixStreamSocket::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
{
MS_TRACE_STD();
if (this->closed)
return;
if (nread == 0)
return;
......@@ -243,7 +254,10 @@ inline void UnixStreamSocket::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
this->isClosedByPeer = true;
// Close local side of the pipe.
Destroy();
Close();
// Notify the subclass.
UserOnUnixStreamSocketClosed(this->isClosedByPeer);
}
// Some error.
else
......@@ -253,7 +267,10 @@ inline void UnixStreamSocket::OnUvRead(ssize_t nread, const uv_buf_t* /*buf*/)
this->hasError = true;
// Close the socket.
Destroy();
Close();
// Notify the subclass.
UserOnUnixStreamSocketClosed(this->isClosedByPeer);
}
}
......@@ -261,7 +278,7 @@ inline void UnixStreamSocket::OnUvWriteError(int error)
{
MS_TRACE_STD();
if (this->isClosing)
if (this->closed)
return;
if (error != UV_EPIPE && error != UV_ENOTCONN)
......@@ -269,31 +286,8 @@ inline void UnixStreamSocket::OnUvWriteError(int error)
MS_ERROR_STD("write error, closing the pipe: %s", uv_strerror(error));
Destroy();
}
inline void UnixStreamSocket::OnUvShutdown(uv_shutdown_t* req, int status)
{
MS_TRACE_STD();
delete req;
if (status != 0)
{
MS_ERROR_STD("shutdown error: %s", uv_strerror(status));
}
// Now do close the handle.
uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose));
}
inline void UnixStreamSocket::OnUvClosed()
{
MS_TRACE_STD();
Close();
// Notify the subclass.
UserOnUnixStreamSocketClosed(this->isClosedByPeer);
// And delete this.
delete this;
}
......@@ -92,6 +92,7 @@ int main(int argc, char* argv[])
// Worker ended.
destroy();
exitSuccess();
}
catch (const MediaSoupError& error)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论