mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 09:24:36 +00:00
Merge pull request #6802 from FedericoCeratto/nodelay
Add TCP_NODELAY support #6795
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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.}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user