Skip to content

Commit

Permalink
For #1657, refine the context for disposing
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Nov 5, 2020
1 parent b492d59 commit fc21b31
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 12 deletions.
7 changes: 6 additions & 1 deletion trunk/src/app/srs_app_caster_flv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ SrsAppCasterFlv::SrsAppCasterFlv(SrsConfDirective* c)
{
http_mux = new SrsHttpServeMux();
output = _srs_config->get_stream_caster_output(c);
manager = new SrsResourceManager("CasterFLV");
manager = new SrsResourceManager("CFLV");
}

SrsAppCasterFlv::~SrsAppCasterFlv()
Expand Down Expand Up @@ -259,6 +259,11 @@ srs_error_t SrsDynamicHttpConn::on_reload_http_stream_crossdomain()
return conn->set_crossdomain_enabled(v);
}

srs_error_t SrsDynamicHttpConn::on_start()
{
return srs_success;
}

srs_error_t SrsDynamicHttpConn::on_http_message(ISrsHttpMessage* msg)
{
return srs_success;
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_caster_flv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class SrsDynamicHttpConn : virtual public ISrsStartableConneciton, virtual publi
virtual srs_error_t on_reload_http_stream_crossdomain();
// Interface ISrsHttpConnOwner.
public:
virtual srs_error_t on_start();
virtual srs_error_t on_http_message(ISrsHttpMessage* msg);
virtual void on_conn_done();
// Interface ISrsResource.
Expand Down
17 changes: 9 additions & 8 deletions trunk/src/app/srs_app_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ srs_error_t SrsResourceManager::cycle()
{
srs_error_t err = srs_success;

srs_trace("%s connection manager run", label_.c_str());
srs_trace("%s: connection manager run", label_.c_str());

while (true) {
if ((err = trd->pull()) != srs_success) {
Expand Down Expand Up @@ -183,10 +183,11 @@ void SrsResourceManager::remove(ISrsResource* c)

void SrsResourceManager::do_remove(ISrsResource* c)
{
SrsContextRestore(cid_);
SrsContextRestore(_srs_context->get_id());
if (verbose_) {
_srs_context->set_id(c->get_id());
srs_trace("before dispose resource(%s), zombies=%d", c->desc().c_str(), (int)zombies_.size());
srs_trace("%s: before dispose resource(%s), zombies=%d",
label_.c_str(), c->desc().c_str(), (int)zombies_.size());
}

// Only notify when not removed(in zombies_).
Expand Down Expand Up @@ -215,7 +216,7 @@ void SrsResourceManager::do_remove(ISrsResource* c)

// Ignore if handler is unsubscribing.
if (!unsubs_.empty() && std::find(unsubs_.begin(), unsubs_.end(), h) != unsubs_.end()) {
srs_warn2(TAG_RESOURCE_UNSUB, "ignore before-dispose for %p", h);
srs_warn2(TAG_RESOURCE_UNSUB, "%s: ignore before-dispose for %p", label_.c_str(), h);
continue;
}

Expand All @@ -234,7 +235,7 @@ void SrsResourceManager::clear()

SrsContextRestore(cid_);
if (verbose_) {
srs_trace("clear zombies=%d connections", (int)zombies_.size());
srs_trace("%s: clear zombies=%d connections", label_.c_str(), (int)zombies_.size());
}

// Clear all unsubscribing handlers, if not removing any resource.
Expand Down Expand Up @@ -262,8 +263,8 @@ void SrsResourceManager::do_clear()

if (verbose_) {
_srs_context->set_id(conn->get_id());
srs_trace("disposing resource(%s), zombies=%d/%d", conn->desc().c_str(),
(int)copy.size(), (int)zombies_.size());
srs_trace("%s: disposing resource(%s), zombies=%d/%d", label_.c_str(),
conn->desc().c_str(), (int)copy.size(), (int)zombies_.size());
}

dispose(conn);
Expand Down Expand Up @@ -304,7 +305,7 @@ void SrsResourceManager::dispose(ISrsResource* c)

// Ignore if handler is unsubscribing.
if (!unsubs_.empty() && std::find(unsubs_.begin(), unsubs_.end(), h) != unsubs_.end()) {
srs_warn2(TAG_RESOURCE_UNSUB, "ignore disposing for %p", h);
srs_warn2(TAG_RESOURCE_UNSUB, "%s: ignore disposing for %p", label_.c_str(), h);
continue;
}

Expand Down
5 changes: 5 additions & 0 deletions trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,11 @@ SrsHttpApi::~SrsHttpApi()
srs_freep(conn);
}

srs_error_t SrsHttpApi::on_start()
{
return srs_success;
}

srs_error_t SrsHttpApi::on_http_message(ISrsHttpMessage* req)
{
srs_error_t err = srs_success;
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_http_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ class SrsHttpApi : virtual public ISrsStartableConneciton, virtual public ISrsHt
virtual ~SrsHttpApi();
// Interface ISrsHttpConnOwner.
public:
virtual srs_error_t on_start();
virtual srs_error_t on_http_message(ISrsHttpMessage* msg);
virtual void on_conn_done();
// Interface ISrsResource.
Expand Down
12 changes: 11 additions & 1 deletion trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ SrsHttpConn::SrsHttpConn(ISrsHttpConnOwner* handler, srs_netfd_t fd, ISrsHttpSer
clk = new SrsWallClock();
kbps = new SrsKbps(clk);
kbps->set_io(skt, skt);
trd = new SrsSTCoroutine("http", this);
trd = new SrsSTCoroutine("http", this, _srs_context->get_id());
}

SrsHttpConn::~SrsHttpConn()
Expand Down Expand Up @@ -159,6 +159,11 @@ srs_error_t SrsHttpConn::cycle()
srs_error_t SrsHttpConn::do_cycle()
{
srs_error_t err = srs_success;

// Notify the handler that we are starting to process the connection.
if ((err = handler_->on_start()) != srs_success) {
return srs_error_wrap(err, "start");
}

// set the recv timeout, for some clients never disconnect the connection.
// @see https://github.com/ossrs/srs/issues/398
Expand Down Expand Up @@ -356,6 +361,11 @@ srs_error_t SrsResponseOnlyHttpConn::on_reload_http_stream_crossdomain()
return conn->set_crossdomain_enabled(v);
}

srs_error_t SrsResponseOnlyHttpConn::on_start()
{
return srs_success;
}

srs_error_t SrsResponseOnlyHttpConn::on_http_message(ISrsHttpMessage* msg)
{
srs_error_t err = srs_success;
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/app/srs_app_http_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class ISrsHttpConnOwner
ISrsHttpConnOwner();
virtual ~ISrsHttpConnOwner();
public:
// When start the coroutine to process connection.
virtual srs_error_t on_start() = 0;
// Handle the HTTP message msg, which may be parsed partially.
// For the static service or api, discard any body.
// For the stream caster, for instance, http flv streaming, may discard the flv header or not.
Expand Down Expand Up @@ -166,6 +168,7 @@ class SrsResponseOnlyHttpConn : virtual public ISrsStartableConneciton, virtual
virtual srs_error_t on_reload_http_stream_crossdomain();
// Interface ISrsHttpConnOwner.
public:
virtual srs_error_t on_start();
virtual srs_error_t on_http_message(ISrsHttpMessage* msg);
virtual void on_conn_done();
// Extract APIs from SrsTcpConnection.
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_rtsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ SrsRtspCaster::SrsRtspCaster(SrsConfDirective* c)
output = _srs_config->get_stream_caster_output(c);
local_port_min = _srs_config->get_stream_caster_rtp_port_min(c);
local_port_max = _srs_config->get_stream_caster_rtp_port_max(c);
manager = new SrsResourceManager("CasterRTSP");
manager = new SrsResourceManager("CRTSP");
}

SrsRtspCaster::~SrsRtspCaster()
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ SrsServer::SrsServer()
pid_fd = -1;

signal_manager = new SrsSignalManager(this);
conn_manager = new SrsResourceManager("RTMP/API");
conn_manager = new SrsResourceManager("TCP", true);

handler = NULL;
ppid = ::getppid();
Expand Down

0 comments on commit fc21b31

Please sign in to comment.