Move checkCloseError to nativesockets
This commit is contained in:
parent
fb8168d338
commit
7a839d7b02
3 changed files with 32 additions and 25 deletions
|
|
@ -620,3 +620,28 @@ proc selectWrite*(writefds: var seq[SocketHandle],
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
var wsa: WSAData
|
var wsa: WSAData
|
||||||
if wsaStartup(0x0101'i16, addr wsa) != 0: raiseOSError(osLastError())
|
if wsaStartup(0x0101'i16, addr wsa) != 0: raiseOSError(osLastError())
|
||||||
|
|
||||||
|
proc checkCloseError*(ret: cint) =
|
||||||
|
## Asserts that the return value of close() or closeSocket() syscall
|
||||||
|
## does not indicate a programming error (such as invalid descriptor).
|
||||||
|
## This must only be used when an error has already occurred and
|
||||||
|
## you are performing a cleanup.
|
||||||
|
## Otherwise, error handling must be performed as usual.
|
||||||
|
##
|
||||||
|
## This procedure must be called right after performing the syscall. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: nim
|
||||||
|
##
|
||||||
|
## let ret = someSysCall()
|
||||||
|
## if ret != 0:
|
||||||
|
## let errcode = osLastError()
|
||||||
|
## checkCloseError sock.closeSocket()
|
||||||
|
## raise newException(OSError, osErrorMsg(errcode))
|
||||||
|
|
||||||
|
if ret != 0:
|
||||||
|
let errcode = osLastError()
|
||||||
|
when useWinVersion:
|
||||||
|
doAssert(errcode.int32 notin {WSANOTINITIALISED, WSAENOTSOCK,
|
||||||
|
WSAEINPROGRESS, WSAEINTR, WSAEWOULDBLOCK})
|
||||||
|
else:
|
||||||
|
doAssert(errcode.int32 notin {EBADF})
|
||||||
|
|
|
||||||
|
|
@ -191,31 +191,6 @@ proc isDisconnectionError*(flags: set[SocketFlag],
|
||||||
SocketFlag.SafeDisconn in flags and
|
SocketFlag.SafeDisconn in flags and
|
||||||
lastError.int32 in {ECONNRESET, EPIPE, ENETRESET}
|
lastError.int32 in {ECONNRESET, EPIPE, ENETRESET}
|
||||||
|
|
||||||
proc checkCloseError*(ret: cint) =
|
|
||||||
## Asserts that the return value of close() or closeSocket() syscall
|
|
||||||
## does not indicate a programming error (such as invalid descriptor).
|
|
||||||
## This must only be used when an error has already occurred and
|
|
||||||
## you are performing a cleanup.
|
|
||||||
## Otherwise, error handling must be performed as usual.
|
|
||||||
##
|
|
||||||
## This procedure must be called right after perfoming the syscall. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: nim
|
|
||||||
##
|
|
||||||
## let ret = someSysCall()
|
|
||||||
## if ret != 0:
|
|
||||||
## let errcode = osLastError()
|
|
||||||
## checkCloseError sock.closeSocket()
|
|
||||||
## raise newException(OSError, osErrorMsg(errcode))
|
|
||||||
|
|
||||||
if ret != 0:
|
|
||||||
let errcode = osLastError()
|
|
||||||
when useWinVersion:
|
|
||||||
doAssert(errcode.int32 notin {WSANOTINITIALISED, WSAENOTSOCK,
|
|
||||||
WSAEINPROGRESS, WSAEINTR, WSAEWOULDBLOCK})
|
|
||||||
else:
|
|
||||||
doAssert(errcode.int32 notin {EBADF})
|
|
||||||
|
|
||||||
proc toOSFlags*(socketFlags: set[SocketFlag]): cint =
|
proc toOSFlags*(socketFlags: set[SocketFlag]): cint =
|
||||||
## Converts the flags into the underlying OS representation.
|
## Converts the flags into the underlying OS representation.
|
||||||
for f in socketFlags:
|
for f in socketFlags:
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,16 @@
|
||||||
|
discard """
|
||||||
|
exitcode: 0
|
||||||
|
output: ""
|
||||||
|
"""
|
||||||
|
|
||||||
import asyncdispatch, net, os, nativesockets
|
import asyncdispatch, net, os, nativesockets
|
||||||
|
|
||||||
# bug: https://github.com/nim-lang/Nim/issues/5279
|
# bug: https://github.com/nim-lang/Nim/issues/5279
|
||||||
|
|
||||||
proc setupServerSocket(hostname: string, port: Port): AsyncFD =
|
proc setupServerSocket(hostname: string, port: Port): AsyncFD =
|
||||||
let fd = newNativeSocket()
|
let fd = newNativeSocket()
|
||||||
|
if fd == osInvalidSocket:
|
||||||
|
raiseOSError(osLastError())
|
||||||
setSockOptInt(fd, SOL_SOCKET, SO_REUSEADDR, 1)
|
setSockOptInt(fd, SOL_SOCKET, SO_REUSEADDR, 1)
|
||||||
var aiList = getAddrInfo(hostname, port)
|
var aiList = getAddrInfo(hostname, port)
|
||||||
if bindAddr(fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32:
|
if bindAddr(fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue