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

For #913, use complex error for server handler.

This commit is contained in:
winlin 2017-07-18 20:24:07 +08:00
parent 1d35ae21ec
commit 71dd3f3137
3 changed files with 34 additions and 34 deletions

View file

@ -561,7 +561,7 @@ void SrsServer::dispose()
#endif #endif
} }
srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler) srs_error_t SrsServer::initialize(ISrsServerCycle* ch)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -574,7 +574,7 @@ srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler)
srs_assert(_srs_config); srs_assert(_srs_config);
_srs_config->subscribe(this); _srs_config->subscribe(this);
handler = cycle_handler; handler = ch;
if(handler && (err = handler->initialize()) != srs_success){ if(handler && (err = handler->initialize()) != srs_success){
return srs_error_wrap(err, "handler initialize"); return srs_error_wrap(err, "handler initialize");
} }
@ -1203,8 +1203,14 @@ int SrsServer::accept_client(SrsListenerType type, srs_netfd_t stfd)
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
SrsConnection* conn = fd2conn(type, stfd); SrsConnection* conn = NULL;
if (conn == NULL) {
if ((err = fd2conn(type, stfd, &conn)) != srs_success) {
srs_error("accept client failed, err=%s", srs_error_desc(err).c_str());
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
srs_close_stfd(stfd); srs_close_stfd(stfd);
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@ -1228,9 +1234,9 @@ int SrsServer::accept_client(SrsListenerType type, srs_netfd_t stfd)
return ret; return ret;
} }
SrsConnection* SrsServer::fd2conn(SrsListenerType type, srs_netfd_t stfd) srs_error_t SrsServer::fd2conn(SrsListenerType type, srs_netfd_t stfd, SrsConnection** pconn)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
int fd = srs_netfd_fileno(stfd); int fd = srs_netfd_fileno(stfd);
string ip = srs_get_peer_ip(fd); string ip = srs_get_peer_ip(fd);
@ -1239,19 +1245,18 @@ SrsConnection* SrsServer::fd2conn(SrsListenerType type, srs_netfd_t stfd)
// will send some tcp packet which we cann't got the ip, // will send some tcp packet which we cann't got the ip,
// we just ignore it. // we just ignore it.
if (ip.empty()) { if (ip.empty()) {
srs_info("ignore empty ip client, fd=%d.", fd); return srs_error_new(ERROR_SOCKET_GET_PEER_IP, "ignore empty ip, fd=%d", fd);
return NULL;
} }
// check connection limitation. // check connection limitation.
int max_connections = _srs_config->get_max_connections(); int max_connections = _srs_config->get_max_connections();
if (handler && (ret = handler->on_accept_client(max_connections, (int)conns.size()) != ERROR_SUCCESS)) { if (handler && (err = handler->on_accept_client(max_connections, (int)conns.size())) != srs_success) {
srs_error("handle accept client failed, drop client: clients=%d, max=%d, fd=%d. ret=%d", (int)conns.size(), max_connections, fd, ret); return srs_error_wrap(err, "drop client fd=%d, max=%d, cur=%d for err: %s",
return NULL; fd, max_connections, (int)conns.size(), srs_error_desc(err).c_str());
} }
if ((int)conns.size() >= max_connections) { if ((int)conns.size() >= max_connections) {
srs_error("exceed the max connections, drop client: clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd); return srs_error_new(ERROR_EXCEED_CONNECTIONS, "drop fd=%d, max=%d, cur=%d for exceed connection limits",
return NULL; fd, max_connections, (int)conns.size());
} }
// avoid fd leak when fork. // avoid fd leak when fork.
@ -1259,33 +1264,27 @@ SrsConnection* SrsServer::fd2conn(SrsListenerType type, srs_netfd_t stfd)
if (true) { if (true) {
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, "fnctl F_GETFD error! fd=%d", fd);
srs_error("fnctl F_GETFD error! fd=%d. ret=%#x", fd, ret);
return NULL;
} }
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, "fcntl F_SETFD error! fd=%d", fd);
srs_error("fcntl F_SETFD error! fd=%d ret=%#x", fd, ret);
return NULL;
} }
} }
SrsConnection* conn = NULL;
if (type == SrsListenerRtmpStream) { if (type == SrsListenerRtmpStream) {
conn = new SrsRtmpConn(this, stfd, ip); *pconn = new SrsRtmpConn(this, stfd, ip);
} else if (type == SrsListenerHttpApi) { } else if (type == SrsListenerHttpApi) {
conn = new SrsHttpApi(this, stfd, http_api_mux, ip); *pconn = new SrsHttpApi(this, stfd, http_api_mux, ip);
} else if (type == SrsListenerHttpStream) { } else if (type == SrsListenerHttpStream) {
conn = new SrsResponseOnlyHttpConn(this, stfd, http_server, ip); *pconn = new SrsResponseOnlyHttpConn(this, stfd, http_server, ip);
} else { } else {
srs_warn("close for no service handler. fd=%d, ip=%s", fd, ip.c_str()); srs_warn("close for no service handler. fd=%d, ip=%s", fd, ip.c_str());
srs_close_stfd(stfd); srs_close_stfd(stfd);
return NULL; return err;
} }
return conn; return err;
} }
void SrsServer::remove(ISrsConnection* c) void SrsServer::remove(ISrsConnection* c)

View file

@ -227,7 +227,7 @@ public:
/** /**
* callback the handler when got client. * callback the handler when got client.
*/ */
virtual int on_accept_client(int conf_conns, int curr_conns) = 0; virtual srs_error_t on_accept_client(int max, int cur) = 0;
}; };
/** /**
@ -298,10 +298,10 @@ private:
// server startup workflow, @see run_master() // server startup workflow, @see run_master()
public: public:
/** /**
* initialize server with callback handler. * initialize server with callback handler ch.
* @remark user must free the cycle handler. * @remark user must free the handler.
*/ */
virtual srs_error_t initialize(ISrsServerCycle* cycle_handler); virtual srs_error_t initialize(ISrsServerCycle* ch);
virtual srs_error_t initialize_st(); virtual srs_error_t initialize_st();
virtual srs_error_t initialize_signal(); virtual srs_error_t initialize_signal();
virtual srs_error_t acquire_pid_file(); virtual srs_error_t acquire_pid_file();
@ -360,7 +360,7 @@ public:
*/ */
virtual int accept_client(SrsListenerType type, srs_netfd_t stfd); virtual int accept_client(SrsListenerType type, srs_netfd_t stfd);
private: private:
virtual SrsConnection* fd2conn(SrsListenerType type, srs_netfd_t stfd); virtual srs_error_t fd2conn(SrsListenerType type, srs_netfd_t stfd, SrsConnection** pconn);
// IConnectionManager // IConnectionManager
public: public:
/** /**

View file

@ -110,6 +110,7 @@
#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 #define ERROR_ASPROCESS_PPID 1073
#define ERROR_EXCEED_CONNECTIONS 1074
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
// RTMP protocol error. // RTMP protocol error.