Log: Use string compare function

pull/1882/head
winlin 5 years ago
parent 6624b8acca
commit f551ff5ae8

@ -373,7 +373,7 @@ srs_error_t SrsPublishRecvThread::consume(SrsCommonMessage* msg)
srs_error_t err = srs_success; srs_error_t err = srs_success;
// when cid changed, change it. // when cid changed, change it.
if (!ncid.equals(cid)) { if (ncid.compare(cid)) {
_srs_context->set_id(ncid); _srs_context->set_id(ncid);
cid = ncid; cid = ncid;
} }

@ -291,13 +291,13 @@ srs_error_t SrsRtcSource::on_source_id_changed(SrsContextId id)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (_source_id.equals(id)) { if (!_source_id.compare(id)) {
return err; return err;
} }
if (_pre_source_id.empty()) { if (_pre_source_id.empty()) {
_pre_source_id = id; _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; _pre_source_id = _source_id;
} }

@ -2068,13 +2068,13 @@ srs_error_t SrsSource::on_source_id_changed(SrsContextId id)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (_source_id.equals(id)) { if (!_source_id.compare(id)) {
return err; return err;
} }
if (_pre_source_id.empty()) { if (_pre_source_id.empty()) {
_pre_source_id = id; _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; _pre_source_id = _source_id;
} }

@ -113,8 +113,10 @@
class SrsCplxError; class SrsCplxError;
typedef SrsCplxError* srs_error_t; typedef SrsCplxError* srs_error_t;
// The context ID, it default to a string object, we can also use other objects.
#include <string> #include <string>
// 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 struct _SrsContextId
{ {
std::string v_; std::string v_;
@ -132,10 +134,18 @@ struct _SrsContextId
bool empty() { bool empty() {
return v_.empty(); return v_.empty();
} }
bool equals(const _SrsContextId& to) { // Compare the two context id. @see http://www.cplusplus.com/reference/string/string/compare/
return v_ == to.v_; // 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; typedef _SrsContextId SrsContextId;
#else
// Actually, we can directly user string as SrsContextId.
typedef std::string SrsContextId;
#endif
#endif #endif

@ -222,14 +222,14 @@ VOID TEST(AppCoroutineTest, Cycle)
MockCoroutineHandler ch; MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch, SrsContextId("250")); SrsSTCoroutine sc("test", &ch, SrsContextId("250"));
ch.trd = &sc; ch.trd = &sc;
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.start());
EXPECT_TRUE(srs_success == sc.pull()); EXPECT_TRUE(srs_success == sc.pull());
// After running, the cid in cycle should equal to the thread. // After running, the cid in cycle should equal to the thread.
srs_cond_timedwait(ch.running, 100 * SRS_UTIME_MILLISECONDS); 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) { if (true) {

@ -1383,8 +1383,8 @@ VOID TEST(TCPServerTest, ContextUtility)
SrsThreadContext ctx; SrsThreadContext ctx;
EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty()); EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty());
EXPECT_TRUE(ctx.set_id(SrsContextId("1000")).equals(SrsContextId("100"))); EXPECT_TRUE(!ctx.set_id(SrsContextId("1000")).compare(SrsContextId("100")));
EXPECT_TRUE(ctx.get_id().equals(SrsContextId("1000"))); EXPECT_TRUE(!ctx.get_id().compare(SrsContextId("1000")));
ctx.clear_cid(); ctx.clear_cid();
EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty()); EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty());

Loading…
Cancel
Save