mirror of
https://github.com/ossrs/srs.git
synced 2025-02-14 20:31:56 +00:00
Refine ST service.
This commit is contained in:
parent
a4160308f5
commit
202a584aab
4 changed files with 53 additions and 18 deletions
|
@ -132,8 +132,13 @@ srs_error_t SrsUdpListener::listen()
|
|||
return srs_error_new(ERROR_SOCKET_CREATE, "create socket. ip=%s, port=%d", ip.c_str(), port);
|
||||
}
|
||||
|
||||
srs_fd_close_exec(_fd);
|
||||
srs_socket_reuse_addr(_fd);
|
||||
if ((err = srs_fd_closeexec(_fd)) != srs_success) {
|
||||
return srs_error_wrap(err, "set closeexec");
|
||||
}
|
||||
|
||||
if ((err = srs_fd_reuseaddr(_fd)) != srs_success) {
|
||||
return srs_error_wrap(err, "set reuseaddr");
|
||||
}
|
||||
|
||||
if (bind(_fd, r->ai_addr, r->ai_addrlen) == -1) {
|
||||
return srs_error_new(ERROR_SOCKET_BIND, "bind socket. ep=%s:%d", ip.c_str(), port);;
|
||||
|
@ -230,16 +235,18 @@ srs_error_t SrsTcpListener::listen()
|
|||
|
||||
// Detect alive for TCP connection.
|
||||
// @see https://github.com/ossrs/srs/issues/1044
|
||||
#ifdef SO_KEEPALIVE
|
||||
int tcp_keepalive = 1;
|
||||
if (setsockopt(_fd, SOL_SOCKET, SO_KEEPALIVE, &tcp_keepalive, sizeof(int)) == -1) {
|
||||
return srs_error_new(ERROR_SOCKET_SETKEEPALIVE, "setsockopt SO_KEEPALIVE[%d]error. port=%d", tcp_keepalive, port);
|
||||
if ((err = srs_fd_keepalive(_fd)) != srs_success) {
|
||||
return srs_error_wrap(err, "set keepalive");
|
||||
}
|
||||
|
||||
if ((err = srs_fd_closeexec(_fd)) != srs_success) {
|
||||
return srs_error_wrap(err, "set closeexec");
|
||||
}
|
||||
|
||||
if ((err = srs_fd_reuseaddr(_fd)) != srs_success) {
|
||||
return srs_error_wrap(err, "set reuseaddr");
|
||||
}
|
||||
#endif
|
||||
|
||||
srs_fd_close_exec(_fd);
|
||||
srs_socket_reuse_addr(_fd);
|
||||
|
||||
if (bind(_fd, r->ai_addr, r->ai_addrlen) == -1) {
|
||||
return srs_error_new(ERROR_SOCKET_BIND, "bind socket. ep=%s:%d", ip.c_str(), port);;
|
||||
}
|
||||
|
@ -276,7 +283,10 @@ srs_error_t SrsTcpListener::cycle()
|
|||
}
|
||||
|
||||
int cfd = srs_netfd_fileno(cstfd);
|
||||
srs_fd_close_exec(cfd);
|
||||
|
||||
if ((err = srs_fd_closeexec(cfd)) != srs_success) {
|
||||
return srs_error_wrap(err, "set closeexec");
|
||||
}
|
||||
|
||||
if ((err = handler->on_tcp_client(cstfd)) != srs_success) {
|
||||
return srs_error_wrap(err, "handle fd=%d", cfd);
|
||||
|
|
|
@ -115,6 +115,8 @@
|
|||
#define ERROR_SOCKET_NO_NODELAY 1076
|
||||
#define ERROR_SOCKET_SNDBUF 1077
|
||||
#define ERROR_THREAD_STARTED 1078
|
||||
#define ERROR_SOCKET_SETREUSEADDR 1079
|
||||
#define ERROR_SOCKET_SETCLOSEEXEC 1080
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// RTMP protocol error.
|
||||
|
|
|
@ -86,17 +86,37 @@ void srs_close_stfd(srs_netfd_t& stfd)
|
|||
}
|
||||
}
|
||||
|
||||
void srs_fd_close_exec(int fd)
|
||||
srs_error_t srs_fd_closeexec(int fd)
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFD);
|
||||
flags |= FD_CLOEXEC;
|
||||
fcntl(fd, F_SETFD, flags);
|
||||
if (fcntl(fd, F_SETFD, flags) == -1) {
|
||||
return srs_error_new(ERROR_SOCKET_SETCLOSEEXEC, "FD_CLOEXEC fd=%v", fd);
|
||||
}
|
||||
|
||||
return srs_success;
|
||||
}
|
||||
|
||||
void srs_socket_reuse_addr(int fd)
|
||||
srs_error_t srs_fd_reuseaddr(int fd)
|
||||
{
|
||||
int v = 1;
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(int));
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(int)) == -1) {
|
||||
return srs_error_new(ERROR_SOCKET_SETREUSEADDR, "SO_REUSEADDR fd=%v", fd);
|
||||
}
|
||||
|
||||
return srs_success;
|
||||
}
|
||||
|
||||
srs_error_t srs_fd_keepalive(int fd)
|
||||
{
|
||||
#ifdef SO_KEEPALIVE
|
||||
int v = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &v, sizeof(int)) == -1) {
|
||||
return srs_error_new(ERROR_SOCKET_SETKEEPALIVE, "SO_KEEPALIVE fd=%d", fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
return srs_success;
|
||||
}
|
||||
|
||||
srs_thread_t srs_thread_self()
|
||||
|
|
|
@ -44,10 +44,13 @@ extern srs_error_t srs_st_init();
|
|||
extern void srs_close_stfd(srs_netfd_t& stfd);
|
||||
|
||||
// Set the FD_CLOEXEC of FD.
|
||||
extern void srs_fd_close_exec(int fd);
|
||||
extern srs_error_t srs_fd_closeexec(int fd);
|
||||
|
||||
// Set the SO_REUSEADDR of socket.
|
||||
extern void srs_socket_reuse_addr(int fd);
|
||||
// Set the SO_REUSEADDR of fd.
|
||||
extern srs_error_t srs_fd_reuseaddr(int fd);
|
||||
|
||||
// Set the SO_KEEPALIVE of fd.
|
||||
extern srs_error_t srs_fd_keepalive(int fd);
|
||||
|
||||
// Get current coroutine/thread.
|
||||
extern srs_thread_t srs_thread_self();
|
||||
|
|
Loading…
Reference in a new issue