From fe79ac593cfc7b6cbda38db74a8e5f5be3d6cbd7 Mon Sep 17 00:00:00 2001 From: crs Date: Sun, 13 May 2001 12:43:16 +0000 Subject: [PATCH] more fixes to reduce latency. nagle agorithm doesn't seem to stay off on a socket on linux because a connection clearly doesn't send data as often as possible. will have to implement a UDP socket to reduce overhead and avoid these delays. wanted to do that anyway. --- CUnixTCPSocket.cpp | 20 ++++++++++++++------ CUnixTCPSocket.h | 3 +++ 2 files changed, 17 insertions(+), 6 deletions(-) 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();