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

C++ clean up: Make destuctor private for those classes that must be explicitely…

C++ clean up: Make destuctor private for those classes that must be explicitely "destroyed" via a Destroy() method.
上级 de1b7ecd
......@@ -12,9 +12,6 @@ namespace Channel
{
public:
explicit Notifier(Channel::UnixStreamSocket* channel);
virtual ~Notifier();
void Close();
void Emit(uint32_t targetId, std::string event);
void Emit(uint32_t targetId, std::string event, Json::Value& data);
......
......@@ -24,8 +24,11 @@ namespace Channel
public:
explicit UnixStreamSocket(int fd);
private:
virtual ~UnixStreamSocket();
public:
void SetListener(Listener* listener);
void Send(Json::Value &json);
void SendLog(char* ns_payload, size_t ns_payload_len);
......
......@@ -76,16 +76,16 @@ namespace RTC
virtual void onDtlsConnected(DtlsTransport* dtlsTransport, RTC::SrtpSession::Profile srtp_profile, uint8_t* srtp_local_key, size_t srtp_local_key_len, uint8_t* srtp_remote_key, size_t srtp_remote_key_len, 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 Close() during this callback.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void onDtlsFailed(DtlsTransport* dtlsTransport) = 0;
// The DTLS connection has been closed due to receipt of a close_notify alert.
// NOTE: The caller MUST NOT call Close() during this callback.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void onDtlsClosed(DtlsTransport* dtlsTransport) = 0;
// Need to send DTLS data to the peer.
// NOTE: The caller MUST NOT call Close() during this callback.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void onOutgoingDtlsData(DtlsTransport* dtlsTransport, const uint8_t* data, size_t len) = 0;
// DTLS application data received.
// NOTE: The caller MUST NOT call Close() during this callback.
// NOTE: The caller MUST NOT call Destroy() during this callback.
virtual void onDtlsApplicationData(DtlsTransport* dtlsTransport, const uint8_t* data, size_t len) = 0;
};
......@@ -115,9 +115,12 @@ namespace RTC
public:
explicit DtlsTransport(Listener* listener);
private:
virtual ~DtlsTransport();
void Close();
public:
void Destroy();
void Dump() const;
void Run(Role localRole);
void SetRemoteFingerprint(Fingerprint fingerprint);
......
......@@ -44,7 +44,7 @@ namespace RTC
public:
IceServer(Listener* listener, const std::string& usernameFragment, const std::string& password);
void Close();
void Destroy();
void ProcessStunMessage(RTC::StunMessage* msg, RTC::TransportTuple* tuple);
const std::string& GetUsernameFragment() const;
const std::string& GetPassword() const;
......
......@@ -47,9 +47,12 @@ namespace RTC
public:
Peer(Listener* listener, Channel::Notifier* notifier, uint32_t peerId, std::string& peerName);
private:
virtual ~Peer();
void Close();
public:
void Destroy();
Json::Value toJson() const;
void HandleRequest(Channel::Request* request);
bool HasCapabilities() const;
......
......@@ -39,9 +39,12 @@ namespace RTC
public:
Room(Listener* listener, Channel::Notifier* notifier, uint32_t roomId, Json::Value& data);
private:
virtual ~Room();
void Close();
public:
void Destroy();
Json::Value toJson() const;
void HandleRequest(Channel::Request* request);
const RTC::RtpCapabilities& GetCapabilities() const;
......
......@@ -41,9 +41,12 @@ namespace RTC
public:
RtpReceiver(Listener* listener, Channel::Notifier* notifier, uint32_t rtpReceiverId, RTC::Media::Kind kind);
private:
virtual ~RtpReceiver();
void Close();
public:
void Destroy();
Json::Value toJson() const;
void HandleRequest(Channel::Request* request);
void SetTransport(RTC::Transport* transport);
......
......@@ -31,25 +31,18 @@ namespace RTC
virtual void onRtpSenderClosed(RtpSender* rtpSender) = 0;
};
// NOTE: Comment for now.
// private:
// struct Data
// {
// std::string muxId;
// RTC::RtpCodecMime mediaMime;
// uint32_t mediaSsrc = 0;
// // TODO: Add much more and move supportedPayloadTypes here.
// };
private:
// Container of RTP packets to retransmit.
static std::vector<RTC::RtpPacket*> rtpRetransmissionContainer;
public:
RtpSender(Listener* listener, Channel::Notifier* notifier, uint32_t rtpSenderId, RTC::Media::Kind kind);
private:
virtual ~RtpSender();
void Close();
public:
void Destroy();
Json::Value toJson() const;
void HandleRequest(Channel::Request* request);
void SetPeerCapabilities(RTC::RtpCapabilities* peerCapabilities);
......
......@@ -34,9 +34,12 @@ namespace RTC
public:
SrtpSession(Type type, Profile profile, uint8_t* key, size_t key_len);
private:
~SrtpSession();
void Close();
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,8 +18,11 @@ namespace RTC
public:
TcpConnection(Listener* listener, size_t bufferSize);
virtual ~TcpConnection();
private:
virtual ~TcpConnection() {};
public:
void Send(const uint8_t* data, size_t len);
/* Pure virtual methods inherited from ::TcpConnection. */
......
......@@ -39,6 +39,8 @@ namespace RTC
public:
TcpServer(Listener* listener, RTC::TcpConnection::Listener* connListener, int address_family);
private:
virtual ~TcpServer() {};
/* Pure virtual methods inherited from ::TcpServer. */
......
......@@ -46,9 +46,12 @@ namespace RTC
public:
Transport(Listener* listener, Channel::Notifier* notifier, uint32_t transportId, Json::Value& data);
private:
virtual ~Transport();
void Close();
public:
void Destroy();
Json::Value toJson() const;
void HandleRequest(Channel::Request* request);
void AddRtpReceiver(RTC::RtpReceiver* rtpReceiver);
......
......@@ -22,7 +22,6 @@ namespace RTC
TransportTuple(RTC::UdpSocket* udpSocket, const struct sockaddr* udpRemoteAddr);
explicit TransportTuple(RTC::TcpConnection* tcpConnection);
void Close();
Json::Value toJson() const;
void Dump() const;
void StoreUdpRemoteAddress();
......@@ -44,13 +43,6 @@ namespace RTC
/* Inline methods. */
inline
void TransportTuple::Close()
{
if (this->protocol == Protocol::TCP)
this->tcpConnection->Close();
}
inline
TransportTuple::TransportTuple(RTC::UdpSocket* udpSocket, const struct sockaddr* udpRemoteAddr) :
udpSocket(udpSocket),
......
......@@ -37,6 +37,8 @@ namespace RTC
public:
UdpSocket(Listener* listener, int address_family);
private:
virtual ~UdpSocket() {};
/* Pure virtual methods inherited from ::UdpSocket. */
......
......@@ -20,7 +20,11 @@ public:
public:
explicit SignalsHandler(Listener* listener);
void Close();
private:
~SignalsHandler() {};
public:
void Destroy();
void AddSignal(int signum, std::string name);
/* Callbacks fired by UV events. */
......
......@@ -5,6 +5,10 @@
#include <string>
#include <uv.h>
// Avoid cyclic #include problem by declaring classes instead of including
// the corresponding header files.
class TcpServer;
class TcpConnection
{
public:
......@@ -26,13 +30,19 @@ public:
uint8_t store[1];
};
// Let the TcpServer class directly call the destructor of TcpConnection.
friend class TcpServer;
public:
explicit TcpConnection(size_t bufferSize);
TcpConnection& operator=(const TcpConnection&) = delete;
TcpConnection(const TcpConnection&) = delete;
protected:
virtual ~TcpConnection();
void Close();
public:
void Destroy();
virtual void Dump() const;
void Setup(Listener* listener, struct sockaddr_storage* localAddr, const std::string &localIP, uint16_t localPort);
bool IsClosing() const;
......
......@@ -15,9 +15,12 @@ public:
* uvHandle must be an already initialized and binded uv_tcp_t pointer.
*/
TcpServer(uv_tcp_t* uvHandle, int backlog);
protected:
virtual ~TcpServer();
void Close();
public:
void Destroy();
virtual void Dump() const;
bool IsClosing() const;
const struct sockaddr* GetLocalAddress() const;
......
......@@ -21,7 +21,11 @@ public:
Timer& operator=(const Timer&) = delete;
Timer(const Timer&) = delete;
void Close();
private:
~Timer() {};
public:
void Destroy();
void Start(uint64_t timeout);
void Stop();
......
......@@ -27,9 +27,12 @@ public:
explicit UdpSocket(uv_udp_t* uvHandle);
UdpSocket& operator=(const UdpSocket&) = delete;
UdpSocket(const UdpSocket&) = delete;
protected:
virtual ~UdpSocket();
void Close();
public:
void Destroy();
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);
......
......@@ -20,9 +20,12 @@ public:
UnixStreamSocket(int fd, size_t bufferSize);
UnixStreamSocket& operator=(const UnixStreamSocket&) = delete;
UnixStreamSocket(const UnixStreamSocket&) = delete;
protected:
virtual ~UnixStreamSocket();
void Close();
public:
void Destroy();
bool IsClosing() const;
void Write(const uint8_t* data, size_t len);
void Write(const std::string &data);
......
......@@ -14,18 +14,6 @@ namespace Channel
MS_TRACE();
}
Notifier::~Notifier()
{
MS_TRACE();
}
void Notifier::Close()
{
MS_TRACE();
delete this;
}
void Notifier::Emit(uint32_t targetId, std::string event)
{
MS_TRACE();
......
......@@ -56,7 +56,7 @@ void Loop::Close()
// Close the SignalsHandler.
if (this->signalsHandler)
this->signalsHandler->Close();
this->signalsHandler->Destroy();
// Close all the Rooms.
// NOTE: Upon Room closure the onRoomClosed() method is called which
......@@ -67,15 +67,15 @@ void Loop::Close()
RTC::Room* room = it->second;
it = this->rooms.erase(it);
room->Close();
room->Destroy();
}
// Close the Notifier.
this->notifier->Close();
// Delete the Notifier.
delete this->notifier;
// Close the Channel socket.
if (this->channel)
this->channel->Close();
this->channel->Destroy();
}
RTC::Room* Loop::GetRoomFromRequest(Channel::Request* request, uint32_t* roomId)
......
......@@ -541,7 +541,7 @@ namespace RTC
}
}
void DtlsTransport::Close()
void DtlsTransport::Destroy()
{
MS_TRACE();
......@@ -552,8 +552,8 @@ namespace RTC
SendPendingOutgoingDtlsData();
}
// Close the DTLS timer.
this->timer->Close();
// Destroy the DTLS timer.
this->timer->Destroy();
delete this;
}
......
......@@ -24,7 +24,7 @@ namespace RTC
MS_DEBUG_TAG(ice, "[usernameFragment:%s, password:%s]", this->usernameFragment.c_str(), this->password.c_str());
}
void IceServer::Close()
void IceServer::Destroy()
{
MS_TRACE();
......
......@@ -37,11 +37,11 @@ namespace RTC
{
MS_TRACE();
// Close the RTCP timer.
this->timer->Close();
// Destroy the RTCP timer.
this->timer->Destroy();
}
void Peer::Close()
void Peer::Destroy()
{
MS_TRACE();
......@@ -55,7 +55,7 @@ namespace RTC
RTC::RtpReceiver* rtpReceiver = it->second;
it = this->rtpReceivers.erase(it);
rtpReceiver->Close();
rtpReceiver->Destroy();
}
// Close all the RtpSenders.
......@@ -64,18 +64,18 @@ namespace RTC
RTC::RtpSender* rtpSender = it->second;
it = this->rtpSenders.erase(it);
rtpSender->Close();
rtpSender->Destroy();
}
// Close all the Transports.
// NOTE: It is critical to close Transports after RtpReceivers/RtpSenders
// because RtcReceiver.Close() fires an event in the Transport.
// because RtcReceiver.Destroy() fires an event in the Transport.
for (auto it = this->transports.begin(); it != this->transports.end();)
{
RTC::Transport* transport = it->second;
it = this->transports.erase(it);
transport->Close();
transport->Destroy();
}
// Notify.
......@@ -156,7 +156,7 @@ namespace RTC
uint32_t peerId = this->peerId;
#endif
Close();
Destroy();
MS_DEBUG_DEV("Peer closed [peerId:%" PRIu32 "]", peerId);
......
......@@ -110,7 +110,7 @@ namespace RTC
MS_TRACE();
}
void Room::Close()
void Room::Destroy()
{
MS_TRACE();
......@@ -127,7 +127,7 @@ namespace RTC
RTC::Peer* peer = it->second;
it = this->peers.erase(it);
peer->Close();
peer->Destroy();
}
// Notify.
......@@ -211,7 +211,7 @@ namespace RTC
uint32_t roomId = this->roomId;
#endif
Close();
Destroy();
MS_DEBUG_DEV("Room closed [roomId:%" PRIu32 "]", roomId);
......@@ -569,7 +569,7 @@ namespace RTC
// RtpSenders associated to the closed RtpReceiver.
if (this->mapRtpReceiverRtpSenders.find(rtpReceiver) != this->mapRtpReceiverRtpSenders.end())
{
// Make a copy of the set of RtpSenders given that Close() will be called
// Make a copy of the set of RtpSenders given that Destroy() will be called
// in all of them, producing onPeerRtpSenderClosed() that will remove it
// from the map.
auto rtpSenders = this->mapRtpReceiverRtpSenders[rtpReceiver];
......@@ -577,7 +577,7 @@ namespace RTC
// Safely iterate the copy of the set.
for (auto& rtpSender : rtpSenders)
{
rtpSender->Close();
rtpSender->Destroy();
}
// Finally delete the RtpReceiver entry in the map.
......
......@@ -41,7 +41,7 @@ namespace RTC
ClearRtpStreams();
}
void RtpReceiver::Close()
void RtpReceiver::Destroy()
{
MS_TRACE();
......@@ -114,7 +114,7 @@ namespace RTC
uint32_t rtpReceiverId = this->rtpReceiverId;
#endif
Close();
Destroy();
MS_DEBUG_DEV("RtpReceiver closed [rtpReceiverId:%" PRIu32 "]", rtpReceiverId);
......
......@@ -43,7 +43,7 @@ namespace RTC
delete this->rtpStream;
}
void RtpSender::Close()
void RtpSender::Destroy()
{
MS_TRACE();
......
......@@ -113,7 +113,7 @@ namespace RTC
}
}
void SrtpSession::Close()
void SrtpSession::Destroy()
{
MS_TRACE();
......
......@@ -17,11 +17,6 @@ namespace RTC
MS_TRACE();
}
TcpConnection::~TcpConnection()
{
MS_TRACE();
}
void TcpConnection::userOnTcpConnectionRead()
{
MS_TRACE();
......@@ -50,7 +45,7 @@ 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 (Close() called) so we cannot call
// a DTLS Close Alert this would be closed (Destroy() called) so we cannot call
// our listeners anymore.
if (IsClosing())
return;
......@@ -125,7 +120,7 @@ namespace RTC
MS_WARN_DEV("no more space in the buffer for the unfinished frame being parsed, closing the connection");
// Close the socket.
Close();
Destroy();
}
}
// The buffer is not full.
......
......@@ -220,7 +220,7 @@ namespace RTC
// Allow just MAX_TCP_CONNECTIONS_PER_SERVER.
if (GetNumConnections() > MAX_TCP_CONNECTIONS_PER_SERVER)
connection->Close();
connection->Destroy();
}
void TcpServer::userOnTcpConnectionClosed(::TcpConnection* connection, bool is_closed_by_peer)
......
......@@ -191,7 +191,7 @@ namespace RTC
// Ensure there is at least one IP:port binding.
if (!this->udpSockets.size() && !this->tcpServers.size())
{
Close();
Destroy();
MS_THROW_ERROR("could not open any IP:port");
}
......@@ -199,7 +199,7 @@ namespace RTC
// Create a DTLS agent.
this->dtlsTransport = new RTC::DtlsTransport(this);
// Hack to avoid that Close() above attempts to delete this.
// Hack to avoid that Destroy() above attempts to delete this.
this->allocated = true;
}
......@@ -208,7 +208,7 @@ namespace RTC
MS_TRACE();
}
void Transport::Close()
void Transport::Destroy()
{
MS_TRACE();
......@@ -217,23 +217,23 @@ namespace RTC
Json::Value event_data(Json::objectValue);
if (this->srtpRecvSession)
this->srtpRecvSession->Close();
this->srtpRecvSession->Destroy();
if (this->srtpSendSession)
this->srtpSendSession->Close();
this->srtpSendSession->Destroy();
if (this->dtlsTransport)
this->dtlsTransport->Close();
this->dtlsTransport->Destroy();
if (this->iceServer)
this->iceServer->Close();
this->iceServer->Destroy();
for (auto socket : this->udpSockets)
socket->Close();
socket->Destroy();
this->udpSockets.clear();
for (auto server : this->tcpServers)
server->Close();
server->Destroy();
this->tcpServers.clear();
this->selectedTuple = nullptr;
......@@ -382,7 +382,7 @@ namespace RTC
uint32_t transportId = this->transportId;
#endif
Close();
Destroy();
MS_DEBUG_DEV("Transport closed [transportId:%" PRIu32 "]", transportId);
request->Accept();
......@@ -968,7 +968,7 @@ namespace RTC
this->notifier->Emit(this->transportId, "icestatechange", event_data);
// This is a fatal error so close the transport.
Close();
Destroy();
}
void Transport::onDtlsConnecting(RTC::DtlsTransport* dtlsTransport)
......@@ -1005,12 +1005,12 @@ namespace RTC
// Close it if it was already set and update it.
if (this->srtpSendSession)
{
this->srtpSendSession->Close();
this->srtpSendSession->Destroy();
this->srtpSendSession = nullptr;
}
if (this->srtpRecvSession)
{
this->srtpRecvSession->Close();
this->srtpRecvSession->Destroy();
this->srtpRecvSession = nullptr;
}
......@@ -1033,7 +1033,7 @@ namespace RTC
{
MS_ERROR("error creating SRTP receiving session: %s", error.what());
this->srtpSendSession->Close();
this->srtpSendSession->Destroy();
this->srtpSendSession = nullptr;
}
......@@ -1062,7 +1062,7 @@ namespace RTC
this->notifier->Emit(this->transportId, "dtlsstatechange", event_data);
// This is a fatal error so close the transport.
Close();
Destroy();
}
void Transport::onDtlsClosed(RTC::DtlsTransport* dtlsTransport)
......@@ -1083,7 +1083,7 @@ namespace RTC
this->notifier->Emit(this->transportId, "dtlsstatechange", event_data);
// This is a fatal error so close the transport.
Close();
Destroy();
}
void Transport::onOutgoingDtlsData(RTC::DtlsTransport* dtlsTransport, const uint8_t* data, size_t len)
......
......@@ -52,7 +52,7 @@ void SignalsHandler::AddSignal(int signum, std::string name)
this->uvHandles.push_back(uvHandle);
}
void SignalsHandler::Close()
void SignalsHandler::Destroy()
{
MS_TRACE();
......
......@@ -97,7 +97,7 @@ void TcpConnection::Setup(Listener* listener, struct sockaddr_storage* localAddr
this->localPort = localPort;
}
void TcpConnection::Close()
void TcpConnection::Destroy()
{
MS_TRACE();
......@@ -195,7 +195,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));
Close();
Destroy();
return;
}
......@@ -255,7 +255,7 @@ 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));
Close();
Destroy();
return;
}
......@@ -360,7 +360,7 @@ void TcpConnection::onUvRead(ssize_t nread, const uv_buf_t* buf)
this->isClosedByPeer = true;
// Close server side of the connection.
Close();
Destroy();
}
// Some error.
else
......@@ -370,7 +370,7 @@ void TcpConnection::onUvRead(ssize_t nread, const uv_buf_t* buf)
this->hasError = true;
// Close server side of the connection.
Close();
Destroy();
}
}
......@@ -387,7 +387,7 @@ void TcpConnection::onUvWriteError(int error)
MS_WARN_DEV("write error, closing the connection: %s", uv_strerror(error));
Close();
Destroy();
}
inline
......
......@@ -125,7 +125,7 @@ TcpServer::~TcpServer()
delete this->uvHandle;
}
void TcpServer::Close()
void TcpServer::Destroy()
{
MS_TRACE();
......@@ -147,7 +147,7 @@ void TcpServer::Close()
for (auto it = this->connections.begin(); it != this->connections.end(); ++it)
{
TcpConnection* connection = *it;
connection->Close();
connection->Destroy();
}
}
}
......@@ -232,7 +232,7 @@ void TcpServer::onUvConnection(int status)
{
MS_ERROR("cannot run the TCP connection, closing the connection: %s", error.what());
connection->Close();
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.
......@@ -261,8 +261,8 @@ void TcpServer::onTcpConnectionClosed(TcpConnection* connection, bool is_closed_
// NOTE:
// Worst scenario is that in which this is the latest connection,
// which is remotely closed (no TcpServer.Close() was called) and the user
// call TcpServer.Close() on userOnTcpConnectionClosed() callback, so Close()
// 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.
......
......@@ -41,7 +41,7 @@ Timer::Timer(Listener* listener) :
}
}
void Timer::Close()
void Timer::Destroy()
{
MS_TRACE();
......
......@@ -151,7 +151,7 @@ UdpSocket::~UdpSocket()
delete this->uvHandle;
}
void UdpSocket::Close()
void UdpSocket::Destroy()
{
MS_TRACE();
......
......@@ -107,7 +107,7 @@ UnixStreamSocket::~UnixStreamSocket()
delete[] this->buffer;
}
void UnixStreamSocket::Close()
void UnixStreamSocket::Destroy()
{
MS_TRACE_STD();
......@@ -174,7 +174,7 @@ void UnixStreamSocket::Write(const uint8_t* data, size_t len)
{
MS_ERROR_STD("uv_try_write() failed, closing the socket: %s", uv_strerror(written));
Close();
Destroy();
return;
}
......@@ -241,7 +241,7 @@ void UnixStreamSocket::onUvRead(ssize_t nread, const uv_buf_t* buf)
this->isClosedByPeer = true;
// Close local side of the pipe.
Close();
Destroy();
}
// Some error.
else
......@@ -251,7 +251,7 @@ void UnixStreamSocket::onUvRead(ssize_t nread, const uv_buf_t* buf)
this->hasError = true;
// Close the socket.
Close();
Destroy();
}
}
......@@ -268,7 +268,7 @@ void UnixStreamSocket::onUvWriteError(int error)
MS_ERROR_STD("write error, closing the pipe: %s", uv_strerror(error));
Close();
Destroy();
}
inline
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论