Skip to content

Commit

Permalink
socket: Set socket options for usbmux connection to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed May 15, 2018
1 parent c724e70 commit e71556d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion common/socket.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* socket.c
*
* Copyright (C) 2012 Martin Szulecki <[email protected]>
Expand Down Expand Up @@ -35,6 +35,7 @@ static int wsa_init = 0;
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
Expand Down Expand Up @@ -116,6 +117,7 @@ int socket_connect_unix(const char *filename)
#ifdef SO_NOSIGPIPE
int yes = 1;
#endif
int bufsize = 0x20000;

// check if socket file exists...
if (stat(filename, &fst) != 0) {
Expand All @@ -138,6 +140,14 @@ int socket_connect_unix(const char *filename)
return -1;
}

if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(int)) == -1) {
perror("Could not set send buffer for socket");
}

if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(int)) == -1) {
perror("Could not set receive buffer for socket");
}

#ifdef SO_NOSIGPIPE
if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&yes, sizeof(int)) == -1) {
perror("setsockopt()");
Expand Down Expand Up @@ -225,6 +235,7 @@ int socket_connect(const char *addr, uint16_t port)
{
int sfd = -1;
int yes = 1;
int bufsize = 0x20000;
struct hostent *hp;
struct sockaddr_in saddr;
#ifdef WIN32
Expand Down Expand Up @@ -275,6 +286,18 @@ int socket_connect(const char *addr, uint16_t port)
}
#endif

if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(int)) == -1) {
perror("Could not set TCP_NODELAY on socket");
}

if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(int)) == -1) {
perror("Could not set send buffer for socket");
}

if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(int)) == -1) {
perror("Could not set receive buffer for socket");
}

memset((void *) &saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = *(uint32_t *) hp->h_addr;
Expand Down

0 comments on commit e71556d

Please sign in to comment.