fix signal bug, break for gmc. fix SrsMessage leak, use common message to free payload. to 0.9.88
正在显示
5 个修改的文件
包含
86 行增加
和
50 行删除
| @@ -532,6 +532,59 @@ int SrsServer::ingest() | @@ -532,6 +532,59 @@ int SrsServer::ingest() | ||
| 532 | int SrsServer::cycle() | 532 | int SrsServer::cycle() |
| 533 | { | 533 | { |
| 534 | int ret = ERROR_SUCCESS; | 534 | int ret = ERROR_SUCCESS; |
| 535 | + | ||
| 536 | + ret = do_cycle(); | ||
| 537 | + | ||
| 538 | +#ifdef SRS_AUTO_INGEST | ||
| 539 | + ingester->stop(); | ||
| 540 | +#endif | ||
| 541 | + | ||
| 542 | + return ret; | ||
| 543 | +} | ||
| 544 | + | ||
| 545 | +void SrsServer::remove(SrsConnection* conn) | ||
| 546 | +{ | ||
| 547 | + std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn); | ||
| 548 | + | ||
| 549 | + if (it != conns.end()) { | ||
| 550 | + conns.erase(it); | ||
| 551 | + } | ||
| 552 | + | ||
| 553 | + srs_info("conn removed. conns=%d", (int)conns.size()); | ||
| 554 | + | ||
| 555 | + // all connections are created by server, | ||
| 556 | + // so we free it here. | ||
| 557 | + srs_freep(conn); | ||
| 558 | +} | ||
| 559 | + | ||
| 560 | +void SrsServer::on_signal(int signo) | ||
| 561 | +{ | ||
| 562 | + if (signo == SIGNAL_RELOAD) { | ||
| 563 | + signal_reload = true; | ||
| 564 | + return; | ||
| 565 | + } | ||
| 566 | + | ||
| 567 | + if (signo == SIGINT) { | ||
| 568 | +#ifdef SRS_AUTO_GPERF_MC | ||
| 569 | + srs_trace("gmc is on, main cycle will terminate normally."); | ||
| 570 | + signal_gmc_stop = true; | ||
| 571 | +#else | ||
| 572 | + srs_trace("user terminate program"); | ||
| 573 | + exit(0); | ||
| 574 | +#endif | ||
| 575 | + return; | ||
| 576 | + } | ||
| 577 | + | ||
| 578 | + if (signo == SIGTERM) { | ||
| 579 | + srs_trace("user terminate program"); | ||
| 580 | + exit(0); | ||
| 581 | + return; | ||
| 582 | + } | ||
| 583 | +} | ||
| 584 | + | ||
| 585 | +int SrsServer::do_cycle() | ||
| 586 | +{ | ||
| 587 | + int ret = ERROR_SUCCESS; | ||
| 535 | 588 | ||
| 536 | // find the max loop | 589 | // find the max loop |
| 537 | int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES); | 590 | int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES); |
| @@ -551,7 +604,7 @@ int SrsServer::cycle() | @@ -551,7 +604,7 @@ int SrsServer::cycle() | ||
| 551 | // because directly exit will cause core-dump. | 604 | // because directly exit will cause core-dump. |
| 552 | #ifdef SRS_AUTO_GPERF_MC | 605 | #ifdef SRS_AUTO_GPERF_MC |
| 553 | if (signal_gmc_stop) { | 606 | if (signal_gmc_stop) { |
| 554 | - break; | 607 | + return ret; |
| 555 | } | 608 | } |
| 556 | #endif | 609 | #endif |
| 557 | 610 | ||
| @@ -582,53 +635,9 @@ int SrsServer::cycle() | @@ -582,53 +635,9 @@ int SrsServer::cycle() | ||
| 582 | } | 635 | } |
| 583 | } | 636 | } |
| 584 | 637 | ||
| 585 | -#ifdef SRS_AUTO_INGEST | ||
| 586 | - ingester->stop(); | ||
| 587 | -#endif | ||
| 588 | - | ||
| 589 | return ret; | 638 | return ret; |
| 590 | } | 639 | } |
| 591 | 640 | ||
| 592 | -void SrsServer::remove(SrsConnection* conn) | ||
| 593 | -{ | ||
| 594 | - std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn); | ||
| 595 | - | ||
| 596 | - if (it != conns.end()) { | ||
| 597 | - conns.erase(it); | ||
| 598 | - } | ||
| 599 | - | ||
| 600 | - srs_info("conn removed. conns=%d", (int)conns.size()); | ||
| 601 | - | ||
| 602 | - // all connections are created by server, | ||
| 603 | - // so we free it here. | ||
| 604 | - srs_freep(conn); | ||
| 605 | -} | ||
| 606 | - | ||
| 607 | -void SrsServer::on_signal(int signo) | ||
| 608 | -{ | ||
| 609 | - if (signo == SIGNAL_RELOAD) { | ||
| 610 | - signal_reload = true; | ||
| 611 | - return; | ||
| 612 | - } | ||
| 613 | - | ||
| 614 | - if (signo == SIGINT) { | ||
| 615 | -#ifdef SRS_AUTO_GPERF_MC | ||
| 616 | - srs_trace("gmc is on, main cycle will terminate normally."); | ||
| 617 | - signal_gmc_stop = true; | ||
| 618 | -#else | ||
| 619 | - srs_trace("user terminate program"); | ||
| 620 | - exit(0); | ||
| 621 | -#endif | ||
| 622 | - return; | ||
| 623 | - } | ||
| 624 | - | ||
| 625 | - if (signo == SIGTERM) { | ||
| 626 | - srs_trace("user terminate program"); | ||
| 627 | - exit(0); | ||
| 628 | - return; | ||
| 629 | - } | ||
| 630 | -} | ||
| 631 | - | ||
| 632 | int SrsServer::listen_rtmp() | 641 | int SrsServer::listen_rtmp() |
| 633 | { | 642 | { |
| 634 | int ret = ERROR_SUCCESS; | 643 | int ret = ERROR_SUCCESS; |
| @@ -140,6 +140,7 @@ public: | @@ -140,6 +140,7 @@ public: | ||
| 140 | virtual void remove(SrsConnection* conn); | 140 | virtual void remove(SrsConnection* conn); |
| 141 | virtual void on_signal(int signo); | 141 | virtual void on_signal(int signo); |
| 142 | private: | 142 | private: |
| 143 | + virtual int do_cycle(); | ||
| 143 | virtual int listen_rtmp(); | 144 | virtual int listen_rtmp(); |
| 144 | virtual int listen_http_api(); | 145 | virtual int listen_http_api(); |
| 145 | virtual int listen_http_stream(); | 146 | virtual int listen_http_stream(); |
| @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 31 | // current release version | 31 | // current release version |
| 32 | #define VERSION_MAJOR "0" | 32 | #define VERSION_MAJOR "0" |
| 33 | #define VERSION_MINOR "9" | 33 | #define VERSION_MINOR "9" |
| 34 | -#define VERSION_REVISION "87" | 34 | +#define VERSION_REVISION "88" |
| 35 | #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION | 35 | #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION |
| 36 | // server info. | 36 | // server info. |
| 37 | #define RTMP_SIG_SRS_KEY "srs" | 37 | #define RTMP_SIG_SRS_KEY "srs" |
| @@ -759,7 +759,7 @@ int SrsProtocol::send_and_free_packet(SrsPacket* packet, int stream_id) | @@ -759,7 +759,7 @@ int SrsProtocol::send_and_free_packet(SrsPacket* packet, int stream_id) | ||
| 759 | } | 759 | } |
| 760 | 760 | ||
| 761 | // to message | 761 | // to message |
| 762 | - SrsMessage* msg = new SrsMessage(); | 762 | + SrsMessage* msg = new SrsCommonMessage(); |
| 763 | 763 | ||
| 764 | msg->payload = (int8_t*)payload; | 764 | msg->payload = (int8_t*)payload; |
| 765 | msg->size = (int32_t)size; | 765 | msg->size = (int32_t)size; |
| @@ -975,7 +975,7 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz | @@ -975,7 +975,7 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz | ||
| 975 | bool is_first_chunk_of_msg = false; | 975 | bool is_first_chunk_of_msg = false; |
| 976 | if (!chunk->msg) { | 976 | if (!chunk->msg) { |
| 977 | is_first_chunk_of_msg = true; | 977 | is_first_chunk_of_msg = true; |
| 978 | - chunk->msg = new SrsMessage(); | 978 | + chunk->msg = new SrsCommonMessage(); |
| 979 | srs_verbose("create message for new chunk, fmt=%d, cid=%d", fmt, chunk->cid); | 979 | srs_verbose("create message for new chunk, fmt=%d, cid=%d", fmt, chunk->cid); |
| 980 | } | 980 | } |
| 981 | 981 | ||
| @@ -1532,7 +1532,16 @@ SrsMessage::SrsMessage() | @@ -1532,7 +1532,16 @@ SrsMessage::SrsMessage() | ||
| 1532 | } | 1532 | } |
| 1533 | 1533 | ||
| 1534 | SrsMessage::~SrsMessage() | 1534 | SrsMessage::~SrsMessage() |
| 1535 | -{ | 1535 | +{ |
| 1536 | +} | ||
| 1537 | + | ||
| 1538 | +SrsCommonMessage::SrsCommonMessage() | ||
| 1539 | +{ | ||
| 1540 | +} | ||
| 1541 | + | ||
| 1542 | +SrsCommonMessage::~SrsCommonMessage() | ||
| 1543 | +{ | ||
| 1544 | + srs_freepa(payload); | ||
| 1536 | } | 1545 | } |
| 1537 | 1546 | ||
| 1538 | SrsSharedPtrMessage::__SrsSharedPtr::__SrsSharedPtr() | 1547 | SrsSharedPtrMessage::__SrsSharedPtr::__SrsSharedPtr() |
| @@ -325,6 +325,12 @@ public: | @@ -325,6 +325,12 @@ public: | ||
| 325 | * message is raw data RTMP message, bytes oriented, | 325 | * message is raw data RTMP message, bytes oriented, |
| 326 | * protcol always recv RTMP message, and can send RTMP message or RTMP packet. | 326 | * protcol always recv RTMP message, and can send RTMP message or RTMP packet. |
| 327 | * the shared-ptr message is a special RTMP message, use ref-count for performance issue. | 327 | * the shared-ptr message is a special RTMP message, use ref-count for performance issue. |
| 328 | +* | ||
| 329 | +* @remark, never directly new SrsMessage, the constructor is protected, | ||
| 330 | +* for in the SrsMessage, we never know whether we should free the message, | ||
| 331 | +* for SrsCommonMessage, we should free the payload, | ||
| 332 | +* while for SrsSharedPtrMessage, we should use ref-count to free it. | ||
| 333 | +* so, use these two concrete message, SrsCommonMessage or SrsSharedPtrMessage instread. | ||
| 328 | */ | 334 | */ |
| 329 | class SrsMessage | 335 | class SrsMessage |
| 330 | { | 336 | { |
| @@ -341,12 +347,23 @@ public: | @@ -341,12 +347,23 @@ public: | ||
| 341 | */ | 347 | */ |
| 342 | int32_t size; | 348 | int32_t size; |
| 343 | int8_t* payload; | 349 | int8_t* payload; |
| 344 | -public: | 350 | +protected: |
| 345 | SrsMessage(); | 351 | SrsMessage(); |
| 352 | +public: | ||
| 346 | virtual ~SrsMessage(); | 353 | virtual ~SrsMessage(); |
| 347 | }; | 354 | }; |
| 348 | 355 | ||
| 349 | /** | 356 | /** |
| 357 | +* the common message used free the payload in common way. | ||
| 358 | +*/ | ||
| 359 | +class SrsCommonMessage : public SrsMessage | ||
| 360 | +{ | ||
| 361 | +public: | ||
| 362 | + SrsCommonMessage(); | ||
| 363 | + virtual ~SrsCommonMessage(); | ||
| 364 | +}; | ||
| 365 | + | ||
| 366 | +/** | ||
| 350 | * shared ptr message. | 367 | * shared ptr message. |
| 351 | * for audio/video/data message that need less memory copy. | 368 | * for audio/video/data message that need less memory copy. |
| 352 | * and only for output. | 369 | * and only for output. |
-
请 注册 或 登录 后发表评论