Skip to content

Display and control your Android device

Enable TCP_NODELAY for the control socket

Romain Vimont8453e3ba7d1b
diff --git a/app/src/server.c b/app/src/server.c
index 2dc00144..e94fcce8 100644
--- a/app/src/server.c
+++ b/app/src/server.c
@@ -659,6 +659,14 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
}
}
+ if (control_socket != SC_SOCKET_NONE) {
+ // Disable Nagle's algorithm for the control socket
+ // (it only impacts the sending side, so it is useless to set it
+ // for the other sockets)
+ bool ok = net_set_tcp_nodelay(control_socket, true);
+ (void) ok; // error already logged
+ }
+
// we don't need the adb tunnel anymore
sc_adb_tunnel_close(tunnel, &server->intr, serial,
server->device_socket_name);
diff --git a/app/src/util/net.c b/app/src/util/net.c
index 67317ead..d43d1c7a 100644
--- a/app/src/util/net.c
+++ b/app/src/util/net.c
@@ -15,6 +15,7 @@
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
+# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <unistd.h>
# include <fcntl.h>
@@ -273,6 +274,22 @@ net_close(sc_socket socket) {
#endif
}
+bool
+net_set_tcp_nodelay(sc_socket socket, bool tcp_nodelay) {
+ sc_raw_socket raw_sock = unwrap(socket);
+
+ int value = tcp_nodelay ? 1 : 0;
+ int ret = setsockopt(raw_sock, IPPROTO_TCP, TCP_NODELAY,
+ (const void *) &value, sizeof(value));
+ if (ret == -1) {
+ net_perror("setsockopt(TCP_NODELAY)");
+ return false;
+ }
+
+ assert(ret == 0);
+ return true;
+}
+
bool
net_parse_ipv4(const char *s, uint32_t *ipv4) {
struct in_addr addr;
diff --git a/app/src/util/net.h b/app/src/util/net.h
index 21396882..ea54b793 100644
--- a/app/src/util/net.h
+++ b/app/src/util/net.h
@@ -67,6 +67,10 @@ net_interrupt(sc_socket socket);
bool
net_close(sc_socket socket);
+// Disable Nagle's algorithm (if tcp_nodelay is true)
+bool
+net_set_tcp_nodelay(sc_socket socket, bool tcp_nodelay);
+
/**
* Parse `ip` "xxx.xxx.xxx.xxx" to an IPv4 host representation
*/