|
|
@ -187,6 +187,43 @@ srs_error_t srs_tcp_connect(string server, int port, srs_utime_t tm, srs_netfd_t
|
|
|
|
return srs_success;
|
|
|
|
return srs_success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
srs_error_t do_srs_tcp_listen(int fd, addrinfo* r, srs_netfd_t* pfd)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Detect alive for TCP connection.
|
|
|
|
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1044
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_reuseport(fd)) != srs_success) {
|
|
|
|
|
|
|
|
return srs_error_wrap(err, "set reuseport");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bind(fd, r->ai_addr, r->ai_addrlen) == -1) {
|
|
|
|
|
|
|
|
return srs_error_new(ERROR_SOCKET_BIND, "bind");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
|
|
|
|
|
|
|
|
return srs_error_new(ERROR_SOCKET_LISTEN, "listen");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((*pfd = srs_netfd_open_socket(fd)) == NULL){
|
|
|
|
|
|
|
|
return srs_error_new(ERROR_ST_OPEN_SOCKET, "st open");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
srs_error_t srs_tcp_listen(std::string ip, int port, srs_netfd_t* pfd)
|
|
|
|
srs_error_t srs_tcp_listen(std::string ip, int port, srs_netfd_t* pfd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
srs_error_t err = srs_success;
|
|
|
@ -213,41 +250,41 @@ srs_error_t srs_tcp_listen(std::string ip, int port, srs_netfd_t* pfd)
|
|
|
|
r->ai_family, r->ai_socktype, r->ai_protocol);
|
|
|
|
r->ai_family, r->ai_socktype, r->ai_protocol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Detect alive for TCP connection.
|
|
|
|
if ((err = do_srs_tcp_listen(fd, r, pfd)) != srs_success) {
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1044
|
|
|
|
|
|
|
|
if ((err = srs_fd_keepalive(fd)) != srs_success) {
|
|
|
|
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_wrap(err, "set keepalive fd=%d", fd);
|
|
|
|
return srs_error_wrap(err, "fd=%d", fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
srs_error_t do_srs_udp_listen(int fd, addrinfo* r, srs_netfd_t* pfd)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_closeexec(fd)) != srs_success) {
|
|
|
|
if ((err = srs_fd_closeexec(fd)) != srs_success) {
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_wrap(err, "set closeexec fd=%d", fd);
|
|
|
|
return srs_error_wrap(err, "set closeexec");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_reuseaddr(fd)) != srs_success) {
|
|
|
|
if ((err = srs_fd_reuseaddr(fd)) != srs_success) {
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_wrap(err, "set reuseaddr fd=%d", fd);
|
|
|
|
return srs_error_wrap(err, "set reuseaddr");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_reuseport(fd)) != srs_success) {
|
|
|
|
if ((err = srs_fd_reuseport(fd)) != srs_success) {
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_wrap(err, "set reuseport fd=%d", fd);
|
|
|
|
return srs_error_wrap(err, "set reuseport");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (bind(fd, r->ai_addr, r->ai_addrlen) == -1) {
|
|
|
|
if (bind(fd, r->ai_addr, r->ai_addrlen) == -1) {
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_new(ERROR_SOCKET_BIND, "bind fd=%d", fd);
|
|
|
|
return srs_error_new(ERROR_SOCKET_BIND, "bind");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
|
|
|
|
|
|
|
|
::close(fd);
|
|
|
|
|
|
|
|
return srs_error_new(ERROR_SOCKET_LISTEN, "listen fd=%d", fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((*pfd = srs_netfd_open_socket(fd)) == NULL){
|
|
|
|
if ((*pfd = srs_netfd_open_socket(fd)) == NULL){
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_new(ERROR_ST_OPEN_SOCKET, "st open fd=%d", fd);
|
|
|
|
return srs_error_new(ERROR_ST_OPEN_SOCKET, "st open");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
return err;
|
|
|
@ -279,29 +316,9 @@ srs_error_t srs_udp_listen(std::string ip, int port, srs_netfd_t* pfd)
|
|
|
|
r->ai_family, r->ai_socktype, r->ai_protocol);
|
|
|
|
r->ai_family, r->ai_socktype, r->ai_protocol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_closeexec(fd)) != srs_success) {
|
|
|
|
if ((err = do_srs_udp_listen(fd, r, pfd)) != srs_success) {
|
|
|
|
::close(fd);
|
|
|
|
|
|
|
|
return srs_error_wrap(err, "set closeexec fd=%d", fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_reuseaddr(fd)) != srs_success) {
|
|
|
|
|
|
|
|
::close(fd);
|
|
|
|
|
|
|
|
return srs_error_wrap(err, "set reuseaddr fd=%d", fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = srs_fd_reuseport(fd)) != srs_success) {
|
|
|
|
|
|
|
|
::close(fd);
|
|
|
|
|
|
|
|
return srs_error_wrap(err, "set reuseport fd=%d", fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bind(fd, r->ai_addr, r->ai_addrlen) == -1) {
|
|
|
|
|
|
|
|
::close(fd);
|
|
|
|
|
|
|
|
return srs_error_new(ERROR_SOCKET_BIND, "bind fd=%d", fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((*pfd = srs_netfd_open_socket(fd)) == NULL){
|
|
|
|
|
|
|
|
::close(fd);
|
|
|
|
::close(fd);
|
|
|
|
return srs_error_new(ERROR_ST_OPEN_SOCKET, "st open fd=%d", fd);
|
|
|
|
return srs_error_wrap(err, "fd=%d", fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
return err;
|
|
|
|