winlin

support reload the removed vhost

@@ -83,7 +83,7 @@ vhost __defaultVhost__ { @@ -83,7 +83,7 @@ vhost __defaultVhost__ {
83 vhost dev { 83 vhost dev {
84 enabled on; 84 enabled on;
85 gop_cache on; 85 gop_cache on;
86 - forward 127.0.0.1:19350; 86 + #forward 127.0.0.1:19350;
87 hls { 87 hls {
88 hls off; 88 hls off;
89 hls_path ./objs/nginx/html; 89 hls_path ./objs/nginx/html;
@@ -100,7 +100,7 @@ vhost dev { @@ -100,7 +100,7 @@ vhost dev {
100 on_stop http://127.0.0.1:8085/api/v1/sessions; 100 on_stop http://127.0.0.1:8085/api/v1/sessions;
101 } 101 }
102 transcode { 102 transcode {
103 - enabled on; 103 + enabled off;
104 ffmpeg ./objs/ffmpeg/bin/ffmpeg; 104 ffmpeg ./objs/ffmpeg/bin/ffmpeg;
105 engine dev { 105 engine dev {
106 enabled on; 106 enabled on;
@@ -55,6 +55,8 @@ SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) @@ -55,6 +55,8 @@ SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
55 #ifdef SRS_HTTP 55 #ifdef SRS_HTTP
56 http_hooks = new SrsHttpHooks(); 56 http_hooks = new SrsHttpHooks();
57 #endif 57 #endif
  58 +
  59 + config->subscribe(this);
58 } 60 }
59 61
60 SrsClient::~SrsClient() 62 SrsClient::~SrsClient()
@@ -67,6 +69,8 @@ SrsClient::~SrsClient() @@ -67,6 +69,8 @@ SrsClient::~SrsClient()
67 #ifdef SRS_HTTP 69 #ifdef SRS_HTTP
68 srs_freep(http_hooks); 70 srs_freep(http_hooks);
69 #endif 71 #endif
  72 +
  73 + config->unsubscribe(this);
70 } 74 }
71 75
72 // TODO: return detail message when error for client. 76 // TODO: return detail message when error for client.
@@ -113,6 +117,18 @@ int SrsClient::do_cycle() @@ -113,6 +117,18 @@ int SrsClient::do_cycle()
113 117
114 return ret; 118 return ret;
115 } 119 }
  120 +
  121 +int SrsClient::on_reload_vhost_removed(SrsConfDirective* vhost)
  122 +{
  123 + int ret = ERROR_SUCCESS;
  124 +
  125 + // if the vhost connected is removed, disconnect the client.
  126 + if (req->vhost == vhost->arg0()) {
  127 + srs_close_stfd(stfd);
  128 + }
  129 +
  130 + return ret;
  131 +}
116 132
117 int SrsClient::service_cycle() 133 int SrsClient::service_cycle()
118 { 134 {
@@ -31,6 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,6 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 #include <srs_core_conn.hpp> 33 #include <srs_core_conn.hpp>
  34 +#include <srs_core_reload.hpp>
34 35
35 class SrsRtmp; 36 class SrsRtmp;
36 class SrsRequest; 37 class SrsRequest;
@@ -46,7 +47,7 @@ class SrsHttpHooks; @@ -46,7 +47,7 @@ class SrsHttpHooks;
46 /** 47 /**
47 * the client provides the main logic control for RTMP clients. 48 * the client provides the main logic control for RTMP clients.
48 */ 49 */
49 -class SrsClient : public SrsConnection 50 +class SrsClient : public SrsConnection, public ISrsReloadHandler
50 { 51 {
51 private: 52 private:
52 char* ip; 53 char* ip;
@@ -62,6 +63,9 @@ public: @@ -62,6 +63,9 @@ public:
62 virtual ~SrsClient(); 63 virtual ~SrsClient();
63 protected: 64 protected:
64 virtual int do_cycle(); 65 virtual int do_cycle();
  66 +// interface ISrsReloadHandler
  67 +public:
  68 + virtual int on_reload_vhost_removed(SrsConfDirective* vhost);
65 private: 69 private:
66 // when valid and connected to vhost/app, service the client. 70 // when valid and connected to vhost/app, service the client.
67 virtual int service_cycle(); 71 virtual int service_cycle();
@@ -198,6 +198,19 @@ SrsConfDirective* SrsConfDirective::get(string _name) @@ -198,6 +198,19 @@ SrsConfDirective* SrsConfDirective::get(string _name)
198 return NULL; 198 return NULL;
199 } 199 }
200 200
  201 +SrsConfDirective* SrsConfDirective::get(string _name, string _arg0)
  202 +{
  203 + std::vector<SrsConfDirective*>::iterator it;
  204 + for (it = directives.begin(); it != directives.end(); ++it) {
  205 + SrsConfDirective* directive = *it;
  206 + if (directive->name == _name && directive->arg0() == _arg0) {
  207 + return directive;
  208 + }
  209 + }
  210 +
  211 + return NULL;
  212 +}
  213 +
201 int SrsConfDirective::parse(const char* filename) 214 int SrsConfDirective::parse(const char* filename)
202 { 215 {
203 int ret = ERROR_SUCCESS; 216 int ret = ERROR_SUCCESS;
@@ -465,6 +478,7 @@ int SrsConfig::reload() @@ -465,6 +478,7 @@ int SrsConfig::reload()
465 } 478 }
466 srs_trace("reload listen success."); 479 srs_trace("reload listen success.");
467 } 480 }
  481 +
468 // merge config: pithy_print 482 // merge config: pithy_print
469 if (!srs_directive_equals(root->get("pithy_print"), old_root->get("pithy_print"))) { 483 if (!srs_directive_equals(root->get("pithy_print"), old_root->get("pithy_print"))) {
470 for (it = subscribes.begin(); it != subscribes.end(); ++it) { 484 for (it = subscribes.begin(); it != subscribes.end(); ++it) {
@@ -476,6 +490,46 @@ int SrsConfig::reload() @@ -476,6 +490,46 @@ int SrsConfig::reload()
476 } 490 }
477 srs_trace("reload pithy_print success."); 491 srs_trace("reload pithy_print success.");
478 } 492 }
  493 +
  494 + // merge config: vhost added, directly supported.
  495 +
  496 + // merge config: vhost removed/disabled/modified.
  497 + for (int i = 0; i < (int)old_root->directives.size(); i++) {
  498 + SrsConfDirective* old_vhost = old_root->at(i);
  499 + // only process vhost directives.
  500 + if (old_vhost->name != "vhost") {
  501 + continue;
  502 + }
  503 +
  504 + SrsConfDirective* new_vhost = root->get("vhost", old_vhost->arg0());
  505 + // ignore if absolutely equal
  506 + if (new_vhost && srs_directive_equals(old_vhost, new_vhost)) {
  507 + continue;
  508 + }
  509 + // ignore if enable the new vhost when old vhost is disabled.
  510 + if (get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {
  511 + continue;
  512 + }
  513 + // ignore if both old and new vhost are disabled.
  514 + if (!get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {
  515 + continue;
  516 + }
  517 +
  518 + // merge config: vhost removed/disabled.
  519 + if (!get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {
  520 + srs_trace("vhost %s disabled, reload it.", old_vhost->name.c_str());
  521 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  522 + ISrsReloadHandler* subscribe = *it;
  523 + if ((ret = subscribe->on_reload_vhost_removed(old_vhost)) != ERROR_SUCCESS) {
  524 + srs_error("notify subscribes pithy_print remove vhost failed. ret=%d", ret);
  525 + return ret;
  526 + }
  527 + }
  528 + srs_trace("reload remove vhost success.");
  529 + }
  530 +
  531 + // merge config: vhost modified.
  532 + }
479 533
480 // TODO: suppor reload hls/forward/ffmpeg/http 534 // TODO: suppor reload hls/forward/ffmpeg/http
481 535
@@ -785,12 +839,17 @@ SrsConfDirective* SrsConfig::get_vhost_on_stop(string vhost) @@ -785,12 +839,17 @@ SrsConfDirective* SrsConfig::get_vhost_on_stop(string vhost)
785 bool SrsConfig::get_vhost_enabled(string vhost) 839 bool SrsConfig::get_vhost_enabled(string vhost)
786 { 840 {
787 SrsConfDirective* vhost_conf = get_vhost(vhost); 841 SrsConfDirective* vhost_conf = get_vhost(vhost);
  842 +
  843 + return get_vhost_enabled(vhost_conf);
  844 +}
788 845
789 - if (!vhost_conf) {  
790 - return true; 846 +bool SrsConfig::get_vhost_enabled(SrsConfDirective* vhost)
  847 +{
  848 + if (!vhost) {
  849 + return false;
791 } 850 }
792 851
793 - SrsConfDirective* conf = vhost_conf->get("enabled"); 852 + SrsConfDirective* conf = vhost->get("enabled");
794 if (!conf) { 853 if (!conf) {
795 return true; 854 return true;
796 } 855 }
@@ -74,6 +74,7 @@ public: @@ -74,6 +74,7 @@ public:
74 std::string arg2(); 74 std::string arg2();
75 SrsConfDirective* at(int index); 75 SrsConfDirective* at(int index);
76 SrsConfDirective* get(std::string _name); 76 SrsConfDirective* get(std::string _name);
  77 + SrsConfDirective* get(std::string _name, std::string _arg0);
77 public: 78 public:
78 virtual int parse(const char* filename); 79 virtual int parse(const char* filename);
79 public: 80 public:
@@ -113,6 +114,7 @@ private: @@ -113,6 +114,7 @@ private:
113 public: 114 public:
114 virtual SrsConfDirective* get_vhost(std::string vhost); 115 virtual SrsConfDirective* get_vhost(std::string vhost);
115 virtual bool get_vhost_enabled(std::string vhost); 116 virtual bool get_vhost_enabled(std::string vhost);
  117 + virtual bool get_vhost_enabled(SrsConfDirective* vhost);
116 virtual SrsConfDirective* get_vhost_on_connect(std::string vhost); 118 virtual SrsConfDirective* get_vhost_on_connect(std::string vhost);
117 virtual SrsConfDirective* get_vhost_on_close(std::string vhost); 119 virtual SrsConfDirective* get_vhost_on_close(std::string vhost);
118 virtual SrsConfDirective* get_vhost_on_publish(std::string vhost); 120 virtual SrsConfDirective* get_vhost_on_publish(std::string vhost);
@@ -307,6 +307,11 @@ void SrsProtocol::set_send_timeout(int64_t timeout_us) @@ -307,6 +307,11 @@ void SrsProtocol::set_send_timeout(int64_t timeout_us)
307 return skt->set_send_timeout(timeout_us); 307 return skt->set_send_timeout(timeout_us);
308 } 308 }
309 309
  310 +int64_t SrsProtocol::get_send_timeout()
  311 +{
  312 + return skt->get_send_timeout();
  313 +}
  314 +
310 int64_t SrsProtocol::get_recv_bytes() 315 int64_t SrsProtocol::get_recv_bytes()
311 { 316 {
312 return skt->get_recv_bytes(); 317 return skt->get_recv_bytes();
@@ -115,6 +115,7 @@ public: @@ -115,6 +115,7 @@ public:
115 virtual void set_recv_timeout(int64_t timeout_us); 115 virtual void set_recv_timeout(int64_t timeout_us);
116 virtual int64_t get_recv_timeout(); 116 virtual int64_t get_recv_timeout();
117 virtual void set_send_timeout(int64_t timeout_us); 117 virtual void set_send_timeout(int64_t timeout_us);
  118 + virtual int64_t get_send_timeout();
118 virtual int64_t get_recv_bytes(); 119 virtual int64_t get_recv_bytes();
119 virtual int64_t get_send_bytes(); 120 virtual int64_t get_send_bytes();
120 virtual int get_recv_kbps(); 121 virtual int get_recv_kbps();
@@ -43,3 +43,8 @@ int ISrsReloadHandler::on_reload_pithy_print() @@ -43,3 +43,8 @@ int ISrsReloadHandler::on_reload_pithy_print()
43 return ERROR_SUCCESS; 43 return ERROR_SUCCESS;
44 } 44 }
45 45
  46 +int ISrsReloadHandler::on_reload_vhost_removed(SrsConfDirective* /*vhost*/)
  47 +{
  48 + return ERROR_SUCCESS;
  49 +}
  50 +
@@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */ 29 */
30 #include <srs_core.hpp> 30 #include <srs_core.hpp>
31 31
  32 +class SrsConfDirective;
  33 +
32 /** 34 /**
33 * the handler for config reload. 35 * the handler for config reload.
34 */ 36 */
@@ -40,6 +42,7 @@ public: @@ -40,6 +42,7 @@ public:
40 public: 42 public:
41 virtual int on_reload_listen(); 43 virtual int on_reload_listen();
42 virtual int on_reload_pithy_print(); 44 virtual int on_reload_pithy_print();
  45 + virtual int on_reload_vhost_removed(SrsConfDirective* vhost);
43 }; 46 };
44 47
45 #endif 48 #endif
@@ -231,6 +231,9 @@ int SrsRtmpClient::handshake() @@ -231,6 +231,9 @@ int SrsRtmpClient::handshake()
231 231
232 SrsSocket skt(stfd); 232 SrsSocket skt(stfd);
233 233
  234 + skt.set_recv_timeout(protocol->get_recv_timeout());
  235 + skt.set_send_timeout(protocol->get_send_timeout());
  236 +
234 SrsComplexHandshake complex_hs; 237 SrsComplexHandshake complex_hs;
235 SrsSimpleHandshake simple_hs; 238 SrsSimpleHandshake simple_hs;
236 if ((ret = simple_hs.handshake_with_server(skt, complex_hs)) != ERROR_SUCCESS) { 239 if ((ret = simple_hs.handshake_with_server(skt, complex_hs)) != ERROR_SUCCESS) {
@@ -422,6 +425,11 @@ void SrsRtmp::set_send_timeout(int64_t timeout_us) @@ -422,6 +425,11 @@ void SrsRtmp::set_send_timeout(int64_t timeout_us)
422 protocol->set_send_timeout(timeout_us); 425 protocol->set_send_timeout(timeout_us);
423 } 426 }
424 427
  428 +int64_t SrsRtmp::get_send_timeout()
  429 +{
  430 + return protocol->get_send_timeout();
  431 +}
  432 +
425 int64_t SrsRtmp::get_recv_bytes() 433 int64_t SrsRtmp::get_recv_bytes()
426 { 434 {
427 return protocol->get_recv_bytes(); 435 return protocol->get_recv_bytes();
@@ -458,6 +466,9 @@ int SrsRtmp::handshake() @@ -458,6 +466,9 @@ int SrsRtmp::handshake()
458 466
459 SrsSocket skt(stfd); 467 SrsSocket skt(stfd);
460 468
  469 + skt.set_recv_timeout(protocol->get_recv_timeout());
  470 + skt.set_send_timeout(protocol->get_send_timeout());
  471 +
461 SrsComplexHandshake complex_hs; 472 SrsComplexHandshake complex_hs;
462 SrsSimpleHandshake simple_hs; 473 SrsSimpleHandshake simple_hs;
463 if ((ret = simple_hs.handshake_with_client(skt, complex_hs)) != ERROR_SUCCESS) { 474 if ((ret = simple_hs.handshake_with_client(skt, complex_hs)) != ERROR_SUCCESS) {
@@ -144,6 +144,7 @@ public: @@ -144,6 +144,7 @@ public:
144 virtual void set_recv_timeout(int64_t timeout_us); 144 virtual void set_recv_timeout(int64_t timeout_us);
145 virtual int64_t get_recv_timeout(); 145 virtual int64_t get_recv_timeout();
146 virtual void set_send_timeout(int64_t timeout_us); 146 virtual void set_send_timeout(int64_t timeout_us);
  147 + virtual int64_t get_send_timeout();
147 virtual int64_t get_recv_bytes(); 148 virtual int64_t get_recv_bytes();
148 virtual int64_t get_send_bytes(); 149 virtual int64_t get_send_bytes();
149 virtual int get_recv_kbps(); 150 virtual int get_recv_kbps();
@@ -52,6 +52,11 @@ void SrsSocket::set_send_timeout(int64_t timeout_us) @@ -52,6 +52,11 @@ void SrsSocket::set_send_timeout(int64_t timeout_us)
52 send_timeout = timeout_us; 52 send_timeout = timeout_us;
53 } 53 }
54 54
  55 +int64_t SrsSocket::get_send_timeout()
  56 +{
  57 + return send_timeout;
  58 +}
  59 +
55 int64_t SrsSocket::get_recv_bytes() 60 int64_t SrsSocket::get_recv_bytes()
56 { 61 {
57 return recv_bytes; 62 return recv_bytes;
@@ -50,6 +50,7 @@ public: @@ -50,6 +50,7 @@ public:
50 virtual void set_recv_timeout(int64_t timeout_us); 50 virtual void set_recv_timeout(int64_t timeout_us);
51 virtual int64_t get_recv_timeout(); 51 virtual int64_t get_recv_timeout();
52 virtual void set_send_timeout(int64_t timeout_us); 52 virtual void set_send_timeout(int64_t timeout_us);
  53 + virtual int64_t get_send_timeout();
53 virtual int64_t get_recv_bytes(); 54 virtual int64_t get_recv_bytes();
54 virtual int64_t get_send_bytes(); 55 virtual int64_t get_send_bytes();
55 virtual int get_recv_kbps(); 56 virtual int get_recv_kbps();