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

For #913, refine server utility

This commit is contained in:
winlin 2017-06-10 15:20:48 +08:00
parent ca9f0bdb1e
commit a20e2c3ef6
15 changed files with 222 additions and 244 deletions

View file

@ -63,8 +63,13 @@ SrsAppCasterFlv::~SrsAppCasterFlv()
int SrsAppCasterFlv::initialize() int SrsAppCasterFlv::initialize()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
if ((err = http_mux->handle("/", this)) != srs_success) {
// TODO: FIXME: Use error.
ret = srs_error_code(err);
srs_freep(err);
if ((ret = http_mux->handle("/", this)) != ERROR_SUCCESS) {
return ret; return ret;
} }

View file

@ -267,12 +267,11 @@ SrsHttpServer::~SrsHttpServer()
srs_error_t SrsHttpServer::initialize() srs_error_t SrsHttpServer::initialize()
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// for SRS go-sharp to detect the status of HTTP server of SRS HTTP FLV Cluster. // 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) { if ((err = http_static->mux.handle("/api/v1/versions", new SrsGoApiVersion())) != srs_success) {
return srs_error_new(ret, "handle versin"); return srs_error_wrap(err, "handle versin");
} }
if ((err = http_stream->initialize()) != srs_success) { if ((err = http_stream->initialize()) != srs_success) {

View file

@ -240,8 +240,8 @@ srs_error_t SrsHttpStaticServer::initialize()
if (!default_root_exists) { if (!default_root_exists) {
// add root // add root
std::string dir = _srs_config->get_http_stream_dir(); std::string dir = _srs_config->get_http_stream_dir();
if ((ret = mux.handle("/", new SrsVodStream(dir))) != ERROR_SUCCESS) { if ((err = mux.handle("/", new SrsVodStream(dir))) != srs_success) {
return srs_error_new(ret, "mount root dir=%s", dir.c_str()); return srs_error_wrap(err, "mount root dir=%s", dir.c_str());
} }
srs_trace("http: root mount to %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 SrsHttpStaticServer::mount_vhost(string vhost, string& pmount)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// when vhost disabled, ignore. // when vhost disabled, ignore.
if (!_srs_config->get_vhost_enabled(vhost)) { if (!_srs_config->get_vhost_enabled(vhost)) {
@ -279,7 +280,11 @@ int SrsHttpStaticServer::mount_vhost(string vhost, string& pmount)
} }
// mount the http of vhost. // 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); srs_error("http: mount dir=%s for vhost=%s failed. ret=%d", dir.c_str(), vhost.c_str(), ret);
return ret; return ret;
} }

View file

@ -721,6 +721,7 @@ srs_error_t SrsHttpStreamServer::initialize()
int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r) int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// the id to identify stream. // the id to identify stream.
std::string sid = r->get_stream_url(); 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, // we must register the handler, then start the thread,
// for the thread will cause thread switch context. // for the thread will cause thread switch context.
// @see https://github.com/ossrs/srs/issues/404 // @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); srs_error("http: mount flv stream for vhost=%s failed. ret=%d", sid.c_str(), ret);
return ret; return ret;
} }

View file

@ -330,24 +330,23 @@ ISrsKafkaCluster::~ISrsKafkaCluster()
// @global kafka event producer, user must use srs_initialize_kafka to initialize it. // @global kafka event producer, user must use srs_initialize_kafka to initialize it.
ISrsKafkaCluster* _srs_kafka = NULL; ISrsKafkaCluster* _srs_kafka = NULL;
int srs_initialize_kafka() srs_error_t srs_initialize_kafka()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
SrsKafkaProducer* kafka = new SrsKafkaProducer(); SrsKafkaProducer* kafka = new SrsKafkaProducer();
_srs_kafka = kafka; _srs_kafka = kafka;
if ((ret = kafka->initialize()) != ERROR_SUCCESS) { if ((err = kafka->initialize()) != srs_success) {
srs_error("initialize the kafka producer failed. ret=%d", ret); return srs_error_wrap(err, "initialize kafka producer");
return ret;
} }
if ((ret = kafka->start()) != ERROR_SUCCESS) { if ((ret = kafka->start()) != ERROR_SUCCESS) {
srs_error("start kafka failed. ret=%d", ret); return srs_error_new(ret, "start kafka producer");
return ret;
} }
return ret; return err;
} }
void srs_dispose_kafka() void srs_dispose_kafka()
@ -390,14 +389,11 @@ SrsKafkaProducer::~SrsKafkaProducer()
srs_cond_destroy(metadata_expired); srs_cond_destroy(metadata_expired);
} }
int SrsKafkaProducer::initialize() srs_error_t SrsKafkaProducer::initialize()
{ {
int ret = ERROR_SUCCESS;
enabled = _srs_config->get_kafka_enabled(); enabled = _srs_config->get_kafka_enabled();
srs_info("initialize kafka ok, enabled=%d.", enabled); srs_info("initialize kafka ok, enabled=%d.", enabled);
return srs_success;
return ret;
} }
int SrsKafkaProducer::start() int SrsKafkaProducer::start()

View file

@ -152,7 +152,7 @@ public:
// @global kafka event producer. // @global kafka event producer.
extern ISrsKafkaCluster* _srs_kafka; extern ISrsKafkaCluster* _srs_kafka;
// kafka initialize and disposer for global object. // kafka initialize and disposer for global object.
extern int srs_initialize_kafka(); extern srs_error_t srs_initialize_kafka();
extern void srs_dispose_kafka(); extern void srs_dispose_kafka();
/** /**
@ -178,7 +178,7 @@ public:
SrsKafkaProducer(); SrsKafkaProducer();
virtual ~SrsKafkaProducer(); virtual ~SrsKafkaProducer();
public: public:
virtual int initialize(); virtual srs_error_t initialize();
virtual int start(); virtual int start();
virtual void stop(); virtual void stop();
// internal: for worker to call task to send object. // internal: for worker to call task to send object.

View file

@ -380,24 +380,18 @@ SrsSignalManager::~SrsSignalManager()
srs_freep(trd); srs_freep(trd);
} }
int SrsSignalManager::initialize() srs_error_t SrsSignalManager::initialize()
{ {
int ret = ERROR_SUCCESS;
/* Create signal pipe */ /* Create signal pipe */
if (pipe(sig_pipe) < 0) { if (pipe(sig_pipe) < 0) {
ret = ERROR_SYSTEM_CREATE_PIPE; return srs_error_new(ERROR_SYSTEM_CREATE_PIPE, "create pipe");
srs_error("create signal manager pipe failed. ret=%d", ret);
return ret;
} }
if ((signal_read_stfd = srs_netfd_open(sig_pipe[0])) == NULL) { if ((signal_read_stfd = srs_netfd_open(sig_pipe[0])) == NULL) {
ret = ERROR_SYSTEM_CREATE_PIPE; return srs_error_new(ERROR_SYSTEM_CREATE_PIPE, "open pipe");
srs_error("create signal manage st pipe failed. ret=%d", ret);
return ret;
} }
return ret; return srs_success;
} }
int SrsSignalManager::start() int SrsSignalManager::start()
@ -588,12 +582,11 @@ srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler)
srs_error_t SrsServer::initialize_st() srs_error_t SrsServer::initialize_st()
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// init st // init st
if ((ret = srs_st_init()) != ERROR_SUCCESS) { if ((err = srs_st_init()) != srs_success) {
return srs_error_new(ret, "initialize st failed"); return srs_error_wrap(err, "initialize st failed");
} }
// @remark, st alloc segment use mmap, which only support 32757 threads, // @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. // initialize the conponents that depends on st.
#ifdef SRS_AUTO_KAFKA #ifdef SRS_AUTO_KAFKA
if ((ret = srs_initialize_kafka()) != ERROR_SUCCESS) { if ((err = srs_initialize_kafka()) != srs_success) {
return srs_error_new(ret, "initialize kafka"); return srs_error_wrap(err, "initialize kafka");
} }
#endif #endif
@ -630,18 +623,16 @@ srs_error_t SrsServer::initialize_st()
return err; return err;
} }
int SrsServer::initialize_signal() srs_error_t SrsServer::initialize_signal()
{ {
return signal_manager->initialize(); 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. // when srs in dolphin mode, no need the pid file.
if (_srs_config->is_dolphin()) { if (_srs_config->is_dolphin()) {
return ret; return srs_success;
} }
std::string pid_file = _srs_config->get_pid_file(); std::string pid_file = _srs_config->get_pid_file();
@ -652,10 +643,8 @@ int SrsServer::acquire_pid_file()
int fd; int fd;
// open pid file // open pid file
if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) < 0) { if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) == -1) {
ret = ERROR_SYSTEM_PID_ACQUIRE; return srs_error_new(ERROR_SYSTEM_PID_ACQUIRE, "open pid file=%s", pid_file.c_str());
srs_error("open pid file %s error, ret=%#x", pid_file.c_str(), ret);
return ret;
} }
// require write lock // 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_whence = SEEK_SET; // SEEK_SET, SEEK_CUR, SEEK_END
lock.l_len = 0; lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) < 0) { if (fcntl(fd, F_SETLK, &lock) == -1) {
if(errno == EACCES || errno == EAGAIN) { if(errno == EACCES || errno == EAGAIN) {
ret = ERROR_SYSTEM_PID_ALREADY_RUNNING; srs_error("srs is already running!");
srs_error("srs is already running! ret=%#x", ret); return srs_error_new(ERROR_SYSTEM_PID_ALREADY_RUNNING, "srs is already running");
return ret;
} }
return srs_error_new(ERROR_SYSTEM_PID_LOCK, "access to pid=%s", pid_file.c_str());
ret = ERROR_SYSTEM_PID_LOCK;
srs_error("require lock for file %s error! ret=%#x", pid_file.c_str(), ret);
return ret;
} }
// truncate file // truncate file
if (ftruncate(fd, 0) < 0) { if (ftruncate(fd, 0) != 0) {
ret = ERROR_SYSTEM_PID_TRUNCATE_FILE; return srs_error_new(ERROR_SYSTEM_PID_TRUNCATE_FILE, "truncate pid file=%s", pid_file.c_str());
srs_error("truncate pid file %s error! ret=%#x", pid_file.c_str(), ret);
return ret;
} }
// write the pid // write the pid
string pid = srs_int2str(getpid()); string pid = srs_int2str(getpid());
if (write(fd, pid.c_str(), pid.length()) != (int)pid.length()) { if (write(fd, pid.c_str(), pid.length()) != (int)pid.length()) {
ret = ERROR_SYSTEM_PID_WRITE_FILE; return srs_error_new(ERROR_SYSTEM_PID_WRITE_FILE, "write pid=%d to file=%s", pid.c_str(), pid_file.c_str());
srs_error("write our pid error! pid=%s file=%s ret=%#x", pid.c_str(), pid_file.c_str(), ret);
return ret;
} }
// auto close when fork child process. // auto close when fork child process.
int val; int val;
if ((val = fcntl(fd, F_GETFD, 0)) < 0) { if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
ret = ERROR_SYSTEM_PID_GET_FILE_INFO; return srs_error_new(ERROR_SYSTEM_PID_GET_FILE_INFO, "fcntl fd=%d", fd);
srs_error("fnctl F_GETFD error! file=%s ret=%#x", pid_file.c_str(), ret);
return ret;
} }
val |= FD_CLOEXEC; val |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, val) < 0) { if (fcntl(fd, F_SETFD, val) < 0) {
ret = ERROR_SYSTEM_PID_SET_FILE_INFO; return srs_error_new(ERROR_SYSTEM_PID_SET_FILE_INFO, "lock file=%s fd=%d", pid_file.c_str(), fd);
srs_error("fcntl F_SETFD error! file=%s ret=%#x", pid_file.c_str(), ret);
return ret;
} }
srs_trace("write pid=%s to %s success!", pid.c_str(), pid_file.c_str()); srs_trace("write pid=%s to %s success!", pid.c_str(), pid_file.c_str());
pid_fd = fd; pid_fd = fd;
return ret; return srs_success;
} }
int SrsServer::listen() srs_error_t SrsServer::listen()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
if ((ret = listen_rtmp()) != ERROR_SUCCESS) { if ((err = listen_rtmp()) != srs_success) {
return ret; return srs_error_wrap(err, "rtmp listen");
} }
if ((ret = listen_http_api()) != ERROR_SUCCESS) { if ((err = listen_http_api()) != srs_success) {
return ret; return srs_error_wrap(err, "http api listen");
} }
if ((ret = listen_http_stream()) != ERROR_SUCCESS) { if ((err = listen_http_stream()) != srs_success) {
return ret; return srs_error_wrap(err, "http stream listen");
} }
if ((ret = listen_stream_caster()) != ERROR_SUCCESS) { if ((err = listen_stream_caster()) != srs_success) {
return ret; return srs_error_wrap(err, "stream caster listen");
} }
if ((ret = conn_manager->start()) != ERROR_SUCCESS) { 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. // 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 ((err = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != srs_success) {
if ((ret = http_api_mux->handle("/", new SrsHttpNotFoundHandler())) != ERROR_SUCCESS) { return srs_error_wrap(err, "handle not found");
return ret;
} }
if ((ret = http_api_mux->handle("/api/", new SrsGoApiApi())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/", new SrsGoApiApi())) != srs_success) {
return ret; return srs_error_wrap(err, "handle api");
} }
if ((ret = http_api_mux->handle("/api/v1/", new SrsGoApiV1())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/", new SrsGoApiV1())) != srs_success) {
return ret; return srs_error_wrap(err, "handle v1");
} }
if ((ret = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != srs_success) {
return ret; return srs_error_wrap(err, "handle versions");
} }
if ((ret = http_api_mux->handle("/api/v1/summaries", new SrsGoApiSummaries())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/summaries", new SrsGoApiSummaries())) != srs_success) {
return ret; return srs_error_wrap(err, "handle summaries");
} }
if ((ret = http_api_mux->handle("/api/v1/rusages", new SrsGoApiRusages())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/rusages", new SrsGoApiRusages())) != srs_success) {
return ret; return srs_error_wrap(err, "handle rusages");
} }
if ((ret = http_api_mux->handle("/api/v1/self_proc_stats", new SrsGoApiSelfProcStats())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/self_proc_stats", new SrsGoApiSelfProcStats())) != srs_success) {
return ret; return srs_error_wrap(err, "handle self proc stats");
} }
if ((ret = http_api_mux->handle("/api/v1/system_proc_stats", new SrsGoApiSystemProcStats())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/system_proc_stats", new SrsGoApiSystemProcStats())) != srs_success) {
return ret; return srs_error_wrap(err, "handle system proc stats");
} }
if ((ret = http_api_mux->handle("/api/v1/meminfos", new SrsGoApiMemInfos())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/meminfos", new SrsGoApiMemInfos())) != srs_success) {
return ret; return srs_error_wrap(err, "handle meminfos");
} }
if ((ret = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != srs_success) {
return ret; return srs_error_wrap(err, "handle authors");
} }
if ((ret = http_api_mux->handle("/api/v1/features", new SrsGoApiFeatures())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/features", new SrsGoApiFeatures())) != srs_success) {
return ret; return srs_error_wrap(err, "handle features");
} }
if ((ret = http_api_mux->handle("/api/v1/vhosts/", new SrsGoApiVhosts())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/vhosts/", new SrsGoApiVhosts())) != srs_success) {
return ret; return srs_error_wrap(err, "handle vhosts");
} }
if ((ret = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/streams/", new SrsGoApiStreams())) != srs_success) {
return ret; return srs_error_wrap(err, "handle streams");
} }
if ((ret = http_api_mux->handle("/api/v1/clients/", new SrsGoApiClients())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/clients/", new SrsGoApiClients())) != srs_success) {
return ret; return srs_error_wrap(err, "handle clients");
} }
if ((ret = http_api_mux->handle("/api/v1/raw", new SrsGoApiRaw(this))) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/raw", new SrsGoApiRaw(this))) != srs_success) {
return ret; return srs_error_wrap(err, "handle raw");
} }
// test the request info. // test the request info.
if ((ret = http_api_mux->handle("/api/v1/tests/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/tests/requests", new SrsGoApiRequests())) != srs_success) {
return ret; return srs_error_wrap(err, "handle tests requests");
} }
// test the error code response. // test the error code response.
if ((ret = http_api_mux->handle("/api/v1/tests/errors", new SrsGoApiError())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/api/v1/tests/errors", new SrsGoApiError())) != srs_success) {
return ret; return srs_error_wrap(err, "handle tests errors");
} }
// test the redirect mechenism. // 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) { if ((err = http_api_mux->handle("/api/v1/tests/redirects", new SrsHttpRedirectHandler("/api/v1/tests/errors", SRS_CONSTS_HTTP_MovedPermanently))) != srs_success) {
return ret; return srs_error_wrap(err, "handle tests redirects");
} }
// test the http vhost. // test the http vhost.
if ((ret = http_api_mux->handle("error.srs.com/api/v1/tests/errors", new SrsGoApiError())) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("error.srs.com/api/v1/tests/errors", new SrsGoApiError())) != srs_success) {
return ret; return srs_error_wrap(err, "handle tests errors for error.srs.com");
} }
// TODO: FIXME: for console. // TODO: FIXME: for console.
// TODO: FIXME: support reload. // TODO: FIXME: support reload.
std::string dir = _srs_config->get_http_stream_dir() + "/console"; std::string dir = _srs_config->get_http_stream_dir() + "/console";
if ((ret = http_api_mux->handle("/console/", new SrsHttpFileServer(dir))) != ERROR_SUCCESS) { if ((err = http_api_mux->handle("/console/", new SrsHttpFileServer(dir))) != srs_success) {
srs_error("http: mount console dir=%s failed. ret=%d", dir.c_str(), ret); return srs_error_wrap(err, "handle console at %s", dir.c_str());
return ret;
} }
srs_trace("http: api mount /console to %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; int ret = ERROR_SUCCESS;
#ifdef SRS_AUTO_INGEST #ifdef SRS_AUTO_INGEST
if ((ret = ingester->start()) != ERROR_SUCCESS) { if ((ret = ingester->start()) != ERROR_SUCCESS) {
srs_error("start ingest streams failed. ret=%d", ret); return srs_error_new(ret, "ingest start");
return ret;
} }
#endif #endif
return ret; return srs_success;
} }
int SrsServer::cycle() srs_error_t SrsServer::cycle()
{ {
int ret = ERROR_SUCCESS; srs_error_t err = do_cycle();
ret = do_cycle();
#ifdef SRS_AUTO_GPERF_MC #ifdef SRS_AUTO_GPERF_MC
destroy(); destroy();
@ -866,7 +843,7 @@ int SrsServer::cycle()
exit(0); exit(0);
#endif #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; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// find the max loop // find the max loop
int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES); int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES);
@ -943,8 +921,7 @@ int SrsServer::do_cycle()
// TODO: FIXME: use SrsHourGlass. // TODO: FIXME: use SrsHourGlass.
while (true) { while (true) {
if (handler && (ret = handler->on_cycle()) != ERROR_SUCCESS) { if (handler && (ret = handler->on_cycle()) != ERROR_SUCCESS) {
srs_error("cycle handle failed. ret=%d", ret); return srs_error_new(ret, "handle callback");
return ret;
} }
// the interval in config. // the interval in config.
@ -958,14 +935,13 @@ int SrsServer::do_cycle()
// asprocess check. // asprocess check.
if (asprocess && ::getppid() != ppid) { if (asprocess && ::getppid() != ppid) {
srs_warn("asprocess ppid changed from %d to %d", ppid, ::getppid()); return srs_error_new(ERROR_ASPROCESS_PPID, "asprocess ppid changed from %d to %d", ppid, ::getppid());
return ret;
} }
// gracefully quit for SIGINT or SIGTERM. // gracefully quit for SIGINT or SIGTERM.
if (signal_gracefully_quit) { if (signal_gracefully_quit) {
srs_trace("cleanup for gracefully terminate."); srs_trace("cleanup for gracefully terminate.");
return ret; return err;
} }
// for gperf heap checker, // for gperf heap checker,
@ -976,7 +952,7 @@ int SrsServer::do_cycle()
#ifdef SRS_AUTO_GPERF_MC #ifdef SRS_AUTO_GPERF_MC
if (signal_gmc_stop) { if (signal_gmc_stop) {
srs_warn("gmc got singal to stop server."); srs_warn("gmc got singal to stop server.");
return ret; return err;
} }
#endif #endif
@ -986,8 +962,7 @@ int SrsServer::do_cycle()
srs_info("get signal to persistence config to file."); srs_info("get signal to persistence config to file.");
if ((ret = _srs_config->persistence()) != ERROR_SUCCESS) { if ((ret = _srs_config->persistence()) != ERROR_SUCCESS) {
srs_error("persistence config to file failed. ret=%d", ret); return srs_error_new(ret, "config persistence to file");
return ret;
} }
srs_trace("persistence config to file success."); srs_trace("persistence config to file success.");
} }
@ -998,15 +973,14 @@ int SrsServer::do_cycle()
srs_info("get signal to reload the config."); srs_info("get signal to reload the config.");
if ((ret = _srs_config->reload()) != ERROR_SUCCESS) { if ((ret = _srs_config->reload()) != ERROR_SUCCESS) {
srs_error("reload config failed. ret=%d", ret); return srs_error_new(ret, "config reload");
return ret;
} }
srs_trace("reload config success."); srs_trace("reload config success.");
} }
// notice the stream sources to cycle. // notice the stream sources to cycle.
if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) { if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) {
return ret; return srs_error_new(ret, "source cycle");
} }
// update the cache time // 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; int ret = ERROR_SUCCESS;
@ -1078,15 +1052,14 @@ int SrsServer::listen_rtmp()
srs_parse_endpoint(ip_ports[i], ip, port); srs_parse_endpoint(ip_ports[i], ip, port);
if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) { if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) {
srs_error("RTMP stream listen at %s:%d failed. ret=%d", ip.c_str(), port, ret); srs_error_new(ret, "rtmp listen %s:%d", ip.c_str(), port);
return ret;
} }
} }
return ret; return srs_success;
} }
int SrsServer::listen_http_api() srs_error_t SrsServer::listen_http_api()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -1102,15 +1075,14 @@ int SrsServer::listen_http_api()
srs_parse_endpoint(ep, ip, port); srs_parse_endpoint(ep, ip, port);
if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) { 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 srs_error_new(ret, "http api listen %s:%d", ip.c_str(), port);
return ret;
} }
} }
return ret; return srs_success;
} }
int SrsServer::listen_http_stream() srs_error_t SrsServer::listen_http_stream()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -1126,15 +1098,14 @@ int SrsServer::listen_http_stream()
srs_parse_endpoint(ep, ip, port); srs_parse_endpoint(ep, ip, port);
if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) { 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 srs_error_new(ret, "http stream listen %s:%d", ip.c_str(), port);
return ret;
} }
} }
return ret; return srs_success;
} }
int SrsServer::listen_stream_caster() srs_error_t SrsServer::listen_stream_caster()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -1160,9 +1131,7 @@ int SrsServer::listen_stream_caster()
} else if (srs_stream_caster_is_flv(caster)) { } else if (srs_stream_caster_is_flv(caster)) {
listener = new SrsHttpFlvListener(this, SrsListenerFlv, stream_caster); listener = new SrsHttpFlvListener(this, SrsListenerFlv, stream_caster);
} else { } else {
ret = ERROR_STREAM_CASTER_ENGINE; return srs_error_new(ERROR_STREAM_CASTER_ENGINE, "invalid caster %s", caster.c_str());
srs_error("unsupported stream caster %s. ret=%d", caster.c_str(), ret);
return ret;
} }
srs_assert(listener != NULL); srs_assert(listener != NULL);
@ -1170,20 +1139,17 @@ int SrsServer::listen_stream_caster()
int port = _srs_config->get_stream_caster_listen(stream_caster); int port = _srs_config->get_stream_caster_listen(stream_caster);
if (port <= 0) { if (port <= 0) {
ret = ERROR_STREAM_CASTER_PORT; return srs_error_new(ERROR_STREAM_CASTER_PORT, "invalid port=%d", port);
srs_error("invalid stream caster port %d. ret=%d", port, ret);
return ret;
} }
// TODO: support listen at <[ip:]port> // TODO: support listen at <[ip:]port>
if ((ret = listener->listen("0.0.0.0", port)) != ERROR_SUCCESS) { if ((ret = listener->listen("0.0.0.0", port)) != ERROR_SUCCESS) {
srs_error("StreamCaster listen at port %d failed. ret=%d", port, ret); return srs_error_new(ret, "listen at %d", port);
return ret;
} }
} }
#endif #endif
return ret; return srs_success;
} }
void SrsServer::close_listeners(SrsListenerType type) void SrsServer::close_listeners(SrsListenerType type)
@ -1334,7 +1300,11 @@ void SrsServer::remove(ISrsConnection* c)
int SrsServer::on_reload_listen() 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() int SrsServer::on_reload_pid()
@ -1344,7 +1314,11 @@ int SrsServer::on_reload_pid()
pid_fd = -1; 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) 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() 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() 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() 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() int SrsServer::on_reload_http_stream_disabled()

View file

@ -194,7 +194,7 @@ public:
SrsSignalManager(SrsServer* s); SrsSignalManager(SrsServer* s);
virtual ~SrsSignalManager(); virtual ~SrsSignalManager();
public: public:
virtual int initialize(); virtual srs_error_t initialize();
virtual int start(); virtual int start();
// interface ISrsEndlessThreadHandler. // interface ISrsEndlessThreadHandler.
public: public:
@ -303,13 +303,13 @@ public:
*/ */
virtual srs_error_t initialize(ISrsServerCycle* cycle_handler); virtual srs_error_t initialize(ISrsServerCycle* cycle_handler);
virtual srs_error_t initialize_st(); virtual srs_error_t initialize_st();
virtual int initialize_signal(); virtual srs_error_t initialize_signal();
virtual int acquire_pid_file(); virtual srs_error_t acquire_pid_file();
virtual int listen(); virtual srs_error_t listen();
virtual int register_signal(); virtual srs_error_t register_signal();
virtual int http_handle(); virtual srs_error_t http_handle();
virtual int ingest(); virtual srs_error_t ingest();
virtual int cycle(); virtual srs_error_t cycle();
// server utilities. // server utilities.
public: public:
/** /**
@ -333,14 +333,14 @@ private:
* update the global static data, for instance, the current time, * update the global static data, for instance, the current time,
* the cpu/mem/network statistic. * the cpu/mem/network statistic.
*/ */
virtual int do_cycle(); virtual srs_error_t do_cycle();
/** /**
* listen at specified protocol. * listen at specified protocol.
*/ */
virtual int listen_rtmp(); virtual srs_error_t listen_rtmp();
virtual int listen_http_api(); virtual srs_error_t listen_http_api();
virtual int listen_http_stream(); virtual srs_error_t listen_http_stream();
virtual int listen_stream_caster(); virtual srs_error_t listen_stream_caster();
/** /**
* close the listeners for specified type, * close the listeners for specified type,
* remove the listen object from manager. * remove the listen object from manager.

View file

@ -109,6 +109,7 @@
#define ERROR_THREAD_INTERRUPED 1070 #define ERROR_THREAD_INTERRUPED 1070
#define ERROR_THREAD_TERMINATED 1071 #define ERROR_THREAD_TERMINATED 1071
#define ERROR_THREAD_DUMMY 1072 #define ERROR_THREAD_DUMMY 1072
#define ERROR_ASPROCESS_PPID 1073
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
// RTMP protocol error. // RTMP protocol error.

View file

@ -48,7 +48,7 @@ using namespace std;
#include <srs_service_utility.hpp> #include <srs_service_utility.hpp>
// pre-declare // 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. // @global log and context.
ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false); 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("input: %s", in_hls_url.c_str());
srs_trace("output: %s", out_rtmp_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 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; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// init st. // init st.
if ((ret = srs_st_init()) != ERROR_SUCCESS) { if ((err = srs_st_init()) != srs_success) {
srs_error("init st failed. ret=%d", ret); return srs_error_wrap(err, "initialize st");
return ret;
} }
SrsHttpUri hls_uri, rtmp_uri; SrsHttpUri hls_uri, rtmp_uri;
if ((ret = hls_uri.initialize(hls)) != ERROR_SUCCESS) { if ((ret = hls_uri.initialize(hls)) != ERROR_SUCCESS) {
srs_error("hls uri invalid. ret=%d", ret); return srs_error_new(ret, "hls parse uri=%s", hls.c_str());
return ret;
} }
if ((ret = rtmp_uri.initialize(rtmp)) != ERROR_SUCCESS) { if ((ret = rtmp_uri.initialize(rtmp)) != ERROR_SUCCESS) {
srs_error("rtmp uri invalid. ret=%d", ret); return srs_error_new(ret, "rtmp parse uri=%s", rtmp.c_str());
return ret;
} }
SrsIngestHlsContext context(&hls_uri, &rtmp_uri); SrsIngestHlsContext context(&hls_uri, &rtmp_uri);
for (;;) { for (;;) {
if ((ret = context.proxy()) != ERROR_SUCCESS) { if ((ret = context.proxy()) != ERROR_SUCCESS) {
srs_error("proxy hls to rtmp failed. ret=%d", ret); return srs_error_new(ret, "proxy hls to rtmp");
return ret;
} }
} }
return ret; return err;
} }

View file

@ -363,7 +363,6 @@ string srs_getenv(const char* name)
srs_error_t run(SrsServer* svr) srs_error_t run(SrsServer* svr)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_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_error_wrap(err, "daemon run master");
} }
return srs_success; return err;
} }
srs_error_t run_master(SrsServer* svr) srs_error_t run_master(SrsServer* svr)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
if ((err = svr->initialize_st()) != srs_success) { if ((err = svr->initialize_st()) != srs_success) {
return srs_error_wrap(err, "initialize st"); return srs_error_wrap(err, "initialize st");
} }
if ((ret = svr->initialize_signal()) != ERROR_SUCCESS) { if ((err = svr->initialize_signal()) != srs_success) {
return srs_error_new(ret, "initialize signal"); return srs_error_wrap(err, "initialize signal");
} }
if ((ret = svr->acquire_pid_file()) != ERROR_SUCCESS) { if ((err = svr->acquire_pid_file()) != srs_success) {
return srs_error_new(ret, "acquire pid file"); return srs_error_wrap(err, "acquire pid file");
} }
if ((ret = svr->listen()) != ERROR_SUCCESS) { if ((err = svr->listen()) != ERROR_SUCCESS) {
return srs_error_new(ret, "listen"); return srs_error_wrap(err, "listen");
} }
if ((ret = svr->register_signal()) != ERROR_SUCCESS) { if ((err = svr->register_signal()) != srs_success) {
return srs_error_new(ret, "register signal"); return srs_error_wrap(err, "register signal");
} }
if ((ret = svr->http_handle()) != ERROR_SUCCESS) { if ((err = svr->http_handle()) != srs_success) {
return srs_error_new(ret, "http handle"); return srs_error_wrap(err, "http handle");
} }
if ((ret = svr->ingest()) != ERROR_SUCCESS) { if ((err = svr->ingest()) != srs_success) {
return srs_error_new(ret, "ingest"); return srs_error_wrap(err, "ingest");
} }
if ((ret = svr->cycle()) != ERROR_SUCCESS) { if ((err = svr->cycle()) != srs_success) {
return srs_error_new(ret, "main cycle"); return srs_error_wrap(err, "main cycle");
} }
return err; return err;

View file

@ -560,24 +560,18 @@ void SrsHttpServeMux::unhijack(ISrsHttpMatchHijacker* h)
hijackers.erase(it); 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); srs_assert(handler);
if (pattern.empty()) { if (pattern.empty()) {
ret = ERROR_HTTP_PATTERN_EMPTY; return srs_error_new(ERROR_HTTP_PATTERN_EMPTY, "empty pattern");
srs_error("http: empty pattern. ret=%d", ret);
return ret;
} }
if (entries.find(pattern) != entries.end()) { if (entries.find(pattern) != entries.end()) {
SrsHttpMuxEntry* exists = entries[pattern]; SrsHttpMuxEntry* exists = entries[pattern];
if (exists->explicit_match) { if (exists->explicit_match) {
ret = ERROR_HTTP_PATTERN_DUPLICATED; return srs_error_new(ERROR_HTTP_PATTERN_DUPLICATED, "pattern=%s exists", pattern.c_str());
srs_error("http: multiple registrations for %s. ret=%d", pattern.c_str(), ret);
return ret;
} }
} }
@ -632,7 +626,7 @@ int SrsHttpServeMux::handle(std::string pattern, ISrsHttpHandler* handler)
} }
} }
return ret; return srs_success;
} }
bool SrsHttpServeMux::can_serve(ISrsHttpMessage* r) bool SrsHttpServeMux::can_serve(ISrsHttpMessage* r)

View file

@ -426,7 +426,7 @@ public:
public: public:
// Handle registers the handler for the given pattern. // Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics. // 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, // whether the http muxer can serve the specified message,
// if not, user can try next muxer. // if not, user can try next muxer.
virtual bool can_serve(ISrsHttpMessage* r); virtual bool can_serve(ISrsHttpMessage* r);

View file

@ -49,37 +49,29 @@ bool srs_st_epoll_is_supported(void)
} }
#endif #endif
int srs_st_init() srs_error_t srs_st_init()
{ {
int ret = ERROR_SUCCESS;
#ifdef __linux__ #ifdef __linux__
// check epoll, some old linux donot support epoll. // check epoll, some old linux donot support epoll.
// @see https://github.com/ossrs/srs/issues/162 // @see https://github.com/ossrs/srs/issues/162
if (!srs_st_epoll_is_supported()) { if (!srs_st_epoll_is_supported()) {
ret = ERROR_ST_SET_EPOLL; return srs_error_new(ERROR_ST_SET_EPOLL, "linux epoll disabled");
srs_error("epoll required on Linux. ret=%d", ret);
return ret;
} }
#endif #endif
// Select the best event system available on the OS. In Linux this is // Select the best event system available on the OS. In Linux this is
// epoll(). On BSD it will be kqueue. // epoll(). On BSD it will be kqueue.
if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) { if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
ret = ERROR_ST_SET_EPOLL; return srs_error_new(ERROR_ST_SET_EPOLL, "st enable st failed, current is %s", st_get_eventsys_name());
srs_error("st_set_eventsys use %s failed. ret=%d", st_get_eventsys_name(), ret);
return ret;
} }
srs_info("st_set_eventsys to %s", st_get_eventsys_name());
if(st_init() != 0){ int r0 = 0;
ret = ERROR_ST_INITIALIZE; if((r0 = st_init()) != 0){
srs_error("st_init failed. ret=%d", ret); return srs_error_new(ERROR_ST_INITIALIZE, "st initialize failed, r0=%d", r0);
return ret;
} }
srs_trace("st_init success, use %s", st_get_eventsys_name()); srs_trace("st_init success, use %s", st_get_eventsys_name());
return ret; return srs_success;
} }
void srs_close_stfd(srs_netfd_t& stfd) void srs_close_stfd(srs_netfd_t& stfd)

View file

@ -40,7 +40,7 @@ typedef uint64_t srs_utime_t;
#define SRS_UTIME_NO_TIMEOUT ((srs_utime_t) -1LL) #define SRS_UTIME_NO_TIMEOUT ((srs_utime_t) -1LL)
// initialize st, requires epoll. // initialize st, requires epoll.
extern int srs_st_init(); extern srs_error_t srs_st_init();
// close the netfd, and close the underlayer fd. // close the netfd, and close the underlayer fd.
// @remark when close, user must ensure io completed. // @remark when close, user must ensure io completed.