For #913, use complex error for server handler.

pull/978/head
winlin 8 years ago
parent 1d35ae21ec
commit 71dd3f3137

@ -561,7 +561,7 @@ void SrsServer::dispose()
#endif
}
srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler)
srs_error_t SrsServer::initialize(ISrsServerCycle* ch)
{
srs_error_t err = srs_success;
@ -574,7 +574,7 @@ srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler)
srs_assert(_srs_config);
_srs_config->subscribe(this);
handler = cycle_handler;
handler = ch;
if(handler && (err = handler->initialize()) != srs_success){
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;
srs_error_t err = srs_success;
SrsConnection* conn = fd2conn(type, stfd);
if (conn == NULL) {
SrsConnection* 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);
return ERROR_SUCCESS;
}
@ -1228,9 +1234,9 @@ int SrsServer::accept_client(SrsListenerType type, srs_netfd_t stfd)
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);
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,
// we just ignore it.
if (ip.empty()) {
srs_info("ignore empty ip client, fd=%d.", fd);
return NULL;
return srs_error_new(ERROR_SOCKET_GET_PEER_IP, "ignore empty ip, fd=%d", fd);
}
// check connection limitation.
int max_connections = _srs_config->get_max_connections();
if (handler && (ret = handler->on_accept_client(max_connections, (int)conns.size()) != ERROR_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 NULL;
if (handler && (err = handler->on_accept_client(max_connections, (int)conns.size())) != srs_success) {
return srs_error_wrap(err, "drop client fd=%d, max=%d, cur=%d for err: %s",
fd, max_connections, (int)conns.size(), srs_error_desc(err).c_str());
}
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 NULL;
return srs_error_new(ERROR_EXCEED_CONNECTIONS, "drop fd=%d, max=%d, cur=%d for exceed connection limits",
fd, max_connections, (int)conns.size());
}
// avoid fd leak when fork.
@ -1259,33 +1264,27 @@ SrsConnection* SrsServer::fd2conn(SrsListenerType type, srs_netfd_t stfd)
if (true) {
int val;
if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
ret = ERROR_SYSTEM_PID_GET_FILE_INFO;
srs_error("fnctl F_GETFD error! fd=%d. ret=%#x", fd, ret);
return NULL;
return srs_error_new(ERROR_SYSTEM_PID_GET_FILE_INFO, "fnctl F_GETFD error! 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! fd=%d ret=%#x", fd, ret);
return NULL;
return srs_error_new(ERROR_SYSTEM_PID_SET_FILE_INFO, "fcntl F_SETFD error! fd=%d", fd);
}
}
SrsConnection* conn = NULL;
if (type == SrsListenerRtmpStream) {
conn = new SrsRtmpConn(this, stfd, ip);
*pconn = new SrsRtmpConn(this, stfd, ip);
} 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) {
conn = new SrsResponseOnlyHttpConn(this, stfd, http_server, ip);
*pconn = new SrsResponseOnlyHttpConn(this, stfd, http_server, ip);
} else {
srs_warn("close for no service handler. fd=%d, ip=%s", fd, ip.c_str());
srs_close_stfd(stfd);
return NULL;
return err;
}
return conn;
return err;
}
void SrsServer::remove(ISrsConnection* c)

@ -106,7 +106,7 @@ public:
virtual ~SrsBufferListener();
public:
virtual int listen(std::string ip, int port);
// ISrsTcpHandler
// ISrsTcpHandler
public:
virtual int on_tcp_client(srs_netfd_t stfd);
};
@ -125,7 +125,7 @@ public:
virtual ~SrsRtspListener();
public:
virtual int listen(std::string i, int p);
// ISrsTcpHandler
// ISrsTcpHandler
public:
virtual int on_tcp_client(srs_netfd_t stfd);
};
@ -143,7 +143,7 @@ public:
virtual ~SrsHttpFlvListener();
public:
virtual int listen(std::string i, int p);
// ISrsTcpHandler
// ISrsTcpHandler
public:
virtual int on_tcp_client(srs_netfd_t stfd);
};
@ -227,7 +227,7 @@ public:
/**
* 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()
public:
/**
* initialize server with callback handler.
* @remark user must free the cycle handler.
* initialize server with callback handler ch.
* @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_signal();
virtual srs_error_t acquire_pid_file();
@ -360,7 +360,7 @@ public:
*/
virtual int accept_client(SrsListenerType type, srs_netfd_t stfd);
private:
virtual SrsConnection* fd2conn(SrsListenerType type, srs_netfd_t stfd);
virtual srs_error_t fd2conn(SrsListenerType type, srs_netfd_t stfd, SrsConnection** pconn);
// IConnectionManager
public:
/**

@ -110,6 +110,7 @@
#define ERROR_THREAD_TERMINATED 1071
#define ERROR_THREAD_DUMMY 1072
#define ERROR_ASPROCESS_PPID 1073
#define ERROR_EXCEED_CONNECTIONS 1074
///////////////////////////////////////////////////////
// RTMP protocol error.

Loading…
Cancel
Save