Skip to content

Commit

Permalink
fix crash which caused by tcp conn destroyed but rtc conn not be dest…
Browse files Browse the repository at this point in the history
…royed
  • Loading branch information
lipeng19811218 committed Sep 4, 2022
1 parent 07339e1 commit 057f159
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
41 changes: 28 additions & 13 deletions trunk/src/app/srs_app_rtc_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,25 @@ ISrsKbpsDelta* SrsRtcNetworks::delta()

ISrsRtcNetwork::ISrsRtcNetwork()
{
establelished_ = false;
}

ISrsRtcNetwork::~ISrsRtcNetwork()
{
}

bool ISrsRtcNetwork::is_establelished()
{
return establelished_;
}

SrsRtcDummyNetwork::SrsRtcDummyNetwork()
{
establelished_ = true;
}

SrsRtcDummyNetwork::~SrsRtcDummyNetwork()
{
}

bool SrsRtcDummyNetwork::is_establelished()
{
return true;
}

srs_error_t SrsRtcDummyNetwork::on_connection_established()
{
return srs_success;
Expand Down Expand Up @@ -202,7 +200,6 @@ srs_error_t SrsRtcUdpNetwork::start_active_handshake()

srs_error_t SrsRtcUdpNetwork::on_dtls(char* data, int nb_data)
{
establelished_ = true;
// Update stat when we received data.
delta_->add_delta(nb_data, 0);

Expand All @@ -219,15 +216,15 @@ srs_error_t SrsRtcUdpNetwork::on_connection_established()
srs_error_t err = srs_success;

// If DTLS done packet received many times, such as ARQ, ignore.
if(SrsRtcNetworkStateClosed == state_) {
if(SrsRtcNetworkStateEstablished == state_) {
return err;
}

if ((err = conn_->on_connection_established()) != srs_success) {
return srs_error_wrap(err, "udp");
}

state_ = SrsRtcNetworkStateClosed;
state_ = SrsRtcNetworkStateEstablished;
return err;
}

Expand Down Expand Up @@ -303,6 +300,11 @@ void SrsRtcUdpNetwork::set_state(SrsRtcNetworkState state)
state_ = state;
}

bool SrsRtcUdpNetwork::is_establelished()
{
return state_ == SrsRtcNetworkStateEstablished;
}

string SrsRtcUdpNetwork::get_peer_ip()
{
srs_assert(sendonly_skt_);
Expand Down Expand Up @@ -464,15 +466,15 @@ srs_error_t SrsRtcTcpNetwork::on_connection_established()
srs_error_t err = srs_success;

// If DTLS done packet received many times, such as ARQ, ignore.
if(SrsRtcNetworkStateClosed == state_) {
if(SrsRtcNetworkStateEstablished == state_) {
return err;
}

if ((err = conn_->on_connection_established()) != srs_success) {
return srs_error_wrap(err, "udp");
}

state_ = SrsRtcNetworkStateClosed;
state_ = SrsRtcNetworkStateEstablished;
return err;
}

Expand Down Expand Up @@ -585,7 +587,6 @@ srs_error_t SrsRtcTcpNetwork::start_active_handshake()

srs_error_t SrsRtcTcpNetwork::on_dtls(char* data, int nb_data)
{
establelished_ = true;
// Update stat when we received data.
delta_->add_delta(nb_data, 0);

Expand Down Expand Up @@ -654,6 +655,11 @@ void SrsRtcTcpNetwork::set_state(SrsRtcNetworkState state)
state_ = state;
}

bool SrsRtcTcpNetwork::is_establelished()
{
return state_ == SrsRtcNetworkStateEstablished;
}

std::string SrsRtcTcpNetwork::get_peer_ip()
{
return peer_ip_;
Expand Down Expand Up @@ -701,6 +707,11 @@ void SrsRtcTcpNetwork::set_peer_id(const std::string& ip, int port)
peer_port_ = port;
}

void SrsRtcTcpNetwork::dispose()
{
state_ = SrsRtcNetworkStateClosed;
}

SrsRtcTcpConn::SrsRtcTcpConn(ISrsProtocolReadWriter* skt, std::string cip, int port, ISrsResourceManager* cm)
{
manager_ = cm;
Expand Down Expand Up @@ -761,6 +772,10 @@ srs_error_t SrsRtcTcpConn::cycle()
// Because we use manager to manage this object,
// not the http connection object, so we must remove it here.
manager_->remove(this);
if(session_) {
session_->tcp()->set_state(SrsRtcNetworkStateClosed);
_srs_rtc_manager->remove(session_);
}

// For HTTP-API timeout, we think it's done successfully,
// because there may be no request or response for HTTP-API.
Expand Down
9 changes: 5 additions & 4 deletions trunk/src/app/srs_app_rtc_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ class SrsRtcNetworks
// For DTLS or Session to call network service.
class ISrsRtcNetwork : public ISrsStreamWriter
{
protected:
bool establelished_;

public:
ISrsRtcNetwork();
virtual ~ISrsRtcNetwork();
Expand All @@ -97,7 +94,7 @@ class ISrsRtcNetwork : public ISrsStreamWriter
// Protect RTCP packet by SRTP context.
virtual srs_error_t protect_rtcp(void* packet, int* nb_cipher) = 0;
public:
bool is_establelished();
virtual bool is_establelished() = 0;
};

// Dummy networks
Expand All @@ -114,6 +111,7 @@ class SrsRtcDummyNetwork : public ISrsRtcNetwork
public:
virtual srs_error_t protect_rtp(void* packet, int* nb_cipher);
virtual srs_error_t protect_rtcp(void* packet, int* nb_cipher);
virtual bool is_establelished();
// Interface ISrsStreamWriter.
public:
virtual srs_error_t write(void* buf, size_t size, ssize_t* nwrite);
Expand Down Expand Up @@ -164,6 +162,7 @@ class SrsRtcUdpNetwork : public ISrsRtcNetwork
public:
// Connection level state machine, for ARQ of UDP packets.
void set_state(SrsRtcNetworkState state);
virtual bool is_establelished();
// ICE reflexive address functions.
std::string get_peer_ip();
int get_peer_port();
Expand Down Expand Up @@ -218,6 +217,7 @@ class SrsRtcTcpNetwork: public ISrsRtcNetwork
public:
// Connection level state machine, for ARQ of UDP packets.
void set_state(SrsRtcNetworkState state);
virtual bool is_establelished();
// ICE reflexive address functions.
std::string get_peer_ip();
int get_peer_port();
Expand All @@ -226,6 +226,7 @@ class SrsRtcTcpNetwork: public ISrsRtcNetwork
virtual srs_error_t write(void* buf, size_t size, ssize_t* nwrite);
public:
void set_peer_id(const std::string& ip, int port);
void dispose();
};

// For WebRTC over TCP.
Expand Down

0 comments on commit 057f159

Please # to comment.