From 2135b638b185315791998ee25c5310379df26d9c Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 11 Sep 2020 16:44:40 +0800 Subject: [PATCH] Merge srs_app_thread.hpp to srs_app_conn.hpp --- trunk/configure | 2 +- trunk/ide/srs_upp/srs_upp.upp | 2 - trunk/src/app/srs_app_async_call.hpp | 2 +- trunk/src/app/srs_app_caster_flv.hpp | 2 +- trunk/src/app/srs_app_config.hpp | 2 +- trunk/src/app/srs_app_conn.cpp | 63 ++++++++++++++++++ trunk/src/app/srs_app_conn.hpp | 27 +++++++- trunk/src/app/srs_app_edge.hpp | 1 - trunk/src/app/srs_app_encoder.hpp | 2 +- trunk/src/app/srs_app_forward.hpp | 1 - trunk/src/app/srs_app_gb28181.hpp | 1 - trunk/src/app/srs_app_http_conn.hpp | 2 +- trunk/src/app/srs_app_ingest.hpp | 2 +- trunk/src/app/srs_app_listener.hpp | 1 - trunk/src/app/srs_app_ng_exec.hpp | 2 +- trunk/src/app/srs_app_recv_thread.hpp | 2 +- trunk/src/app/srs_app_rtsp.hpp | 3 +- trunk/src/app/srs_app_server.cpp | 1 - trunk/src/app/srs_app_thread.cpp | 95 --------------------------- trunk/src/app/srs_app_thread.hpp | 60 ----------------- 20 files changed, 100 insertions(+), 173 deletions(-) delete mode 100755 trunk/src/app/srs_app_thread.cpp delete mode 100644 trunk/src/app/srs_app_thread.hpp diff --git a/trunk/configure b/trunk/configure index 48bf155fc..72b3e8073 100755 --- a/trunk/configure +++ b/trunk/configure @@ -267,7 +267,7 @@ if [[ $SRS_FFMPEG_FIT == YES ]]; then fi MODULE_FILES=("srs_app_server" "srs_app_conn" "srs_app_rtmp_conn" "srs_app_source" "srs_app_refer" "srs_app_hls" "srs_app_forward" "srs_app_encoder" "srs_app_http_stream" - "srs_app_thread" "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config" + "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config" "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_edge" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_http_static" diff --git a/trunk/ide/srs_upp/srs_upp.upp b/trunk/ide/srs_upp/srs_upp.upp index 283146430..3c82bed66 100755 --- a/trunk/ide/srs_upp/srs_upp.upp +++ b/trunk/ide/srs_upp/srs_upp.upp @@ -142,8 +142,6 @@ file ../../src/app/srs_app_statistic.cpp, ../../src/app/srs_app_source.hpp, ../../src/app/srs_app_source.cpp, - ../../src/app/srs_app_thread.hpp, - ../../src/app/srs_app_thread.cpp, ../../src/app/srs_app_utility.hpp, ../../src/app/srs_app_utility.cpp, utest readonly separator, diff --git a/trunk/src/app/srs_app_async_call.hpp b/trunk/src/app/srs_app_async_call.hpp index 7cc183f4a..b05fda21a 100644 --- a/trunk/src/app/srs_app_async_call.hpp +++ b/trunk/src/app/srs_app_async_call.hpp @@ -29,7 +29,7 @@ #include #include -#include +#include // The async call for http hooks, for the http hooks will switch st-thread, // so we must use isolate thread to avoid the thread corrupt, diff --git a/trunk/src/app/srs_app_caster_flv.hpp b/trunk/src/app/srs_app_caster_flv.hpp index ef1587fa5..76e04ab84 100644 --- a/trunk/src/app/srs_app_caster_flv.hpp +++ b/trunk/src/app/srs_app_caster_flv.hpp @@ -40,7 +40,7 @@ class SrsFlvDecoder; class SrsTcpClient; class SrsSimpleRtmpClient; -#include +#include #include #include #include diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 5b083784a..974dd05f5 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -34,7 +34,7 @@ #include #include -#include +#include class SrsRequest; class SrsFileWriter; diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 80c1e3a6c..d1f1949de 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -31,6 +31,69 @@ using namespace std; #include #include +SrsCoroutineManager::SrsCoroutineManager() +{ + cond = srs_cond_new(); + trd = new SrsSTCoroutine("manager", this); +} + +SrsCoroutineManager::~SrsCoroutineManager() +{ + srs_freep(trd); + srs_cond_destroy(cond); + + clear(); +} + +srs_error_t SrsCoroutineManager::start() +{ + srs_error_t err = srs_success; + + if ((err = trd->start()) != srs_success) { + return srs_error_wrap(err, "coroutine manager"); + } + + return err; +} + +srs_error_t SrsCoroutineManager::cycle() +{ + srs_error_t err = srs_success; + + while (true) { + if ((err = trd->pull()) != srs_success) { + return srs_error_wrap(err, "coroutine mansger"); + } + + srs_cond_wait(cond); + clear(); + } + + return err; +} + +void SrsCoroutineManager::remove(ISrsConnection* c) +{ + if (::find(conns.begin(), conns.end(), c) == conns.end()) { + conns.push_back(c); + } + srs_cond_signal(cond); +} + +void SrsCoroutineManager::clear() +{ + // To prevent thread switch when delete connection, + // we copy all connections then free one by one. + vector copy; + copy.swap(conns); + + vector::iterator it; + for (it = copy.begin(); it != copy.end(); ++it) { + ISrsConnection* conn = *it; + srs_freep(conn); + } +} + SrsConnection::SrsConnection(IConnectionManager* cm, srs_netfd_t c, string cip, int cport) { manager = cm; diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index 551976c0a..14df865a3 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -27,15 +27,40 @@ #include #include +#include #include -#include #include #include #include class SrsWallClock; +// The coroutine manager use a thread to delete a connection, which will stop the service +// thread, for example, when the RTMP connection thread cycle terminated, it will notify +// the manager(the server) to remove the connection from list of server and push it to +// the manager thread to delete it, finally the thread of connection will stop. +class SrsCoroutineManager : virtual public ISrsCoroutineHandler, virtual public IConnectionManager +{ +private: + SrsCoroutine* trd; + std::vector conns; + srs_cond_t cond; +public: + SrsCoroutineManager(); + virtual ~SrsCoroutineManager(); +public: + srs_error_t start(); +// Interface ISrsCoroutineHandler +public: + virtual srs_error_t cycle(); +// Interface IConnectionManager +public: + virtual void remove(ISrsConnection* c); +private: + void clear(); +}; + // The basic connection of SRS, // all connections accept from listener must extends from this base class, // server will add the connection to manager, and delete it when remove. diff --git a/trunk/src/app/srs_app_edge.hpp b/trunk/src/app/srs_app_edge.hpp index 4c7249614..19ba88c1e 100644 --- a/trunk/src/app/srs_app_edge.hpp +++ b/trunk/src/app/srs_app_edge.hpp @@ -27,7 +27,6 @@ #include #include -#include #include diff --git a/trunk/src/app/srs_app_encoder.hpp b/trunk/src/app/srs_app_encoder.hpp index 1969547b8..2beb64612 100644 --- a/trunk/src/app/srs_app_encoder.hpp +++ b/trunk/src/app/srs_app_encoder.hpp @@ -29,7 +29,7 @@ #include #include -#include +#include class SrsConfDirective; class SrsRequest; diff --git a/trunk/src/app/srs_app_forward.hpp b/trunk/src/app/srs_app_forward.hpp index 9b50f89d7..a4adcfc15 100644 --- a/trunk/src/app/srs_app_forward.hpp +++ b/trunk/src/app/srs_app_forward.hpp @@ -29,7 +29,6 @@ #include #include -#include class ISrsProtocolReadWriter; class SrsSharedPtrMessage; diff --git a/trunk/src/app/srs_app_gb28181.hpp b/trunk/src/app/srs_app_gb28181.hpp index b50685070..b392883cd 100644 --- a/trunk/src/app/srs_app_gb28181.hpp +++ b/trunk/src/app/srs_app_gb28181.hpp @@ -33,7 +33,6 @@ #include #include -#include #include #include #include diff --git a/trunk/src/app/srs_app_http_conn.hpp b/trunk/src/app/srs_app_http_conn.hpp index 21c754bdd..67c489088 100644 --- a/trunk/src/app/srs_app_http_conn.hpp +++ b/trunk/src/app/srs_app_http_conn.hpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include diff --git a/trunk/src/app/srs_app_ingest.hpp b/trunk/src/app/srs_app_ingest.hpp index 4118d77fb..d392a24e3 100644 --- a/trunk/src/app/srs_app_ingest.hpp +++ b/trunk/src/app/srs_app_ingest.hpp @@ -28,7 +28,7 @@ #include -#include +#include #include class SrsFFMPEG; diff --git a/trunk/src/app/srs_app_listener.hpp b/trunk/src/app/srs_app_listener.hpp index 19f7ef2af..758c09e73 100644 --- a/trunk/src/app/srs_app_listener.hpp +++ b/trunk/src/app/srs_app_listener.hpp @@ -32,7 +32,6 @@ #include #include -#include struct sockaddr; diff --git a/trunk/src/app/srs_app_ng_exec.hpp b/trunk/src/app/srs_app_ng_exec.hpp index 901aa9535..c00fcdece 100644 --- a/trunk/src/app/srs_app_ng_exec.hpp +++ b/trunk/src/app/srs_app_ng_exec.hpp @@ -29,7 +29,7 @@ #include #include -#include +#include class SrsRequest; class SrsPithyPrint; diff --git a/trunk/src/app/srs_app_recv_thread.hpp b/trunk/src/app/srs_app_recv_thread.hpp index 98546f091..c61cf33ed 100644 --- a/trunk/src/app/srs_app_recv_thread.hpp +++ b/trunk/src/app/srs_app_recv_thread.hpp @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include #include diff --git a/trunk/src/app/srs_app_rtsp.hpp b/trunk/src/app/srs_app_rtsp.hpp index 6fac84dae..9d51ab615 100644 --- a/trunk/src/app/srs_app_rtsp.hpp +++ b/trunk/src/app/srs_app_rtsp.hpp @@ -31,8 +31,8 @@ #include #include -#include #include +#include class SrsStSocket; class SrsRtspConn; @@ -51,6 +51,7 @@ class SrsAudioFrame; class SrsSimpleStream; class SrsPithyPrint; class SrsSimpleRtmpClient; +class SrsCoroutineManager; // A rtp connection which transport a stream. class SrsRtpConn: public ISrsUdpHandler diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 8bcdf897e..15decf09e 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -52,7 +52,6 @@ using namespace std; #include #include #include -#include #include #include #include diff --git a/trunk/src/app/srs_app_thread.cpp b/trunk/src/app/srs_app_thread.cpp deleted file mode 100755 index 4b3310f35..000000000 --- a/trunk/src/app/srs_app_thread.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2013-2020 Winlin - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include - -#include -#include - -#include -#include -using namespace std; - -SrsCoroutineManager::SrsCoroutineManager() -{ - cond = srs_cond_new(); - trd = new SrsSTCoroutine("manager", this); -} - -SrsCoroutineManager::~SrsCoroutineManager() -{ - srs_freep(trd); - srs_cond_destroy(cond); - - clear(); -} - -srs_error_t SrsCoroutineManager::start() -{ - srs_error_t err = srs_success; - - if ((err = trd->start()) != srs_success) { - return srs_error_wrap(err, "coroutine manager"); - } - - return err; -} - -srs_error_t SrsCoroutineManager::cycle() -{ - srs_error_t err = srs_success; - - while (true) { - if ((err = trd->pull()) != srs_success) { - return srs_error_wrap(err, "coroutine mansger"); - } - - srs_cond_wait(cond); - clear(); - } - - return err; -} - -void SrsCoroutineManager::remove(ISrsConnection* c) -{ - if (::find(conns.begin(), conns.end(), c) == conns.end()) { - conns.push_back(c); - } - srs_cond_signal(cond); -} - -void SrsCoroutineManager::clear() -{ - // To prevent thread switch when delete connection, - // we copy all connections then free one by one. - vector copy; - copy.swap(conns); - - vector::iterator it; - for (it = copy.begin(); it != copy.end(); ++it) { - ISrsConnection* conn = *it; - srs_freep(conn); - } -} - diff --git a/trunk/src/app/srs_app_thread.hpp b/trunk/src/app/srs_app_thread.hpp deleted file mode 100644 index 8aa384a3d..000000000 --- a/trunk/src/app/srs_app_thread.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2013-2020 Winlin - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef SRS_APP_THREAD_HPP -#define SRS_APP_THREAD_HPP - -#include - -#include - -#include -#include - -// The coroutine manager use a thread to delete a connection, which will stop the service -// thread, for example, when the RTMP connection thread cycle terminated, it will notify -// the manager(the server) to remove the connection from list of server and push it to -// the manager thread to delete it, finally the thread of connection will stop. -class SrsCoroutineManager : virtual public ISrsCoroutineHandler, virtual public IConnectionManager -{ -private: - SrsCoroutine* trd; - std::vector conns; - srs_cond_t cond; -public: - SrsCoroutineManager(); - virtual ~SrsCoroutineManager(); -public: - srs_error_t start(); -// Interface ISrsCoroutineHandler -public: - virtual srs_error_t cycle(); -// Interface IConnectionManager -public: - virtual void remove(ISrsConnection* c); -private: - void clear(); -}; - -#endif -