From 20a42599f39fe15f0a9168e80fa4ef5f25bdb841 Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 22 Sep 2017 19:54:50 +0800 Subject: [PATCH] For #913, http callback use complex error --- trunk/src/app/srs_app_async_call.cpp | 6 +- trunk/src/app/srs_app_async_call.hpp | 2 +- trunk/src/app/srs_app_conn.cpp | 5 +- trunk/src/app/srs_app_dvr.cpp | 15 +- trunk/src/app/srs_app_dvr.hpp | 2 +- trunk/src/app/srs_app_heartbeat.cpp | 9 +- trunk/src/app/srs_app_hls.cpp | 30 ++- trunk/src/app/srs_app_hls.hpp | 4 +- trunk/src/app/srs_app_http_conn.cpp | 4 +- trunk/src/app/srs_app_http_hooks.cpp | 208 ++++++++---------- trunk/src/app/srs_app_http_hooks.hpp | 14 +- trunk/src/app/srs_app_kafka.cpp | 16 +- trunk/src/app/srs_app_kafka.hpp | 2 +- trunk/src/app/srs_app_rtmp_conn.cpp | 70 +++--- trunk/src/app/srs_app_rtmp_conn.hpp | 6 +- trunk/src/app/srs_app_rtsp.cpp | 5 +- trunk/src/main/srs_main_ingest_hls.cpp | 17 +- trunk/src/service/srs_service_http_client.cpp | 53 ++--- trunk/src/service/srs_service_http_client.hpp | 8 +- trunk/src/service/srs_service_rtmp_conn.cpp | 6 +- trunk/src/service/srs_service_st.cpp | 42 ++-- trunk/src/service/srs_service_st.hpp | 8 +- 22 files changed, 264 insertions(+), 268 deletions(-) diff --git a/trunk/src/app/srs_app_async_call.cpp b/trunk/src/app/srs_app_async_call.cpp index 0c582db58..49d92943b 100644 --- a/trunk/src/app/srs_app_async_call.cpp +++ b/trunk/src/app/srs_app_async_call.cpp @@ -111,9 +111,9 @@ srs_error_t SrsAsyncCallWorker::cycle() for (it = copy.begin(); it != copy.end(); ++it) { ISrsAsyncCallTask* task = *it; - int ret = ERROR_SUCCESS; - if ((ret = task->call()) != ERROR_SUCCESS) { - srs_warn("ignore async callback %s, ret=%d", task->to_string().c_str(), ret); + if ((err = task->call()) != srs_success) { + srs_warn("ignore task failed %s", srs_error_desc(err).c_str()); + srs_freep(err); } srs_freep(task); } diff --git a/trunk/src/app/srs_app_async_call.hpp b/trunk/src/app/srs_app_async_call.hpp index 12dc20076..ce433a8ea 100644 --- a/trunk/src/app/srs_app_async_call.hpp +++ b/trunk/src/app/srs_app_async_call.hpp @@ -50,7 +50,7 @@ public: * this method is the actual execute method of task, * for example, to notify callback server. */ - virtual int call() = 0; + virtual srs_error_t call() = 0; /** * convert task to string to describe it. * used for logger. diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index f3543f8d8..37e9fdded 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -82,11 +82,10 @@ void SrsConnection::dispose() srs_error_t SrsConnection::start() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; - if ((ret = skt->initialize(stfd)) != ERROR_SUCCESS) { - return srs_error_new(ret, "socket"); + if ((err = skt->initialize(stfd)) != srs_success) { + return srs_error_wrap(err, "init socket"); } if ((err = trd->start()) != srs_success) { diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index 45322d7c3..f2c0f03d4 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -553,12 +553,12 @@ SrsDvrAsyncCallOnDvr::~SrsDvrAsyncCallOnDvr() srs_freep(req); } -int SrsDvrAsyncCallOnDvr::call() +srs_error_t SrsDvrAsyncCallOnDvr::call() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { - return ret; + return err; } // the http hooks will cause context switch, @@ -571,7 +571,7 @@ int SrsDvrAsyncCallOnDvr::call() if (!conf) { srs_info("ignore the empty http callback: on_dvr"); - return ret; + return err; } hooks = conf->args; @@ -579,13 +579,12 @@ int SrsDvrAsyncCallOnDvr::call() for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((ret = SrsHttpHooks::on_dvr(cid, url, req, path)) != ERROR_SUCCESS) { - srs_error("hook client on_dvr failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + if ((err = SrsHttpHooks::on_dvr(cid, url, req, path)) != srs_success) { + return srs_error_wrap(err, "callback on_dvr %s", url.c_str()); } } - return ret; + return err; } string SrsDvrAsyncCallOnDvr::to_string() diff --git a/trunk/src/app/srs_app_dvr.hpp b/trunk/src/app/srs_app_dvr.hpp index d3ce624a1..efae24802 100644 --- a/trunk/src/app/srs_app_dvr.hpp +++ b/trunk/src/app/srs_app_dvr.hpp @@ -174,7 +174,7 @@ public: SrsDvrAsyncCallOnDvr(int c, SrsRequest* r, std::string p); virtual ~SrsDvrAsyncCallOnDvr(); public: - virtual int call(); + virtual srs_error_t call(); virtual std::string to_string(); }; diff --git a/trunk/src/app/srs_app_heartbeat.cpp b/trunk/src/app/srs_app_heartbeat.cpp index 8e722e523..4b024f6ea 100644 --- a/trunk/src/app/srs_app_heartbeat.cpp +++ b/trunk/src/app/srs_app_heartbeat.cpp @@ -47,6 +47,7 @@ SrsHttpHeartbeat::~SrsHttpHeartbeat() void SrsHttpHeartbeat::heartbeat() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; std::string url = _srs_config->get_heartbeat_url(); @@ -78,13 +79,17 @@ void SrsHttpHeartbeat::heartbeat() } SrsHttpClient http; - if ((ret = http.initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) { + if ((err = http.initialize(uri.get_host(), uri.get_port())) != srs_success) { + srs_freep(err); return; } std::string req = obj->dumps(); ISrsHttpMessage* msg = NULL; - if ((ret = http.post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) { + if ((err = http.post(uri.get_path(), req, &msg)) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_info("http post hartbeart uri failed. url=%s, request=%s, ret=%d", url.c_str(), req.c_str(), ret); return; diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index 0a4439bb3..babf4d4eb 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -90,12 +90,12 @@ SrsDvrAsyncCallOnHls::~SrsDvrAsyncCallOnHls() srs_freep(req); } -int SrsDvrAsyncCallOnHls::call() +srs_error_t SrsDvrAsyncCallOnHls::call() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { - return ret; + return err; } // the http hooks will cause context switch, @@ -108,7 +108,7 @@ int SrsDvrAsyncCallOnHls::call() if (!conf) { srs_info("ignore the empty http callback: on_hls"); - return ret; + return err; } hooks = conf->args; @@ -116,13 +116,12 @@ int SrsDvrAsyncCallOnHls::call() for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((ret = SrsHttpHooks::on_hls(cid, url, req, path, ts_url, m3u8, m3u8_url, seq_no, duration)) != ERROR_SUCCESS) { - srs_error("hook client on_hls failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + if ((err = SrsHttpHooks::on_hls(cid, url, req, path, ts_url, m3u8, m3u8_url, seq_no, duration)) != srs_success) { + return srs_error_wrap(err, "callback on_hls %s", url.c_str()); } } - return ret; + return err; } string SrsDvrAsyncCallOnHls::to_string() @@ -142,12 +141,12 @@ SrsDvrAsyncCallOnHlsNotify::~SrsDvrAsyncCallOnHlsNotify() srs_freep(req); } -int SrsDvrAsyncCallOnHlsNotify::call() +srs_error_t SrsDvrAsyncCallOnHlsNotify::call() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { - return ret; + return err; } // the http hooks will cause context switch, @@ -160,7 +159,7 @@ int SrsDvrAsyncCallOnHlsNotify::call() if (!conf) { srs_info("ignore the empty http callback: on_hls_notify"); - return ret; + return err; } hooks = conf->args; @@ -169,13 +168,12 @@ int SrsDvrAsyncCallOnHlsNotify::call() int nb_notify = _srs_config->get_vhost_hls_nb_notify(req->vhost); for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((ret = SrsHttpHooks::on_hls_notify(cid, url, req, ts_url, nb_notify)) != ERROR_SUCCESS) { - srs_error("hook client on_hls_notify failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + if ((err = SrsHttpHooks::on_hls_notify(cid, url, req, ts_url, nb_notify)) != srs_success) { + return srs_error_wrap(err, "callback on_hls_notify %s", url.c_str()); } } - return ret; + return err; } string SrsDvrAsyncCallOnHlsNotify::to_string() diff --git a/trunk/src/app/srs_app_hls.hpp b/trunk/src/app/srs_app_hls.hpp index 47b42709f..ca29a544d 100644 --- a/trunk/src/app/srs_app_hls.hpp +++ b/trunk/src/app/srs_app_hls.hpp @@ -91,7 +91,7 @@ public: SrsDvrAsyncCallOnHls(int c, SrsRequest* r, std::string p, std::string t, std::string m, std::string mu, int s, double d); virtual ~SrsDvrAsyncCallOnHls(); public: - virtual int call(); + virtual srs_error_t call(); virtual std::string to_string(); }; @@ -108,7 +108,7 @@ public: SrsDvrAsyncCallOnHlsNotify(int c, SrsRequest* r, std::string u); virtual ~SrsDvrAsyncCallOnHlsNotify(); public: - virtual int call(); + virtual srs_error_t call(); virtual std::string to_string(); }; diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 11d356b1a..483bb0d95 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -217,8 +217,8 @@ srs_error_t SrsResponseOnlyHttpConn::pop_message(ISrsHttpMessage** preq) SrsStSocket skt; - if ((ret = skt.initialize(stfd)) != ERROR_SUCCESS) { - return srs_error_new(ret, "init socket"); + if ((err = skt.initialize(stfd)) != srs_success) { + return srs_error_wrap(err, "init socket"); } if ((ret = parser->parse_message(&skt, this, preq)) != ERROR_SUCCESS) { diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index 7e9212c81..741177bf3 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -56,9 +56,9 @@ SrsHttpHooks::~SrsHttpHooks() { } -int SrsHttpHooks::on_connect(string url, SrsRequest* req) +srs_error_t SrsHttpHooks::on_connect(string url, SrsRequest* req) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = _srs_context->get_id(); @@ -78,23 +78,20 @@ int SrsHttpHooks::on_connect(string url, SrsRequest* req) int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_error("http post on_connect uri failed. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); - return ret; + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + return srs_error_wrap(err, "http: on_connect failed, client_id=%d, url=%s, request=%s, response=%s, code=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code); } - srs_trace("http hook on_connect success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_connect ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); - return ret; + return err; } void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int64_t recv_bytes) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = _srs_context->get_id(); @@ -114,23 +111,23 @@ void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_warn("http post on_close uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + int ret = srs_error_code(err); + srs_freep(err); + srs_warn("http: ignore on_close failed, client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return; } - srs_trace("http hook on_close success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_close ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); return; } -int SrsHttpHooks::on_publish(string url, SrsRequest* req) +srs_error_t SrsHttpHooks::on_publish(string url, SrsRequest* req) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = _srs_context->get_id(); @@ -150,23 +147,20 @@ int SrsHttpHooks::on_publish(string url, SrsRequest* req) int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_error("http post on_publish uri failed. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); - return ret; + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + return srs_error_wrap(err, "http: on_publish failed, client_id=%d, url=%s, request=%s, response=%s, code=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code); } - srs_trace("http hook on_publish success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_publish ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); - return ret; + return err; } void SrsHttpHooks::on_unpublish(string url, SrsRequest* req) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = _srs_context->get_id(); @@ -185,23 +179,23 @@ void SrsHttpHooks::on_unpublish(string url, SrsRequest* req) int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_warn("http post on_unpublish uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + int ret = srs_error_code(err); + srs_freep(err); + srs_warn("http: ignore on_unpublish failed, client_id=%d, url=%s, request=%s, response=%s, status=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return; } - srs_trace("http hook on_unpublish success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_unpublish ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); return; } -int SrsHttpHooks::on_play(string url, SrsRequest* req) +srs_error_t SrsHttpHooks::on_play(string url, SrsRequest* req) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = _srs_context->get_id(); @@ -221,23 +215,20 @@ int SrsHttpHooks::on_play(string url, SrsRequest* req) int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_error("http post on_play uri failed. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); - return ret; + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + return srs_error_wrap(err, "http: on_play failed, client_id=%d, url=%s, request=%s, response=%s, status=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code); } - srs_trace("http hook on_play success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_play ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); - return ret; + return err; } void SrsHttpHooks::on_stop(string url, SrsRequest* req) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = _srs_context->get_id(); @@ -256,23 +247,23 @@ void SrsHttpHooks::on_stop(string url, SrsRequest* req) int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_warn("http post on_stop uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + int ret = srs_error_code(err); + srs_freep(err); + srs_warn("http: ignore on_stop failed, client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); return; } - srs_trace("http hook on_stop success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_stop ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); return; } -int SrsHttpHooks::on_dvr(int cid, string url, SrsRequest* req, string file) +srs_error_t SrsHttpHooks::on_dvr(int cid, string url, SrsRequest* req, string file) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = cid; std::string cwd = _srs_config->cwd(); @@ -294,23 +285,20 @@ int SrsHttpHooks::on_dvr(int cid, string url, SrsRequest* req, string file) int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_error("http post on_dvr uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); - return ret; + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + return srs_error_wrap(err, "http post on_dvr uri failed, client_id=%d, url=%s, request=%s, response=%s, code=%d", + client_id, url.c_str(), data.c_str(), res.c_str(), status_code); } - srs_trace("http hook on_dvr success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http hook on_dvr success. client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); - return ret; + return err; } -int SrsHttpHooks::on_hls(int cid, string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, double duration) +srs_error_t SrsHttpHooks::on_hls(int cid, string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, double duration) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = cid; std::string cwd = _srs_config->cwd(); @@ -343,23 +331,20 @@ int SrsHttpHooks::on_hls(int cid, string url, SrsRequest* req, string file, stri int status_code; SrsHttpClient http; - if ((ret = do_post(&http, url, data, status_code, res)) != ERROR_SUCCESS) { - srs_error("http post on_hls uri failed, ignored. " - "client_id=%d, url=%s, request=%s, response=%s, code=%d, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), status_code, ret); - return ret; + if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { + return srs_error_wrap(err, "http: post %s with %s, status=%d, res=%s", url.c_str(), data.c_str(), status_code, res.c_str()); } - srs_trace("http hook on_hls success. " - "client_id=%d, url=%s, request=%s, response=%s, ret=%d", - client_id, url.c_str(), data.c_str(), res.c_str(), ret); + srs_trace("http: on_hls ok, client_id=%d, url=%s, request=%s, response=%s", + client_id, url.c_str(), data.c_str(), res.c_str()); - return ret; + return err; } -int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify) +srs_error_t SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int client_id = cid; std::string cwd = _srs_config->cwd(); @@ -376,13 +361,12 @@ int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std:: SrsHttpUri uri; if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + return srs_error_new(ret, "http: init url=%s", url.c_str()); } SrsHttpClient http; - if ((ret = http.initialize(uri.get_host(), uri.get_port(), SRS_HLS_NOTIFY_TMMS)) != ERROR_SUCCESS) { - return ret; + if ((err = http.initialize(uri.get_host(), uri.get_port(), SRS_HLS_NOTIFY_TMMS)) != srs_success) { + return srs_error_wrap(err, "http: init client for %s", url.c_str()); } std::string path = uri.get_query(); @@ -393,11 +377,11 @@ int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std:: path += "?"; path += uri.get_query(); } - srs_warn("GET %s", path.c_str()); + srs_info("GET %s", path.c_str()); ISrsHttpMessage* msg = NULL; - if ((ret = http.get(path.c_str(), "", &msg)) != ERROR_SUCCESS) { - return ret; + if ((err = http.get(path.c_str(), "", &msg)) != srs_success) { + return srs_error_wrap(err, "http: get %s", url.c_str()); } SrsAutoFree(ISrsHttpMessage, msg); @@ -417,87 +401,75 @@ int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std:: int spenttime = (int)(srs_update_system_time_ms() - starttime); srs_trace("http hook on_hls_notify success. client_id=%d, url=%s, code=%d, spent=%dms, read=%dB, ret=%d", - client_id, url.c_str(), msg->status_code(), spenttime, nb_read, ret); + client_id, url.c_str(), msg->status_code(), spenttime, nb_read, ret); // ignore any error for on_hls_notify. ret = ERROR_SUCCESS; - return ret; + return err; } -int SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, string& res) +srs_error_t SrsHttpHooks::do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, string& res) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsHttpUri uri; if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { - srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + return srs_error_new(ret, "http: post failed. url=%s", url.c_str()); } - if ((ret = hc->initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) { - return ret; + if ((err = hc->initialize(uri.get_host(), uri.get_port())) != srs_success) { + return srs_error_wrap(err, "http: init client"); } ISrsHttpMessage* msg = NULL; - if ((ret = hc->post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) { - return ret; + if ((err = hc->post(uri.get_path(), req, &msg)) != srs_success) { + return srs_error_wrap(err, "http: client post"); } SrsAutoFree(ISrsHttpMessage, msg); code = msg->status_code(); if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "http: body read"); } // ensure the http status is ok. // https://github.com/ossrs/srs/issues/158 if (code != SRS_CONSTS_HTTP_OK && code != SRS_CONSTS_HTTP_Created) { - ret = ERROR_HTTP_STATUS_INVALID; - srs_error("invalid response status=%d. ret=%d", code, ret); - return ret; + return srs_error_new(ERROR_HTTP_STATUS_INVALID, "http: status %d", code); } // should never be empty. if (res.empty()) { - ret = ERROR_HTTP_DATA_INVALID; - srs_error("invalid empty response. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: empty response"); } // parse string res to json. SrsJsonAny* info = SrsJsonAny::loads(res); if (!info) { - ret = ERROR_HTTP_DATA_INVALID; - srs_error("invalid response %s. ret=%d", res.c_str(), ret); - return ret; + return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: not json %s", res.c_str()); } SrsAutoFree(SrsJsonAny, info); // response error code in string. if (!info->is_object()) { - if (res != SRS_HTTP_RESPONSE_OK) { - ret = ERROR_HTTP_DATA_INVALID; - srs_error("invalid response number %s. ret=%d", res.c_str(), ret); - return ret; + if (res == SRS_HTTP_RESPONSE_OK) { + return err; } - return ret; + return srs_error_new(ERROR_HTTP_DATA_INVALID, "http: response number code %s", res.c_str()); } // response standard object, format in json: {"code": 0, "data": ""} SrsJsonObject* res_info = info->to_object(); SrsJsonAny* res_code = NULL; if ((res_code = res_info->ensure_property_integer("code")) == NULL) { - ret = ERROR_RESPONSE_CODE; - srs_error("invalid response without code, ret=%d", ret); - return ret; + return srs_error_new(ERROR_RESPONSE_CODE, "http: response object no code %s", res.c_str()); } if ((res_code->to_integer()) != ERROR_SUCCESS) { - ret = ERROR_RESPONSE_CODE; - srs_error("error response code=%d. ret=%d", res_code->to_integer(), ret); - return ret; + return srs_error_new(ERROR_RESPONSE_CODE, "http: response object code %d %s", res_code->to_integer(), res.c_str()); } - return ret; + return err; } diff --git a/trunk/src/app/srs_app_http_hooks.hpp b/trunk/src/app/srs_app_http_hooks.hpp index aef264b46..586aaaac8 100644 --- a/trunk/src/app/srs_app_http_hooks.hpp +++ b/trunk/src/app/srs_app_http_hooks.hpp @@ -51,7 +51,7 @@ public: * @param url the api server url, to valid the client. * ignore if empty. */ - static int on_connect(std::string url, SrsRequest* req); + static srs_error_t on_connect(std::string url, SrsRequest* req); /** * on_close hook, when client disconnect to srs, where client is valid by on_connect. * @param url the api server url, to process the event. @@ -63,7 +63,7 @@ public: * @param url the api server url, to valid the client. * ignore if empty. */ - static int on_publish(std::string url, SrsRequest* req); + static srs_error_t on_publish(std::string url, SrsRequest* req); /** * on_unpublish hook, when client(encoder) stop publish stream. * @param url the api server url, to process the event. @@ -75,7 +75,7 @@ public: * @param url the api server url, to valid the client. * ignore if empty. */ - static int on_play(std::string url, SrsRequest* req); + static srs_error_t on_play(std::string url, SrsRequest* req); /** * on_stop hook, when client stop to play the stream. * @param url the api server url, to process the event. @@ -89,7 +89,7 @@ public: * @param file the file path, can be relative or absolute path. * @param cid the source connection cid, for the on_dvr is async call. */ - static int on_dvr(int cid, std::string url, SrsRequest* req, std::string file); + static srs_error_t on_dvr(int cid, std::string url, SrsRequest* req, std::string file); /** * when hls reap segment, callback. * @param url the api server url, to process the event. @@ -102,7 +102,7 @@ public: * @param duration the segment duration in seconds. * @param cid the source connection cid, for the on_dvr is async call. */ - static int on_hls(int cid, std::string url, SrsRequest* req, std::string file, std::string ts_url, std::string m3u8, std::string m3u8_url, int sn, double duration); + static srs_error_t on_hls(int cid, std::string url, SrsRequest* req, std::string file, std::string ts_url, std::string m3u8, std::string m3u8_url, int sn, double duration); /** * when hls reap segment, callback. * @param url the api server url, to process the event. @@ -111,9 +111,9 @@ public: * @param nb_notify the max bytes to read from notify server. * @param cid the source connection cid, for the on_dvr is async call. */ - static int on_hls_notify(int cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify); + static srs_error_t on_hls_notify(int cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify); private: - static int do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, std::string& res); + static srs_error_t do_post(SrsHttpClient* hc, std::string url, std::string req, int& code, std::string& res); }; #endif diff --git a/trunk/src/app/srs_app_kafka.cpp b/trunk/src/app/srs_app_kafka.cpp index 89b672d6e..7c30c6a97 100644 --- a/trunk/src/app/srs_app_kafka.cpp +++ b/trunk/src/app/srs_app_kafka.cpp @@ -155,6 +155,7 @@ string SrsKafkaPartition::hostport() int SrsKafkaPartition::connect() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (transport) { return ret; @@ -162,8 +163,11 @@ int SrsKafkaPartition::connect() transport = new SrsTcpClient(host, port, SRS_KAFKA_PRODUCER_TIMEOUT); kafka = new SrsKafkaClient(transport); - if ((ret = transport->connect()) != ERROR_SUCCESS) { + if ((err = transport->connect()) != srs_success) { disconnect(); + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("connect to %s partition=%d failed. ret=%d", hostport().c_str(), id, ret); return ret; } @@ -196,14 +200,14 @@ SrsKafkaMessage::~SrsKafkaMessage() srs_freep(obj); } -int SrsKafkaMessage::call() +srs_error_t SrsKafkaMessage::call() { int ret = producer->send(key, obj); // the obj is manged by producer now. obj = NULL; - return ret; + return srs_error_new(ret, "kafka send"); } string SrsKafkaMessage::to_string() @@ -552,6 +556,7 @@ srs_error_t SrsKafkaProducer::do_cycle() int SrsKafkaProducer::request_metadata() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // ignore when disabled. if (!enabled) { @@ -588,7 +593,10 @@ int SrsKafkaProducer::request_metadata() SrsAutoFree(SrsKafkaClient, kafka); // reconnect to kafka server. - if ((ret = transport->connect()) != ERROR_SUCCESS) { + if ((err = transport->connect()) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("kafka connect %s:%d failed. ret=%d", server.c_str(), port, ret); return ret; } diff --git a/trunk/src/app/srs_app_kafka.hpp b/trunk/src/app/srs_app_kafka.hpp index 7018f70b3..466b348b1 100644 --- a/trunk/src/app/srs_app_kafka.hpp +++ b/trunk/src/app/srs_app_kafka.hpp @@ -89,7 +89,7 @@ public: virtual ~SrsKafkaMessage(); // interface ISrsAsyncCallTask public: - virtual int call(); + virtual srs_error_t call(); virtual std::string to_string(); }; diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 94506b826..e7d37e5cc 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -502,6 +502,7 @@ int SrsRtmpConn::service_cycle() int SrsRtmpConn::stream_service_cycle() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; @@ -563,7 +564,10 @@ int SrsRtmpConn::stream_service_cycle() srs_error("start to play stream failed. ret=%d", ret); return ret; } - if ((ret = http_hooks_on_play()) != ERROR_SUCCESS) { + if ((err = http_hooks_on_play()) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("http hook on_play failed. ret=%d", ret); return ret; } @@ -617,6 +621,7 @@ int SrsRtmpConn::stream_service_cycle() int SrsRtmpConn::check_vhost(bool try_default_vhost) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; srs_assert(req != NULL); @@ -647,7 +652,11 @@ int SrsRtmpConn::check_vhost(bool try_default_vhost) srs_verbose("check refer success."); } - if ((ret = http_hooks_on_connect()) != ERROR_SUCCESS) { + if ((err = http_hooks_on_connect()) != srs_success) { + srs_error("check vhost failed %s", srs_error_desc(err).c_str()); + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); return ret; } @@ -880,6 +889,7 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe int SrsRtmpConn::publishing(SrsSource* source) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; @@ -891,7 +901,10 @@ int SrsRtmpConn::publishing(SrsSource* source) srs_verbose("check publish_refer success."); } - if ((ret = http_hooks_on_publish()) != ERROR_SUCCESS) { + if ((err = http_hooks_on_publish()) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("http hook on_publish failed. ret=%d", ret); return ret; } @@ -1330,6 +1343,7 @@ void SrsRtmpConn::set_sock_options() int SrsRtmpConn::check_edge_token_traverse_auth() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; srs_assert(req); @@ -1350,7 +1364,10 @@ int SrsRtmpConn::check_edge_token_traverse_auth() SrsTcpClient* transport = new SrsTcpClient(server, port, SRS_EDGE_TOKEN_TRAVERSE_TMMS); SrsAutoFree(SrsTcpClient, transport); - if ((ret = transport->connect()) != ERROR_SUCCESS) { + if ((err = transport->connect()) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_warn("Illegal edge token, tcUrl=%s to server=%s, port=%d. ret=%d", req->tcUrl.c_str(), server.c_str(), port, ret); continue; } @@ -1410,14 +1427,14 @@ srs_error_t SrsRtmpConn::on_disconnect() return err; } -int SrsRtmpConn::http_hooks_on_connect() +srs_error_t SrsRtmpConn::http_hooks_on_connect() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { - return ret; + return err; } // the http hooks will cause context switch, @@ -1430,7 +1447,7 @@ int SrsRtmpConn::http_hooks_on_connect() if (!conf) { srs_info("ignore the empty http callback: on_connect"); - return ret; + return err; } hooks = conf->args; @@ -1438,13 +1455,12 @@ int SrsRtmpConn::http_hooks_on_connect() for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((ret = SrsHttpHooks::on_connect(url, req)) != ERROR_SUCCESS) { - srs_error("hook client on_connect failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + if ((err = SrsHttpHooks::on_connect(url, req)) != srs_success) { + return srs_error_wrap(err, "rtmp on_connect %s", url.c_str()); } } - return ret; + return err; } void SrsRtmpConn::http_hooks_on_close() @@ -1477,14 +1493,14 @@ void SrsRtmpConn::http_hooks_on_close() } } -int SrsRtmpConn::http_hooks_on_publish() +srs_error_t SrsRtmpConn::http_hooks_on_publish() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { - return ret; + return err; } // the http hooks will cause context switch, @@ -1497,7 +1513,7 @@ int SrsRtmpConn::http_hooks_on_publish() if (!conf) { srs_info("ignore the empty http callback: on_publish"); - return ret; + return err; } hooks = conf->args; @@ -1505,13 +1521,12 @@ int SrsRtmpConn::http_hooks_on_publish() for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((ret = SrsHttpHooks::on_publish(url, req)) != ERROR_SUCCESS) { - srs_error("hook client on_publish failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + if ((err = SrsHttpHooks::on_publish(url, req)) != srs_success) { + return srs_error_wrap(err, "rtmp on_publish %s", url.c_str()); } } - return ret; + return err; } void SrsRtmpConn::http_hooks_on_unpublish() @@ -1544,14 +1559,14 @@ void SrsRtmpConn::http_hooks_on_unpublish() } } -int SrsRtmpConn::http_hooks_on_play() +srs_error_t SrsRtmpConn::http_hooks_on_play() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { - return ret; + return err; } // the http hooks will cause context switch, @@ -1564,7 +1579,7 @@ int SrsRtmpConn::http_hooks_on_play() if (!conf) { srs_info("ignore the empty http callback: on_play"); - return ret; + return err; } hooks = conf->args; @@ -1572,13 +1587,12 @@ int SrsRtmpConn::http_hooks_on_play() for (int i = 0; i < (int)hooks.size(); i++) { std::string url = hooks.at(i); - if ((ret = SrsHttpHooks::on_play(url, req)) != ERROR_SUCCESS) { - srs_error("hook client on_play failed. url=%s, ret=%d", url.c_str(), ret); - return ret; + if ((err = SrsHttpHooks::on_play(url, req)) != srs_success) { + return srs_error_wrap(err, "rtmp on_play %s", url.c_str()); } } - return ret; + return err; } void SrsRtmpConn::http_hooks_on_stop() diff --git a/trunk/src/app/srs_app_rtmp_conn.hpp b/trunk/src/app/srs_app_rtmp_conn.hpp index 0921c874c..97f24dd37 100644 --- a/trunk/src/app/srs_app_rtmp_conn.hpp +++ b/trunk/src/app/srs_app_rtmp_conn.hpp @@ -173,11 +173,11 @@ private: */ virtual srs_error_t on_disconnect(); private: - virtual int http_hooks_on_connect(); + virtual srs_error_t http_hooks_on_connect(); virtual void http_hooks_on_close(); - virtual int http_hooks_on_publish(); + virtual srs_error_t http_hooks_on_publish(); virtual void http_hooks_on_unpublish(); - virtual int http_hooks_on_play(); + virtual srs_error_t http_hooks_on_play(); virtual void http_hooks_on_stop(); }; diff --git a/trunk/src/app/srs_app_rtsp.cpp b/trunk/src/app/srs_app_rtsp.cpp index 583af3702..7790ab7cd 100644 --- a/trunk/src/app/srs_app_rtsp.cpp +++ b/trunk/src/app/srs_app_rtsp.cpp @@ -231,11 +231,10 @@ SrsRtspConn::~SrsRtspConn() srs_error_t SrsRtspConn::serve() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; - if ((ret = skt->initialize(stfd)) != ERROR_SUCCESS) { - return srs_error_new(ret, "socket initialize"); + if ((err = skt->initialize(stfd)) != srs_success) { + return srs_error_wrap(err, "socket initialize"); } if ((err = trd->start()) != srs_success) { diff --git a/trunk/src/main/srs_main_ingest_hls.cpp b/trunk/src/main/srs_main_ingest_hls.cpp index 37ddb62c8..646dc49b8 100644 --- a/trunk/src/main/srs_main_ingest_hls.cpp +++ b/trunk/src/main/srs_main_ingest_hls.cpp @@ -366,17 +366,24 @@ int SrsIngestHlsInput::parseAac(ISrsAacHandler* handler, char* body, int nb_body int SrsIngestHlsInput::parseM3u8(SrsHttpUri* url, double& td, double& duration) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsHttpClient client; srs_trace("parse input hls %s", url->get_url().c_str()); - if ((ret = client.initialize(url->get_host(), url->get_port())) != ERROR_SUCCESS) { + if ((err = client.initialize(url->get_host(), url->get_port())) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("connect to server failed. ret=%d", ret); return ret; } ISrsHttpMessage* msg = NULL; - if ((ret = client.get(url->get_path(), "", &msg)) != ERROR_SUCCESS) { + if ((err = client.get(url->get_path(), "", &msg)) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("HTTP GET %s failed. ret=%d", url->get_url().c_str(), ret); return ret; } @@ -575,6 +582,7 @@ void SrsIngestHlsInput::remove_dirty() int SrsIngestHlsInput::SrsTsPiece::fetch(string m3u8) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (skip || sent || !body.empty()) { return ret; @@ -598,7 +606,10 @@ int SrsIngestHlsInput::SrsTsPiece::fetch(string m3u8) } ISrsHttpMessage* msg = NULL; - if ((ret = client.get(uri.get_path(), "", &msg)) != ERROR_SUCCESS) { + if ((err = client.get(uri.get_path(), "", &msg)) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("HTTP GET %s failed. ret=%d", uri.get_url().c_str(), ret); return ret; } diff --git a/trunk/src/service/srs_service_http_client.cpp b/trunk/src/service/srs_service_http_client.cpp index aa0cb8c46..c7c26d895 100644 --- a/trunk/src/service/srs_service_http_client.cpp +++ b/trunk/src/service/srs_service_http_client.cpp @@ -53,16 +53,16 @@ SrsHttpClient::~SrsHttpClient() } // TODO: FIXME: use ms for timeout. -int SrsHttpClient::initialize(string h, int p, int64_t tm) +srs_error_t SrsHttpClient::initialize(string h, int p, int64_t tm) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; srs_freep(parser); parser = new SrsHttpParser(); if ((ret = parser->initialize(HTTP_RESPONSE, false)) != ERROR_SUCCESS) { - srs_error("initialize parser failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "http: init parser"); } // Always disconnect the transport. @@ -83,7 +83,7 @@ int SrsHttpClient::initialize(string h, int p, int64_t tm) headers["User-Agent"] = RTMP_SIG_SRS_SERVER; headers["Content-Type"] = "application/json"; - return ret; + return err; } SrsHttpClient* SrsHttpClient::set_header(string k, string v) @@ -93,18 +93,18 @@ SrsHttpClient* SrsHttpClient::set_header(string k, string v) return this; } -int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg) +srs_error_t SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg) { *ppmsg = NULL; int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // always set the content length. headers["Content-Length"] = srs_int2str(req.length()); - if ((ret = connect()) != ERROR_SUCCESS) { - srs_warn("http connect server failed. ret=%d", ret); - return ret; + if ((err = connect()) != srs_success) { + return srs_error_wrap(err, "http: connect server"); } // send POST request to uri @@ -122,14 +122,12 @@ int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg) if ((ret = transport->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) { // Disconnect the transport when channel error, reconnect for next operation. disconnect(); - srs_error("write http post failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "http: write"); } ISrsHttpMessage* msg = NULL; if ((ret = parser->parse_message(transport, NULL, &msg)) != ERROR_SUCCESS) { - srs_error("parse http post response failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "http: parse response"); } srs_assert(msg); @@ -140,21 +138,21 @@ int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg) } srs_info("parse http post response success."); - return ret; + return err; } -int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg) +srs_error_t SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg) { *ppmsg = NULL; int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // always set the content length. headers["Content-Length"] = srs_int2str(req.length()); - if ((ret = connect()) != ERROR_SUCCESS) { - srs_warn("http connect server failed. ret=%d", ret); - return ret; + if ((err = connect()) != srs_success) { + return srs_error_wrap(err, "http: connect server"); } // send POST request to uri @@ -172,14 +170,12 @@ int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg) if ((ret = transport->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) { // Disconnect the transport when channel error, reconnect for next operation. disconnect(); - srs_error("write http get failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "http: write"); } ISrsHttpMessage* msg = NULL; if ((ret = parser->parse_message(transport, NULL, &msg)) != ERROR_SUCCESS) { - srs_error("parse http post response failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "http: parse response"); } srs_assert(msg); @@ -190,7 +186,7 @@ int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg) } srs_info("parse http get response success."); - return ret; + return err; } void SrsHttpClient::set_recv_timeout(int64_t tm) @@ -218,20 +214,19 @@ void SrsHttpClient::disconnect() srs_freep(transport); } -int SrsHttpClient::connect() +srs_error_t SrsHttpClient::connect() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // When transport connected, ignore. if (transport) { - return ret; + return err; } transport = new SrsTcpClient(host, port, timeout); - if ((ret = transport->connect()) != ERROR_SUCCESS) { + if ((err = transport->connect()) != srs_success) { disconnect(); - srs_warn("http client failed, server=%s, port=%d, timeout=%" PRId64 ", ret=%d", host.c_str(), port, timeout, ret); - return ret; + return srs_error_wrap(err, "http: tcp connect %s:%d to=%d", host.c_str(), port, (int)timeout); } srs_info("connect to server success. server=%s, port=%d", host.c_str(), port); @@ -241,6 +236,6 @@ int SrsHttpClient::connect() kbps->set_io(transport, transport); - return ret; + return err; } diff --git a/trunk/src/service/srs_service_http_client.hpp b/trunk/src/service/srs_service_http_client.hpp index 201203cd6..0c58fc5bc 100644 --- a/trunk/src/service/srs_service_http_client.hpp +++ b/trunk/src/service/srs_service_http_client.hpp @@ -75,7 +75,7 @@ public: * @param tm The underlayer TCP transport timeout in ms. * @remark we will set default values in headers, which can be override by set_header. */ - virtual int initialize(std::string h, int p, int64_t tm = SRS_HTTP_CLIENT_TMMS); + virtual srs_error_t initialize(std::string h, int p, int64_t tm = SRS_HTTP_CLIENT_TMMS); /** * Set HTTP request header in header[k]=v. * @return the HTTP client itself. @@ -89,7 +89,7 @@ public: * @param ppmsg output the http message to read the response. * @remark user must free the ppmsg if not NULL. */ - virtual int post(std::string path, std::string req, ISrsHttpMessage** ppmsg); + virtual srs_error_t post(std::string path, std::string req, ISrsHttpMessage** ppmsg); /** * to get data from the uri. * @param the path to request on. @@ -97,14 +97,14 @@ public: * @param ppmsg output the http message to read the response. * @remark user must free the ppmsg if not NULL. */ - virtual int get(std::string path, std::string req, ISrsHttpMessage** ppmsg); + virtual srs_error_t get(std::string path, std::string req, ISrsHttpMessage** ppmsg); private: virtual void set_recv_timeout(int64_t tm); public: virtual void kbps_sample(const char* label, int64_t age); private: virtual void disconnect(); - virtual int connect(); + virtual srs_error_t connect(); }; #endif diff --git a/trunk/src/service/srs_service_rtmp_conn.cpp b/trunk/src/service/srs_service_rtmp_conn.cpp index d806742d7..5d778a1e2 100644 --- a/trunk/src/service/srs_service_rtmp_conn.cpp +++ b/trunk/src/service/srs_service_rtmp_conn.cpp @@ -60,6 +60,7 @@ SrsBasicRtmpClient::~SrsBasicRtmpClient() int SrsBasicRtmpClient::connect() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; close(); @@ -67,8 +68,11 @@ int SrsBasicRtmpClient::connect() client = new SrsRtmpClient(transport); kbps->set_io(transport, transport); - if ((ret = transport->connect()) != ERROR_SUCCESS) { + if ((err = transport->connect()) != srs_success) { close(); + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); return ret; } diff --git a/trunk/src/service/srs_service_st.cpp b/trunk/src/service/srs_service_st.cpp index 025db6118..9feb8aedb 100644 --- a/trunk/src/service/srs_service_st.cpp +++ b/trunk/src/service/srs_service_st.cpp @@ -102,9 +102,9 @@ srs_thread_t srs_thread_self() return (srs_thread_t)st_thread_self(); } -int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd) +srs_error_t srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; st_utime_t timeout = ST_UTIME_NO_TIMEOUT; if (tm != SRS_CONSTS_NO_TMMS) { @@ -117,9 +117,7 @@ int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd) int sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == -1){ - ret = ERROR_SOCKET_CREATE; - srs_error("create socket error. ret=%d", ret); - return ret; + return srs_error_new(ERROR_SOCKET_CREATE, "create socket"); } srs_fd_close_exec(sock); @@ -127,17 +125,13 @@ int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd) srs_assert(!stfd); stfd = st_netfd_open_socket(sock); if(stfd == NULL){ - ret = ERROR_ST_OPEN_SOCKET; - srs_error("st_netfd_open_socket failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_ST_OPEN_SOCKET, "open socket"); } // connect to server. std::string ip = srs_dns_resolve(server); if (ip.empty()) { - ret = ERROR_SYSTEM_IP_INVALID; - srs_error("dns resolve server error, ip empty. ret=%d", ret); - goto failed; + return srs_error_new(ERROR_SYSTEM_IP_INVALID, "resolve server %s", server.c_str()); } addr.sin_family = AF_INET; @@ -145,20 +139,19 @@ int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd) addr.sin_addr.s_addr = inet_addr(ip.c_str()); if (st_connect((st_netfd_t)stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), timeout) == -1){ - ret = ERROR_ST_CONNECT; - srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret); + err = srs_error_new(ERROR_ST_CONNECT, "connect to %s:%d", ip.c_str(), port); goto failed; } srs_info("connect ok. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port); *pstfd = stfd; - return ret; + return err; failed: if (stfd) { srs_close_stfd(stfd); } - return ret; + return err; } srs_cond_t srs_cond_new() @@ -252,10 +245,10 @@ SrsStSocket::~SrsStSocket() { } -int SrsStSocket::initialize(srs_netfd_t fd) +srs_error_t SrsStSocket::initialize(srs_netfd_t fd) { stfd = fd; - return ERROR_SUCCESS; + return srs_success; } bool SrsStSocket::is_never_timeout(int64_t tm) @@ -444,23 +437,22 @@ SrsTcpClient::~SrsTcpClient() srs_freep(io); } -int SrsTcpClient::connect() +srs_error_t SrsTcpClient::connect() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; close(); srs_assert(stfd == NULL); - if ((ret = srs_socket_connect(host, port, timeout, &stfd)) != ERROR_SUCCESS) { - srs_error("connect tcp://%s:%d failed, to=%" PRId64 "ms. ret=%d", host.c_str(), port, timeout, ret); - return ret; + if ((err = srs_socket_connect(host, port, timeout, &stfd)) != srs_success) { + return srs_error_wrap(err, "tcp: connect %s:%d to=%d", host.c_str(), port, (int)timeout); } - if ((ret = io->initialize(stfd)) != ERROR_SUCCESS) { - return ret; + if ((err = io->initialize(stfd)) != srs_success) { + return srs_error_wrap(err, "tcp: init socket object"); } - return ret; + return err; } void SrsTcpClient::close() diff --git a/trunk/src/service/srs_service_st.hpp b/trunk/src/service/srs_service_st.hpp index 0ad8bc49a..a900bdc12 100644 --- a/trunk/src/service/srs_service_st.hpp +++ b/trunk/src/service/srs_service_st.hpp @@ -57,7 +57,7 @@ extern srs_thread_t srs_thread_self(); // client open socket and connect to server. // @param tm The timeout in ms. -extern int srs_socket_connect(std::string server, int port, int64_t tm, srs_netfd_t* pstfd); +extern srs_error_t srs_socket_connect(std::string server, int port, int64_t tm, srs_netfd_t* pstfd); // Wrap for coroutine. extern srs_cond_t srs_cond_new(); @@ -126,7 +126,7 @@ public: virtual ~SrsStSocket(); public: // Initialize the socket with stfd, user must manage it. - virtual int initialize(srs_netfd_t fd); + virtual srs_error_t initialize(srs_netfd_t fd); public: virtual bool is_never_timeout(int64_t tm); virtual void set_recv_timeout(int64_t tm); @@ -182,14 +182,14 @@ public: * Connect to server over TCP. * @remark We will close the exists connection before do connect. */ - virtual int connect(); + virtual srs_error_t connect(); private: /** * Close the connection to server. * @remark User should never use the client when close it. */ virtual void close(); - // interface ISrsProtocolReaderWriter +// interface ISrsProtocolReaderWriter public: virtual bool is_never_timeout(int64_t tm); virtual void set_recv_timeout(int64_t tm);