Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set tcp keepalive for tcp socket #1045

Merged
merged 1 commit into from Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.txt
Expand Up @@ -26,4 +26,5 @@ CONTRIBUTORS ordered by first contribution.
* lovecat<littlefawn@163.com> "Bug fixed"
* panda1986<542638787@qq.com> "Bug fixed"
* YueHonghui<hongf.yue@hotmail.com> "Bug fixed"
* JuntaoLiu<juntliu@gmail.com> "Bug fixed"

10 changes: 10 additions & 0 deletions trunk/src/app/srs_app_listener.cpp
Expand Up @@ -223,6 +223,16 @@ int SrsTcpListener::listen()
}
srs_verbose("setsockopt reuse-addr success. port=%d, fd=%d", port, _fd);

#ifdef SO_KEEPALIVE
Copy link
Member

@winlinvip winlinvip Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use macro definition to enable this setting? Why not always set SO_KEEPALIVE?
This macro should be undefined, so this code will not take effect, right?

TRANS_BY_GPT3

Copy link
Member

@winlinvip winlinvip Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This macro is system-defined and is a reasonable way to write it.

TRANS_BY_GPT3

Copy link
Contributor Author

@juntaoliu juntaoliu Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not set to open using macros, but to enhance the portability of the code. Most Unix-like systems generally define SO_KEEPALIVE in the system, so there is no problem with or without this macro judgment in such systems. However, if the code is ported to other systems, those systems may not support SO_KEEPALIVE.

TRANS_BY_GPT3

int tcp_keepalive = 1;
if (setsockopt(_fd, SOL_SOCKET, SO_KEEPALIVE, &tcp_keepalive, sizeof(int)) == -1) {
ret = ERROR_SOCKET_SETKEEPALIVE;
srs_error("setsockopt SO_KEEPALIVE[%d]error. port=%d, ret=%d", tcp_keepalive, port, ret);
return ret;
}
srs_verbose("setsockopt SO_KEEPALIVE[%d]success. port=%d", tcp_keepalive, port);
#endif

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
Expand Down
1 change: 1 addition & 0 deletions trunk/src/kernel/srs_kernel_error.hpp
Expand Up @@ -98,6 +98,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ERROR_SYSTEM_CREATE_DIR 1057
#define ERROR_SYSTEM_KILL 1058
#define ERROR_SYSTEM_DNS_RESOLVE 1059
#define ERROR_SOCKET_SETKEEPALIVE 1060

///////////////////////////////////////////////////////
// RTMP protocol error.
Expand Down