Set TCP_NODELAY socket option before calling connect.

Setting the option after calling connect but before the socket is
connected fails in some circumstances on Windows, while setting it
before connecting always succeeds. That's what Chrome is doing;
TCPClientSocket::OpenSocket calls SetDefaultOptionsForClient (which
sets TCP_NODELAY) right after opening the socket.

Also, start logging errors, and storing last error when setsockopt
fails.

Bug: webrtc:12217
Change-Id: I169d52e31b50e54e5bc93ff3590bae656cacb2b2
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/195060
Commit-Queue: Taylor <deadbeef@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32696}
This commit is contained in:
Taylor Brandstetter 2020-11-25 15:24:53 -08:00 committed by Commit Bot
parent b6b599fc58
commit 9d269408a4
3 changed files with 28 additions and 11 deletions

View file

@ -81,16 +81,20 @@ AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
return NULL;
}
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance; this causes
// small media packets to be sent immediately rather than being buffered up,
// reducing latency.
if (socket->SetOption(Socket::OPT_NODELAY, 1) != 0) {
RTC_LOG(LS_ERROR) << "Setting TCP_NODELAY option failed with error "
<< socket->GetError();
}
// If using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS));
socket = new AsyncSSLSocket(socket);
}
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
// See http://go/gtalktcpnodelayexperiment
socket->SetOption(Socket::OPT_NODELAY, 1);
if (opts & PacketSocketFactory::OPT_STUN)
return new cricket::AsyncStunTCPSocket(socket, true);
@ -123,6 +127,16 @@ AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
}
}
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance; this causes
// small media packets to be sent immediately rather than being buffered up,
// reducing latency.
//
// Must be done before calling Connect, otherwise it may fail.
if (socket->SetOption(Socket::OPT_NODELAY, 1) != 0) {
RTC_LOG(LS_ERROR) << "Setting TCP_NODELAY option failed with error "
<< socket->GetError();
}
// If using a proxy, wrap the socket in a proxy socket.
if (proxy_info.type == PROXY_SOCKS5) {
socket = new AsyncSocksProxySocket(
@ -181,10 +195,6 @@ AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
tcp_socket = new AsyncTCPSocket(socket, false);
}
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
// See http://go/gtalktcpnodelayexperiment
tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
return tcp_socket;
}

View file

@ -230,10 +230,10 @@ class NATSocket : public AsyncSocket, public sigslot::has_slots<> {
return connected_ ? CS_CONNECTED : CS_CLOSED;
}
int GetOption(Option opt, int* value) override {
return socket_->GetOption(opt, value);
return socket_ ? socket_->GetOption(opt, value) : -1;
}
int SetOption(Option opt, int value) override {
return socket_->SetOption(opt, value);
return socket_ ? socket_->SetOption(opt, value) : -1;
}
void OnConnectEvent(AsyncSocket* socket) {

View file

@ -335,10 +335,17 @@ int PhysicalSocket::SetOption(Option opt, int value) {
#if defined(WEBRTC_POSIX)
if (sopt == IPV6_TCLASS) {
// Set the IPv4 option in all cases to support dual-stack sockets.
// Don't bother checking the return code, as this is expected to fail if
// it's not actually dual-stack.
::setsockopt(s_, IPPROTO_IP, IP_TOS, (SockOptArg)&value, sizeof(value));
}
#endif
return ::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value));
int result =
::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value));
if (result != 0) {
UpdateLastError();
}
return result;
}
int PhysicalSocket::Send(const void* pv, size_t cb) {