From f551ff5ae8029f0c7f72470f1cff296cf8a48530 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 5 Jul 2020 23:49:47 +0800 Subject: [PATCH] Log: Use string compare function --- trunk/src/app/srs_app_recv_thread.cpp | 2 +- trunk/src/app/srs_app_rtc_source.cpp | 4 ++-- trunk/src/app/srs_app_source.cpp | 4 ++-- trunk/src/core/srs_core.hpp | 16 +++++++++++++--- trunk/src/utest/srs_utest_app.cpp | 4 ++-- trunk/src/utest/srs_utest_service.cpp | 4 ++-- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/trunk/src/app/srs_app_recv_thread.cpp b/trunk/src/app/srs_app_recv_thread.cpp index 44dc8dd98..78ee964f0 100644 --- a/trunk/src/app/srs_app_recv_thread.cpp +++ b/trunk/src/app/srs_app_recv_thread.cpp @@ -373,7 +373,7 @@ srs_error_t SrsPublishRecvThread::consume(SrsCommonMessage* msg) srs_error_t err = srs_success; // when cid changed, change it. - if (!ncid.equals(cid)) { + if (ncid.compare(cid)) { _srs_context->set_id(ncid); cid = ncid; } diff --git a/trunk/src/app/srs_app_rtc_source.cpp b/trunk/src/app/srs_app_rtc_source.cpp index 5acfef470..265ac52e1 100644 --- a/trunk/src/app/srs_app_rtc_source.cpp +++ b/trunk/src/app/srs_app_rtc_source.cpp @@ -291,13 +291,13 @@ srs_error_t SrsRtcSource::on_source_id_changed(SrsContextId id) { srs_error_t err = srs_success; - if (_source_id.equals(id)) { + if (!_source_id.compare(id)) { return err; } if (_pre_source_id.empty()) { _pre_source_id = id; - } else if (!_pre_source_id.equals(_source_id)) { + } else if (_pre_source_id.compare(_source_id)) { _pre_source_id = _source_id; } diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index dd032854e..fd54a7aca 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -2068,13 +2068,13 @@ srs_error_t SrsSource::on_source_id_changed(SrsContextId id) { srs_error_t err = srs_success; - if (_source_id.equals(id)) { + if (!_source_id.compare(id)) { return err; } if (_pre_source_id.empty()) { _pre_source_id = id; - } else if (!_pre_source_id.equals(_source_id)) { + } else if (_pre_source_id.compare(_source_id)) { _pre_source_id = _source_id; } diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 6ce9840fe..8d256cee2 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -113,8 +113,10 @@ class SrsCplxError; typedef SrsCplxError* srs_error_t; -// The context ID, it default to a string object, we can also use other objects. #include +// The context ID, it default to a string object, we can also use other objects. +// @remark User can directly user string as SrsContextId, we user struct to ensure the context is an object. +#if 1 struct _SrsContextId { std::string v_; @@ -132,10 +134,18 @@ struct _SrsContextId bool empty() { return v_.empty(); } - bool equals(const _SrsContextId& to) { - return v_ == to.v_; + // Compare the two context id. @see http://www.cplusplus.com/reference/string/string/compare/ + // 0 They compare equal + // <0 Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter. + // >0 Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer. + int compare(const _SrsContextId& to) { + return v_.compare(to.v_); } }; typedef _SrsContextId SrsContextId; +#else +// Actually, we can directly user string as SrsContextId. +typedef std::string SrsContextId; +#endif #endif diff --git a/trunk/src/utest/srs_utest_app.cpp b/trunk/src/utest/srs_utest_app.cpp index 523bb3b60..e04a859de 100644 --- a/trunk/src/utest/srs_utest_app.cpp +++ b/trunk/src/utest/srs_utest_app.cpp @@ -222,14 +222,14 @@ VOID TEST(AppCoroutineTest, Cycle) MockCoroutineHandler ch; SrsSTCoroutine sc("test", &ch, SrsContextId("250")); ch.trd = ≻ - EXPECT_TRUE(sc.cid().equals(SrsContextId("250"))); + EXPECT_TRUE(!sc.cid().compare(SrsContextId("250"))); EXPECT_TRUE(srs_success == sc.start()); EXPECT_TRUE(srs_success == sc.pull()); // After running, the cid in cycle should equal to the thread. srs_cond_timedwait(ch.running, 100 * SRS_UTIME_MILLISECONDS); - EXPECT_TRUE(ch.cid.equals(SrsContextId("250"))); + EXPECT_TRUE(!ch.cid.compare(SrsContextId("250"))); } if (true) { diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index c51882cfc..ab5250422 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -1383,8 +1383,8 @@ VOID TEST(TCPServerTest, ContextUtility) SrsThreadContext ctx; EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty()); - EXPECT_TRUE(ctx.set_id(SrsContextId("1000")).equals(SrsContextId("100"))); - EXPECT_TRUE(ctx.get_id().equals(SrsContextId("1000"))); + EXPECT_TRUE(!ctx.set_id(SrsContextId("1000")).compare(SrsContextId("100"))); + EXPECT_TRUE(!ctx.get_id().compare(SrsContextId("1000"))); ctx.clear_cid(); EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty());