Skip to content

Commit

Permalink
For #913, use complex error for server handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 18, 2017
1 parent 1d35ae2 commit 71dd3f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
51 changes: 25 additions & 26 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -1239,53 +1245,46 @@ 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.
// @see https://github.com/ossrs/srs/issues/518
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)
Expand Down
16 changes: 8 additions & 8 deletions trunk/src/app/srs_app_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SrsBufferListener : virtual public SrsListener, virtual public ISrsTcpHand
virtual ~SrsBufferListener();
public:
virtual int listen(std::string ip, int port);
// ISrsTcpHandler
// ISrsTcpHandler
public:
virtual int on_tcp_client(srs_netfd_t stfd);
};
Expand All @@ -125,7 +125,7 @@ class SrsRtspListener : virtual public SrsListener, virtual public ISrsTcpHandle
virtual ~SrsRtspListener();
public:
virtual int listen(std::string i, int p);
// ISrsTcpHandler
// ISrsTcpHandler
public:
virtual int on_tcp_client(srs_netfd_t stfd);
};
Expand All @@ -143,7 +143,7 @@ class SrsHttpFlvListener : virtual public SrsListener, virtual public ISrsTcpHan
virtual ~SrsHttpFlvListener();
public:
virtual int listen(std::string i, int p);
// ISrsTcpHandler
// ISrsTcpHandler
public:
virtual int on_tcp_client(srs_netfd_t stfd);
};
Expand Down Expand Up @@ -227,7 +227,7 @@ class ISrsServerCycle
/**
* 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;
};

/**
Expand Down Expand Up @@ -298,10 +298,10 @@ class SrsServer : virtual public ISrsReloadHandler
// 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();
Expand Down Expand Up @@ -360,7 +360,7 @@ class SrsServer : virtual public ISrsReloadHandler
*/
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:
/**
Expand Down
1 change: 1 addition & 0 deletions trunk/src/kernel/srs_kernel_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 71dd3f3

Please sign in to comment.