1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #913, http callback use complex error

This commit is contained in:
winlin 2017-09-22 19:54:50 +08:00
parent 59b53dab8b
commit 20a42599f3
22 changed files with 264 additions and 268 deletions

View file

@ -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);
}

View file

@ -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.

View file

@ -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) {

View file

@ -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()

View file

@ -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();
};

View file

@ -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;

View file

@ -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()

View file

@ -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();
};

View file

@ -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) {

View file

@ -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;
}

View file

@ -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

View file

@ -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;
}

View file

@ -89,7 +89,7 @@ public:
virtual ~SrsKafkaMessage();
// interface ISrsAsyncCallTask
public:
virtual int call();
virtual srs_error_t call();
virtual std::string to_string();
};

View file

@ -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()

View file

@ -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();
};

View file

@ -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) {