From a20e2c3ef67f959a3927abf7f43f50358bf74764 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 10 Jun 2017 15:20:48 +0800 Subject: [PATCH] For #913, refine server utility --- trunk/src/app/srs_app_caster_flv.cpp | 7 +- trunk/src/app/srs_app_http_conn.cpp | 5 +- trunk/src/app/srs_app_http_static.cpp | 11 +- trunk/src/app/srs_app_http_stream.cpp | 7 +- trunk/src/app/srs_app_kafka.cpp | 20 +- trunk/src/app/srs_app_kafka.hpp | 4 +- trunk/src/app/srs_app_server.cpp | 286 ++++++++++++------------- trunk/src/app/srs_app_server.hpp | 26 +-- trunk/src/kernel/srs_kernel_error.hpp | 1 + trunk/src/main/srs_main_ingest_hls.cpp | 27 +-- trunk/src/main/srs_main_server.cpp | 32 ++- trunk/src/protocol/srs_http_stack.cpp | 14 +- trunk/src/protocol/srs_http_stack.hpp | 2 +- trunk/src/service/srs_service_st.cpp | 22 +- trunk/src/service/srs_service_st.hpp | 2 +- 15 files changed, 222 insertions(+), 244 deletions(-) diff --git a/trunk/src/app/srs_app_caster_flv.cpp b/trunk/src/app/srs_app_caster_flv.cpp index 2dbc34f4d..6fdc18da8 100644 --- a/trunk/src/app/srs_app_caster_flv.cpp +++ b/trunk/src/app/srs_app_caster_flv.cpp @@ -63,8 +63,13 @@ SrsAppCasterFlv::~SrsAppCasterFlv() int SrsAppCasterFlv::initialize() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = http_mux->handle("/", this)) != ERROR_SUCCESS) { + if ((err = http_mux->handle("/", this)) != srs_success) { + // TODO: FIXME: Use error. + ret = srs_error_code(err); + srs_freep(err); + return ret; } diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 88f5c2626..e9a527cc3 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -267,12 +267,11 @@ SrsHttpServer::~SrsHttpServer() srs_error_t SrsHttpServer::initialize() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // for SRS go-sharp to detect the status of HTTP server of SRS HTTP FLV Cluster. - if ((ret = http_static->mux.handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) { - return srs_error_new(ret, "handle versin"); + if ((err = http_static->mux.handle("/api/v1/versions", new SrsGoApiVersion())) != srs_success) { + return srs_error_wrap(err, "handle versin"); } if ((err = http_stream->initialize()) != srs_success) { diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index 427f05b59..d6fcfef09 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -240,8 +240,8 @@ srs_error_t SrsHttpStaticServer::initialize() if (!default_root_exists) { // add root std::string dir = _srs_config->get_http_stream_dir(); - if ((ret = mux.handle("/", new SrsVodStream(dir))) != ERROR_SUCCESS) { - return srs_error_new(ret, "mount root dir=%s", dir.c_str()); + if ((err = mux.handle("/", new SrsVodStream(dir))) != srs_success) { + return srs_error_wrap(err, "mount root dir=%s", dir.c_str()); } srs_trace("http: root mount to %s", dir.c_str()); } @@ -252,6 +252,7 @@ srs_error_t SrsHttpStaticServer::initialize() int SrsHttpStaticServer::mount_vhost(string vhost, string& pmount) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // when vhost disabled, ignore. if (!_srs_config->get_vhost_enabled(vhost)) { @@ -279,7 +280,11 @@ int SrsHttpStaticServer::mount_vhost(string vhost, string& pmount) } // mount the http of vhost. - if ((ret = mux.handle(mount, new SrsVodStream(dir))) != ERROR_SUCCESS) { + if ((err = mux.handle(mount, new SrsVodStream(dir))) != srs_success) { + // TODO: FIXME: Use error. + ret = srs_error_code(err); + srs_freep(err); + srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret); return ret; } diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index 5ea1142a6..c3b8bb5e1 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -721,6 +721,7 @@ srs_error_t SrsHttpStreamServer::initialize() int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // the id to identify stream. std::string sid = r->get_stream_url(); @@ -767,7 +768,11 @@ int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r) // we must register the handler, then start the thread, // for the thread will cause thread switch context. // @see https://github.com/ossrs/srs/issues/404 - if ((ret = mux.handle(mount, entry->stream)) != ERROR_SUCCESS) { + if ((err = mux.handle(mount, entry->stream)) != srs_success) { + // TODO: FIXME: Use error. + ret = srs_error_code(err); + srs_freep(err); + srs_error("http: mount flv stream for vhost=%s failed. ret=%d", sid.c_str(), ret); return ret; } diff --git a/trunk/src/app/srs_app_kafka.cpp b/trunk/src/app/srs_app_kafka.cpp index 30ef8dddf..575c502e5 100644 --- a/trunk/src/app/srs_app_kafka.cpp +++ b/trunk/src/app/srs_app_kafka.cpp @@ -330,24 +330,23 @@ ISrsKafkaCluster::~ISrsKafkaCluster() // @global kafka event producer, user must use srs_initialize_kafka to initialize it. ISrsKafkaCluster* _srs_kafka = NULL; -int srs_initialize_kafka() +srs_error_t srs_initialize_kafka() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsKafkaProducer* kafka = new SrsKafkaProducer(); _srs_kafka = kafka; - if ((ret = kafka->initialize()) != ERROR_SUCCESS) { - srs_error("initialize the kafka producer failed. ret=%d", ret); - return ret; + if ((err = kafka->initialize()) != srs_success) { + return srs_error_wrap(err, "initialize kafka producer"); } if ((ret = kafka->start()) != ERROR_SUCCESS) { - srs_error("start kafka failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "start kafka producer"); } - return ret; + return err; } void srs_dispose_kafka() @@ -390,14 +389,11 @@ SrsKafkaProducer::~SrsKafkaProducer() srs_cond_destroy(metadata_expired); } -int SrsKafkaProducer::initialize() +srs_error_t SrsKafkaProducer::initialize() { - int ret = ERROR_SUCCESS; - enabled = _srs_config->get_kafka_enabled(); srs_info("initialize kafka ok, enabled=%d.", enabled); - - return ret; + return srs_success; } int SrsKafkaProducer::start() diff --git a/trunk/src/app/srs_app_kafka.hpp b/trunk/src/app/srs_app_kafka.hpp index 72b126c1e..916c00fa1 100644 --- a/trunk/src/app/srs_app_kafka.hpp +++ b/trunk/src/app/srs_app_kafka.hpp @@ -152,7 +152,7 @@ public: // @global kafka event producer. extern ISrsKafkaCluster* _srs_kafka; // kafka initialize and disposer for global object. -extern int srs_initialize_kafka(); +extern srs_error_t srs_initialize_kafka(); extern void srs_dispose_kafka(); /** @@ -178,7 +178,7 @@ public: SrsKafkaProducer(); virtual ~SrsKafkaProducer(); public: - virtual int initialize(); + virtual srs_error_t initialize(); virtual int start(); virtual void stop(); // internal: for worker to call task to send object. diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 271a7ad13..14c4a3111 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -380,24 +380,18 @@ SrsSignalManager::~SrsSignalManager() srs_freep(trd); } -int SrsSignalManager::initialize() +srs_error_t SrsSignalManager::initialize() { - int ret = ERROR_SUCCESS; - /* Create signal pipe */ if (pipe(sig_pipe) < 0) { - ret = ERROR_SYSTEM_CREATE_PIPE; - srs_error("create signal manager pipe failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CREATE_PIPE, "create pipe"); } if ((signal_read_stfd = srs_netfd_open(sig_pipe[0])) == NULL) { - ret = ERROR_SYSTEM_CREATE_PIPE; - srs_error("create signal manage st pipe failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CREATE_PIPE, "open pipe"); } - return ret; + return srs_success; } int SrsSignalManager::start() @@ -588,12 +582,11 @@ srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler) srs_error_t SrsServer::initialize_st() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // init st - if ((ret = srs_st_init()) != ERROR_SUCCESS) { - return srs_error_new(ret, "initialize st failed"); + if ((err = srs_st_init()) != srs_success) { + return srs_error_wrap(err, "initialize st failed"); } // @remark, st alloc segment use mmap, which only support 32757 threads, @@ -613,8 +606,8 @@ srs_error_t SrsServer::initialize_st() // initialize the conponents that depends on st. #ifdef SRS_AUTO_KAFKA - if ((ret = srs_initialize_kafka()) != ERROR_SUCCESS) { - return srs_error_new(ret, "initialize kafka"); + if ((err = srs_initialize_kafka()) != srs_success) { + return srs_error_wrap(err, "initialize kafka"); } #endif @@ -630,18 +623,16 @@ srs_error_t SrsServer::initialize_st() return err; } -int SrsServer::initialize_signal() +srs_error_t SrsServer::initialize_signal() { return signal_manager->initialize(); } -int SrsServer::acquire_pid_file() +srs_error_t SrsServer::acquire_pid_file() { - int ret = ERROR_SUCCESS; - // when srs in dolphin mode, no need the pid file. if (_srs_config->is_dolphin()) { - return ret; + return srs_success; } std::string pid_file = _srs_config->get_pid_file(); @@ -652,10 +643,8 @@ int SrsServer::acquire_pid_file() int fd; // open pid file - if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) < 0) { - ret = ERROR_SYSTEM_PID_ACQUIRE; - srs_error("open pid file %s error, ret=%#x", pid_file.c_str(), ret); - return ret; + if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) == -1) { + return srs_error_new(ERROR_SYSTEM_PID_ACQUIRE, "open pid file=%s", pid_file.c_str()); } // require write lock @@ -666,185 +655,173 @@ int SrsServer::acquire_pid_file() lock.l_whence = SEEK_SET; // SEEK_SET, SEEK_CUR, SEEK_END lock.l_len = 0; - if (fcntl(fd, F_SETLK, &lock) < 0) { + if (fcntl(fd, F_SETLK, &lock) == -1) { if(errno == EACCES || errno == EAGAIN) { - ret = ERROR_SYSTEM_PID_ALREADY_RUNNING; - srs_error("srs is already running! ret=%#x", ret); - return ret; + srs_error("srs is already running!"); + return srs_error_new(ERROR_SYSTEM_PID_ALREADY_RUNNING, "srs is already running"); } - - ret = ERROR_SYSTEM_PID_LOCK; - srs_error("require lock for file %s error! ret=%#x", pid_file.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_PID_LOCK, "access to pid=%s", pid_file.c_str()); } // truncate file - if (ftruncate(fd, 0) < 0) { - ret = ERROR_SYSTEM_PID_TRUNCATE_FILE; - srs_error("truncate pid file %s error! ret=%#x", pid_file.c_str(), ret); - return ret; + if (ftruncate(fd, 0) != 0) { + return srs_error_new(ERROR_SYSTEM_PID_TRUNCATE_FILE, "truncate pid file=%s", pid_file.c_str()); } // write the pid string pid = srs_int2str(getpid()); if (write(fd, pid.c_str(), pid.length()) != (int)pid.length()) { - ret = ERROR_SYSTEM_PID_WRITE_FILE; - srs_error("write our pid error! pid=%s file=%s ret=%#x", pid.c_str(), pid_file.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_PID_WRITE_FILE, "write pid=%d to file=%s", pid.c_str(), pid_file.c_str()); } // auto close when fork child process. int val; if ((val = fcntl(fd, F_GETFD, 0)) < 0) { - ret = ERROR_SYSTEM_PID_GET_FILE_INFO; - srs_error("fnctl F_GETFD error! file=%s ret=%#x", pid_file.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_PID_GET_FILE_INFO, "fcntl fd=%d", fd); } val |= FD_CLOEXEC; if (fcntl(fd, F_SETFD, val) < 0) { - ret = ERROR_SYSTEM_PID_SET_FILE_INFO; - srs_error("fcntl F_SETFD error! file=%s ret=%#x", pid_file.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_PID_SET_FILE_INFO, "lock file=%s fd=%d", pid_file.c_str(), fd); } srs_trace("write pid=%s to %s success!", pid.c_str(), pid_file.c_str()); pid_fd = fd; - return ret; + return srs_success; } -int SrsServer::listen() +srs_error_t SrsServer::listen() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = listen_rtmp()) != ERROR_SUCCESS) { - return ret; + if ((err = listen_rtmp()) != srs_success) { + return srs_error_wrap(err, "rtmp listen"); } - if ((ret = listen_http_api()) != ERROR_SUCCESS) { - return ret; + if ((err = listen_http_api()) != srs_success) { + return srs_error_wrap(err, "http api listen"); } - if ((ret = listen_http_stream()) != ERROR_SUCCESS) { - return ret; + if ((err = listen_http_stream()) != srs_success) { + return srs_error_wrap(err, "http stream listen"); } - if ((ret = listen_stream_caster()) != ERROR_SUCCESS) { - return ret; + if ((err = listen_stream_caster()) != srs_success) { + return srs_error_wrap(err, "stream caster listen"); } if ((ret = conn_manager->start()) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "connection manager"); } - return ret; + return err; } -int SrsServer::register_signal() +srs_error_t SrsServer::register_signal() { // start signal process thread. - return signal_manager->start(); + int ret = signal_manager->start(); + if (ret != ERROR_SUCCESS) { + return srs_error_new(ret, "signal manager start"); + } + return srs_success; } -int SrsServer::http_handle() +srs_error_t SrsServer::http_handle() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - srs_assert(http_api_mux); - if ((ret = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != srs_success) { + return srs_error_wrap(err, "handle not found"); } - if ((ret = http_api_mux->handle("/api/", new SrsGoApiApi())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/", new SrsGoApiApi())) != srs_success) { + return srs_error_wrap(err, "handle api"); } - if ((ret = http_api_mux->handle("/api/v1/", new SrsGoApiV1())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/", new SrsGoApiV1())) != srs_success) { + return srs_error_wrap(err, "handle v1"); } - if ((ret = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != srs_success) { + return srs_error_wrap(err, "handle versions"); } - if ((ret = http_api_mux->handle("/api/v1/summaries", new SrsGoApiSummaries())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/summaries", new SrsGoApiSummaries())) != srs_success) { + return srs_error_wrap(err, "handle summaries"); } - if ((ret = http_api_mux->handle("/api/v1/rusages", new SrsGoApiRusages())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/rusages", new SrsGoApiRusages())) != srs_success) { + return srs_error_wrap(err, "handle rusages"); } - if ((ret = http_api_mux->handle("/api/v1/self_proc_stats", new SrsGoApiSelfProcStats())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/self_proc_stats", new SrsGoApiSelfProcStats())) != srs_success) { + return srs_error_wrap(err, "handle self proc stats"); } - if ((ret = http_api_mux->handle("/api/v1/system_proc_stats", new SrsGoApiSystemProcStats())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/system_proc_stats", new SrsGoApiSystemProcStats())) != srs_success) { + return srs_error_wrap(err, "handle system proc stats"); } - if ((ret = http_api_mux->handle("/api/v1/meminfos", new SrsGoApiMemInfos())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/meminfos", new SrsGoApiMemInfos())) != srs_success) { + return srs_error_wrap(err, "handle meminfos"); } - if ((ret = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != srs_success) { + return srs_error_wrap(err, "handle authors"); } - if ((ret = http_api_mux->handle("/api/v1/features", new SrsGoApiFeatures())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/features", new SrsGoApiFeatures())) != srs_success) { + return srs_error_wrap(err, "handle features"); } - if ((ret = http_api_mux->handle("/api/v1/vhosts/", new SrsGoApiVhosts())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/vhosts/", new SrsGoApiVhosts())) != srs_success) { + return srs_error_wrap(err, "handle vhosts"); } - if ((ret = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != srs_success) { + return srs_error_wrap(err, "handle streams"); } - if ((ret = http_api_mux->handle("/api/v1/clients/", new SrsGoApiClients())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/clients/", new SrsGoApiClients())) != srs_success) { + return srs_error_wrap(err, "handle clients"); } - if ((ret = http_api_mux->handle("/api/v1/raw", new SrsGoApiRaw(this))) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/raw", new SrsGoApiRaw(this))) != srs_success) { + return srs_error_wrap(err, "handle raw"); } // test the request info. - if ((ret = http_api_mux->handle("/api/v1/tests/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/tests/requests", new SrsGoApiRequests())) != srs_success) { + return srs_error_wrap(err, "handle tests requests"); } // test the error code response. - if ((ret = http_api_mux->handle("/api/v1/tests/errors", new SrsGoApiError())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/tests/errors", new SrsGoApiError())) != srs_success) { + return srs_error_wrap(err, "handle tests errors"); } // test the redirect mechenism. - if ((ret = http_api_mux->handle("/api/v1/tests/redirects", new SrsHttpRedirectHandler("/api/v1/tests/errors", SRS_CONSTS_HTTP_MovedPermanently))) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("/api/v1/tests/redirects", new SrsHttpRedirectHandler("/api/v1/tests/errors", SRS_CONSTS_HTTP_MovedPermanently))) != srs_success) { + return srs_error_wrap(err, "handle tests redirects"); } // test the http vhost. - if ((ret = http_api_mux->handle("error.srs.com/api/v1/tests/errors", new SrsGoApiError())) != ERROR_SUCCESS) { - return ret; + if ((err = http_api_mux->handle("error.srs.com/api/v1/tests/errors", new SrsGoApiError())) != srs_success) { + return srs_error_wrap(err, "handle tests errors for error.srs.com"); } // TODO: FIXME: for console. // TODO: FIXME: support reload. std::string dir = _srs_config->get_http_stream_dir() + "/console"; - if ((ret = http_api_mux->handle("/console/", new SrsHttpFileServer(dir))) != ERROR_SUCCESS) { - srs_error("http: mount console dir=%s failed. ret=%d", dir.c_str(), ret); - return ret; + if ((err = http_api_mux->handle("/console/", new SrsHttpFileServer(dir))) != srs_success) { + return srs_error_wrap(err, "handle console at %s", dir.c_str()); } srs_trace("http: api mount /console to %s", dir.c_str()); - return ret; + return err; } -int SrsServer::ingest() +srs_error_t SrsServer::ingest() { int ret = ERROR_SUCCESS; #ifdef SRS_AUTO_INGEST if ((ret = ingester->start()) != ERROR_SUCCESS) { - srs_error("start ingest streams failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "ingest start"); } #endif - return ret; + return srs_success; } -int SrsServer::cycle() +srs_error_t SrsServer::cycle() { - int ret = ERROR_SUCCESS; - - ret = do_cycle(); + srs_error_t err = do_cycle(); #ifdef SRS_AUTO_GPERF_MC destroy(); @@ -866,7 +843,7 @@ int SrsServer::cycle() exit(0); #endif - return ret; + return err; } @@ -919,9 +896,10 @@ void SrsServer::on_signal(int signo) } } -int SrsServer::do_cycle() +srs_error_t SrsServer::do_cycle() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // find the max loop int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES); @@ -943,8 +921,7 @@ int SrsServer::do_cycle() // TODO: FIXME: use SrsHourGlass. while (true) { if (handler && (ret = handler->on_cycle()) != ERROR_SUCCESS) { - srs_error("cycle handle failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "handle callback"); } // the interval in config. @@ -958,14 +935,13 @@ int SrsServer::do_cycle() // asprocess check. if (asprocess && ::getppid() != ppid) { - srs_warn("asprocess ppid changed from %d to %d", ppid, ::getppid()); - return ret; + return srs_error_new(ERROR_ASPROCESS_PPID, "asprocess ppid changed from %d to %d", ppid, ::getppid()); } // gracefully quit for SIGINT or SIGTERM. if (signal_gracefully_quit) { srs_trace("cleanup for gracefully terminate."); - return ret; + return err; } // for gperf heap checker, @@ -976,7 +952,7 @@ int SrsServer::do_cycle() #ifdef SRS_AUTO_GPERF_MC if (signal_gmc_stop) { srs_warn("gmc got singal to stop server."); - return ret; + return err; } #endif @@ -986,8 +962,7 @@ int SrsServer::do_cycle() srs_info("get signal to persistence config to file."); if ((ret = _srs_config->persistence()) != ERROR_SUCCESS) { - srs_error("persistence config to file failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "config persistence to file"); } srs_trace("persistence config to file success."); } @@ -998,15 +973,14 @@ int SrsServer::do_cycle() srs_info("get signal to reload the config."); if ((ret = _srs_config->reload()) != ERROR_SUCCESS) { - srs_error("reload config failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "config reload"); } srs_trace("reload config success."); } // notice the stream sources to cycle. if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "source cycle"); } // update the cache time @@ -1056,10 +1030,10 @@ int SrsServer::do_cycle() } } - return ret; + return err; } -int SrsServer::listen_rtmp() +srs_error_t SrsServer::listen_rtmp() { int ret = ERROR_SUCCESS; @@ -1078,15 +1052,14 @@ int SrsServer::listen_rtmp() srs_parse_endpoint(ip_ports[i], ip, port); if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) { - srs_error("RTMP stream listen at %s:%d failed. ret=%d", ip.c_str(), port, ret); - return ret; + srs_error_new(ret, "rtmp listen %s:%d", ip.c_str(), port); } } - return ret; + return srs_success; } -int SrsServer::listen_http_api() +srs_error_t SrsServer::listen_http_api() { int ret = ERROR_SUCCESS; @@ -1102,15 +1075,14 @@ int SrsServer::listen_http_api() srs_parse_endpoint(ep, ip, port); if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) { - srs_error("HTTP api listen at %s:%d failed. ret=%d", ip.c_str(), port, ret); - return ret; + return srs_error_new(ret, "http api listen %s:%d", ip.c_str(), port); } } - return ret; + return srs_success; } -int SrsServer::listen_http_stream() +srs_error_t SrsServer::listen_http_stream() { int ret = ERROR_SUCCESS; @@ -1126,15 +1098,14 @@ int SrsServer::listen_http_stream() srs_parse_endpoint(ep, ip, port); if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) { - srs_error("HTTP stream listen at %s:%d failed. ret=%d", ip.c_str(), port, ret); - return ret; + return srs_error_new(ret, "http stream listen %s:%d", ip.c_str(), port); } } - return ret; + return srs_success; } -int SrsServer::listen_stream_caster() +srs_error_t SrsServer::listen_stream_caster() { int ret = ERROR_SUCCESS; @@ -1160,9 +1131,7 @@ int SrsServer::listen_stream_caster() } else if (srs_stream_caster_is_flv(caster)) { listener = new SrsHttpFlvListener(this, SrsListenerFlv, stream_caster); } else { - ret = ERROR_STREAM_CASTER_ENGINE; - srs_error("unsupported stream caster %s. ret=%d", caster.c_str(), ret); - return ret; + return srs_error_new(ERROR_STREAM_CASTER_ENGINE, "invalid caster %s", caster.c_str()); } srs_assert(listener != NULL); @@ -1170,20 +1139,17 @@ int SrsServer::listen_stream_caster() int port = _srs_config->get_stream_caster_listen(stream_caster); if (port <= 0) { - ret = ERROR_STREAM_CASTER_PORT; - srs_error("invalid stream caster port %d. ret=%d", port, ret); - return ret; + return srs_error_new(ERROR_STREAM_CASTER_PORT, "invalid port=%d", port); } // TODO: support listen at <[ip:]port> if ((ret = listener->listen("0.0.0.0", port)) != ERROR_SUCCESS) { - srs_error("StreamCaster listen at port %d failed. ret=%d", port, ret); - return ret; + return srs_error_new(ret, "listen at %d", port); } } #endif - return ret; + return srs_success; } void SrsServer::close_listeners(SrsListenerType type) @@ -1334,7 +1300,11 @@ void SrsServer::remove(ISrsConnection* c) int SrsServer::on_reload_listen() { - return listen(); + // TODO: FIXME: Use error. + srs_error_t err = listen(); + int ret = srs_error_code(err); + srs_freep(err); + return ret; } int SrsServer::on_reload_pid() @@ -1344,7 +1314,11 @@ int SrsServer::on_reload_pid() pid_fd = -1; } - return acquire_pid_file(); + // TODO: FIXME: Use error. + srs_error_t err = acquire_pid_file(); + int ret = srs_error_code(err); + srs_freep(err); + return ret; } int SrsServer::on_reload_vhost_added(std::string vhost) @@ -1377,7 +1351,11 @@ int SrsServer::on_reload_vhost_removed(std::string /*vhost*/) int SrsServer::on_reload_http_api_enabled() { - return listen_http_api(); + // TODO: FIXME: Use error. + srs_error_t err = listen_http_api(); + int ret = srs_error_code(err); + srs_freep(err); + return ret; } int SrsServer::on_reload_http_api_disabled() @@ -1388,7 +1366,11 @@ int SrsServer::on_reload_http_api_disabled() int SrsServer::on_reload_http_stream_enabled() { - return listen_http_stream(); + // TODO: FIXME: Use error. + srs_error_t err = listen_http_stream(); + int ret = srs_error_code(err); + srs_freep(err); + return ret; } int SrsServer::on_reload_http_stream_disabled() diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index 77be38a8b..e82ebab1c 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -194,7 +194,7 @@ public: SrsSignalManager(SrsServer* s); virtual ~SrsSignalManager(); public: - virtual int initialize(); + virtual srs_error_t initialize(); virtual int start(); // interface ISrsEndlessThreadHandler. public: @@ -303,13 +303,13 @@ public: */ virtual srs_error_t initialize(ISrsServerCycle* cycle_handler); virtual srs_error_t initialize_st(); - virtual int initialize_signal(); - virtual int acquire_pid_file(); - virtual int listen(); - virtual int register_signal(); - virtual int http_handle(); - virtual int ingest(); - virtual int cycle(); + virtual srs_error_t initialize_signal(); + virtual srs_error_t acquire_pid_file(); + virtual srs_error_t listen(); + virtual srs_error_t register_signal(); + virtual srs_error_t http_handle(); + virtual srs_error_t ingest(); + virtual srs_error_t cycle(); // server utilities. public: /** @@ -333,14 +333,14 @@ private: * update the global static data, for instance, the current time, * the cpu/mem/network statistic. */ - virtual int do_cycle(); + virtual srs_error_t do_cycle(); /** * listen at specified protocol. */ - virtual int listen_rtmp(); - virtual int listen_http_api(); - virtual int listen_http_stream(); - virtual int listen_stream_caster(); + virtual srs_error_t listen_rtmp(); + virtual srs_error_t listen_http_api(); + virtual srs_error_t listen_http_stream(); + virtual srs_error_t listen_stream_caster(); /** * close the listeners for specified type, * remove the listen object from manager. diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index f8406fcd6..0fb06adbc 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -109,6 +109,7 @@ #define ERROR_THREAD_INTERRUPED 1070 #define ERROR_THREAD_TERMINATED 1071 #define ERROR_THREAD_DUMMY 1072 +#define ERROR_ASPROCESS_PPID 1073 /////////////////////////////////////////////////////// // RTMP protocol error. diff --git a/trunk/src/main/srs_main_ingest_hls.cpp b/trunk/src/main/srs_main_ingest_hls.cpp index db5dc8419..37ddb62c8 100644 --- a/trunk/src/main/srs_main_ingest_hls.cpp +++ b/trunk/src/main/srs_main_ingest_hls.cpp @@ -48,7 +48,7 @@ using namespace std; #include // pre-declare -int proxy_hls2rtmp(std::string hls, std::string rtmp); +srs_error_t proxy_hls2rtmp(std::string hls, std::string rtmp); // @global log and context. ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false); @@ -110,7 +110,11 @@ int main(int argc, char** argv) srs_trace("input: %s", in_hls_url.c_str()); srs_trace("output: %s", out_rtmp_url.c_str()); - return proxy_hls2rtmp(in_hls_url, out_rtmp_url); + srs_error_t err = proxy_hls2rtmp(in_hls_url, out_rtmp_url); + + int ret = srs_error_code(err); + srs_freep(err); + return ret; } class ISrsAacHandler @@ -1283,34 +1287,31 @@ public: } }; -int proxy_hls2rtmp(string hls, string rtmp) +srs_error_t proxy_hls2rtmp(string hls, string rtmp) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // init st. - if ((ret = srs_st_init()) != ERROR_SUCCESS) { - srs_error("init st failed. ret=%d", ret); - return ret; + if ((err = srs_st_init()) != srs_success) { + return srs_error_wrap(err, "initialize st"); } SrsHttpUri hls_uri, rtmp_uri; if ((ret = hls_uri.initialize(hls)) != ERROR_SUCCESS) { - srs_error("hls uri invalid. ret=%d", ret); - return ret; + return srs_error_new(ret, "hls parse uri=%s", hls.c_str()); } if ((ret = rtmp_uri.initialize(rtmp)) != ERROR_SUCCESS) { - srs_error("rtmp uri invalid. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp parse uri=%s", rtmp.c_str()); } SrsIngestHlsContext context(&hls_uri, &rtmp_uri); for (;;) { if ((ret = context.proxy()) != ERROR_SUCCESS) { - srs_error("proxy hls to rtmp failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "proxy hls to rtmp"); } } - return ret; + return err; } diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 3808e1de9..44b733c54 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -363,7 +363,6 @@ string srs_getenv(const char* name) srs_error_t run(SrsServer* svr) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; /** @@ -418,44 +417,43 @@ srs_error_t run(SrsServer* svr) return srs_error_wrap(err, "daemon run master"); } - return srs_success; + return err; } srs_error_t run_master(SrsServer* svr) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; if ((err = svr->initialize_st()) != srs_success) { return srs_error_wrap(err, "initialize st"); } - if ((ret = svr->initialize_signal()) != ERROR_SUCCESS) { - return srs_error_new(ret, "initialize signal"); + if ((err = svr->initialize_signal()) != srs_success) { + return srs_error_wrap(err, "initialize signal"); } - if ((ret = svr->acquire_pid_file()) != ERROR_SUCCESS) { - return srs_error_new(ret, "acquire pid file"); + if ((err = svr->acquire_pid_file()) != srs_success) { + return srs_error_wrap(err, "acquire pid file"); } - if ((ret = svr->listen()) != ERROR_SUCCESS) { - return srs_error_new(ret, "listen"); + if ((err = svr->listen()) != ERROR_SUCCESS) { + return srs_error_wrap(err, "listen"); } - if ((ret = svr->register_signal()) != ERROR_SUCCESS) { - return srs_error_new(ret, "register signal"); + if ((err = svr->register_signal()) != srs_success) { + return srs_error_wrap(err, "register signal"); } - if ((ret = svr->http_handle()) != ERROR_SUCCESS) { - return srs_error_new(ret, "http handle"); + if ((err = svr->http_handle()) != srs_success) { + return srs_error_wrap(err, "http handle"); } - if ((ret = svr->ingest()) != ERROR_SUCCESS) { - return srs_error_new(ret, "ingest"); + if ((err = svr->ingest()) != srs_success) { + return srs_error_wrap(err, "ingest"); } - if ((ret = svr->cycle()) != ERROR_SUCCESS) { - return srs_error_new(ret, "main cycle"); + if ((err = svr->cycle()) != srs_success) { + return srs_error_wrap(err, "main cycle"); } return err; diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index 29b37e91a..460ec53b6 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -560,24 +560,18 @@ void SrsHttpServeMux::unhijack(ISrsHttpMatchHijacker* h) hijackers.erase(it); } -int SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handler) +srs_error_t SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handler) { - int ret = ERROR_SUCCESS; - srs_assert(handler); if (pattern.empty()) { - ret = ERROR_HTTP_PATTERN_EMPTY; - srs_error("http: empty pattern. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HTTP_PATTERN_EMPTY, "empty pattern"); } if (entries.find(pattern) != entries.end()) { SrsHttpMuxEntry* exists = entries[pattern]; if (exists->explicit_match) { - ret = ERROR_HTTP_PATTERN_DUPLICATED; - srs_error("http: multiple registrations for %s. ret=%d", pattern.c_str(), ret); - return ret; + return srs_error_new(ERROR_HTTP_PATTERN_DUPLICATED, "pattern=%s exists", pattern.c_str()); } } @@ -632,7 +626,7 @@ int SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handler) } } - return ret; + return srs_success; } bool SrsHttpServeMux::can_serve(ISrsHttpMessage* r) diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index f3e93356b..80ac0aa78 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -426,7 +426,7 @@ public: public: // Handle registers the handler for the given pattern. // If a handler already exists for pattern, Handle panics. - virtual int handle(std::string pattern, ISrsHttpHandler* handler); + virtual srs_error_t handle(std::string pattern, ISrsHttpHandler* handler); // whether the http muxer can serve the specified message, // if not, user can try next muxer. virtual bool can_serve(ISrsHttpMessage* r); diff --git a/trunk/src/service/srs_service_st.cpp b/trunk/src/service/srs_service_st.cpp index 858fe2312..025db6118 100644 --- a/trunk/src/service/srs_service_st.cpp +++ b/trunk/src/service/srs_service_st.cpp @@ -49,37 +49,29 @@ bool srs_st_epoll_is_supported(void) } #endif -int srs_st_init() +srs_error_t srs_st_init() { - int ret = ERROR_SUCCESS; - #ifdef __linux__ // check epoll, some old linux donot support epoll. // @see https://github.com/ossrs/srs/issues/162 if (!srs_st_epoll_is_supported()) { - ret = ERROR_ST_SET_EPOLL; - srs_error("epoll required on Linux. ret=%d", ret); - return ret; + return srs_error_new(ERROR_ST_SET_EPOLL, "linux epoll disabled"); } #endif // Select the best event system available on the OS. In Linux this is // epoll(). On BSD it will be kqueue. if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) { - ret = ERROR_ST_SET_EPOLL; - srs_error("st_set_eventsys use %s failed. ret=%d", st_get_eventsys_name(), ret); - return ret; + return srs_error_new(ERROR_ST_SET_EPOLL, "st enable st failed, current is %s", st_get_eventsys_name()); } - srs_info("st_set_eventsys to %s", st_get_eventsys_name()); - if(st_init() != 0){ - ret = ERROR_ST_INITIALIZE; - srs_error("st_init failed. ret=%d", ret); - return ret; + int r0 = 0; + if((r0 = st_init()) != 0){ + return srs_error_new(ERROR_ST_INITIALIZE, "st initialize failed, r0=%d", r0); } srs_trace("st_init success, use %s", st_get_eventsys_name()); - return ret; + return srs_success; } void srs_close_stfd(srs_netfd_t& stfd) diff --git a/trunk/src/service/srs_service_st.hpp b/trunk/src/service/srs_service_st.hpp index 5d6cc50e9..6df2a4882 100644 --- a/trunk/src/service/srs_service_st.hpp +++ b/trunk/src/service/srs_service_st.hpp @@ -40,7 +40,7 @@ typedef uint64_t srs_utime_t; #define SRS_UTIME_NO_TIMEOUT ((srs_utime_t) -1LL) // initialize st, requires epoll. -extern int srs_st_init(); +extern srs_error_t srs_st_init(); // close the netfd, and close the underlayer fd. // @remark when close, user must ensure io completed.