diff --git a/CUnixTCPSocket.cpp b/CUnixTCPSocket.cpp index 99cd1ecd..ed5d2202 100644 --- a/CUnixTCPSocket.cpp +++ b/CUnixTCPSocket.cpp @@ -31,12 +31,8 @@ CUnixTCPSocket::CUnixTCPSocket() : m_fd(-1), throw XSocketCreate(::strerror(errno)); } - // turn off Nagle algorithm. we send lots of really short messages. - struct protoent* p = getprotobyname("tcp"); - if (p) { - int on = 1; - setsockopt(m_fd, p->p_proto, TCP_NODELAY, &on, sizeof(on)); - } + // always send immediately + setNoDelay(); } CUnixTCPSocket::CUnixTCPSocket(int fd) : m_fd(fd), @@ -44,6 +40,7 @@ CUnixTCPSocket::CUnixTCPSocket(int fd) : m_fd(fd), m_addedJobs(false) { assert(m_fd != -1); + setNoDelay(); } CUnixTCPSocket::~CUnixTCPSocket() @@ -268,3 +265,14 @@ void CUnixTCPSocket::write( numBytes -= n; } } + +void CUnixTCPSocket::setNoDelay() +{ + // turn off Nagle algorithm. we send lots of really short messages + // so we'll accept the (much) larger overhead to reduce latency. + struct protoent* p = getprotobyname("tcp"); + if (p) { + int on = 1; + setsockopt(m_fd, p->p_proto, TCP_NODELAY, &on, sizeof(on)); + } +} diff --git a/CUnixTCPSocket.h b/CUnixTCPSocket.h index ac8d0a34..0a04c5c4 100644 --- a/CUnixTCPSocket.h +++ b/CUnixTCPSocket.h @@ -23,6 +23,9 @@ class CUnixTCPSocket : public CSocket { private: CUnixTCPSocket(int); + // disable Nagle algorithm + void setNoDelay(); + // callbacks for read/write events void readCB(); void writeCB();