From 83de3a85e7144b5e222fbd89858a03dba95c9fd4 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Fri, 24 Nov 2017 22:34:29 +0000 Subject: Add TCP_NODELAY support #6795 --- lib/pure/net.nim | 14 +++++++++++++- lib/windows/winlean.nim | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pure/net.nim b/lib/pure/net.nim index b8d05642b..15f2c1228 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -145,7 +145,7 @@ type SOBool* = enum ## Boolean socket options. OptAcceptConn, OptBroadcast, OptDebug, OptDontRoute, OptKeepAlive, - OptOOBInline, OptReuseAddr, OptReusePort + OptOOBInline, OptReuseAddr, OptReusePort, OptNoDelay ReadLineResult* = enum ## result for readLineAsync ReadFullLine, ReadPartialLine, ReadDisconnected, ReadNone @@ -869,6 +869,11 @@ proc close*(socket: Socket) = socket.fd.close() +when defined(posix): + from posix import TCP_NODELAY +else: + from winlean import TCP_NODELAY + proc toCInt*(opt: SOBool): cint = ## Converts a ``SOBool`` into its Socket Option cint representation. case opt @@ -880,6 +885,7 @@ proc toCInt*(opt: SOBool): cint = of OptOOBInline: SO_OOBINLINE of OptReuseAddr: SO_REUSEADDR of OptReusePort: SO_REUSEPORT + of OptNoDelay: TCP_NODELAY proc getSockOpt*(socket: Socket, opt: SOBool, level = SOL_SOCKET): bool {. tags: [ReadIOEffect].} = @@ -902,6 +908,12 @@ proc getPeerAddr*(socket: Socket): (string, Port) = proc setSockOpt*(socket: Socket, opt: SOBool, value: bool, level = SOL_SOCKET) {. tags: [WriteIOEffect].} = ## Sets option ``opt`` to a boolean value specified by ``value``. + ## + ## .. code-block:: Nim + ## var socket = newSocket() + ## socket.setSockOpt(OptReusePort, true) + ## socket.setSockOpt(OptNoDelay, true, level=IPPROTO_TCP.toInt) + ## var valuei = cint(if value: 1 else: 0) setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei) diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim index c3229cc7b..7eb268a9a 100644 --- a/lib/windows/winlean.nim +++ b/lib/windows/winlean.nim @@ -541,6 +541,7 @@ var SO_DONTLINGER* {.importc, header: "winsock2.h".}: cint SO_EXCLUSIVEADDRUSE* {.importc, header: "winsock2.h".}: cint # disallow local address reuse SO_ERROR* {.importc, header: "winsock2.h".}: cint + TCP_NODELAY* {.importc, header: "winsock2.h".}: cint proc `==`*(x, y: SocketHandle): bool {.borrow.} -- cgit 1.4.1-2-gfad0