This commit is contained in:
Dominik Picheta 2018-02-13 10:08:37 +00:00 • committed by Andreas Rumpf
commit 1a2351f95f
7 changed files with 76 additions and 19 deletions

View file

@ -235,6 +235,10 @@ styledEcho "Red on Green.", resetStyle
- ``\n`` is now only the single line feed character like in most - ``\n`` is now only the single line feed character like in most
other programming languages. The new platform specific newline escape sequence is other programming languages. The new platform specific newline escape sequence is
written as ``\p``. This change only affects the Windows platform. written as ``\p``. This change only affects the Windows platform.
- ``newAsyncNativeSocket`` is now named ``createAsyncNativeSocket``
and it no longer raises an OS error but returns an ``osInvalidSocket`` when
creation fails.
- ``newNativeSocket`` is now named ``createNativeSocket``.
- Type inference for generic type parameters involving numeric types is now symetric. See - Type inference for generic type parameters involving numeric types is now symetric. See
[Generic type inference for numeric types](https://nim-lang.org/docs/manual.html#generics-generic-type-inference-fornumeric-types) [Generic type inference for numeric types](https://nim-lang.org/docs/manual.html#generics-generic-type-inference-fornumeric-types)
for more information. for more information.

View file

@ -1648,4 +1648,8 @@ proc waitFor*[T](fut: Future[T]): T =
fut.read fut.read
{.deprecated: [setEvent: trigger].} proc setEvent*(ev: AsyncEvent) {.deprecated.} =
## Set event ``ev`` to signaled state.
##
## **Deprecated since v0.18.0:** Use ``trigger`` instead.
ev.trigger()

View file

@ -163,8 +163,10 @@ proc newAsyncSocket*(domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM,
## ##
## This procedure will also create a brand new file descriptor for ## This procedure will also create a brand new file descriptor for
## this socket. ## this socket.
result = newAsyncSocket(newAsyncNativeSocket(domain, sockType, protocol), let fd = createAsyncNativeSocket(domain, sockType, protocol)
domain, sockType, protocol, buffered) if fd.SocketHandle == osInvalidSocket:
raiseOSError(osLastError())
result = newAsyncSocket(fd, domain, sockType, protocol, buffered)
proc newAsyncSocket*(domain, sockType, protocol: cint, proc newAsyncSocket*(domain, sockType, protocol: cint,
buffered = true): AsyncSocket = buffered = true): AsyncSocket =
@ -172,8 +174,10 @@ proc newAsyncSocket*(domain, sockType, protocol: cint,
## ##
## This procedure will also create a brand new file descriptor for ## This procedure will also create a brand new file descriptor for
## this socket. ## this socket.
result = newAsyncSocket(newAsyncNativeSocket(domain, sockType, protocol), let fd = createAsyncNativeSocket(domain, sockType, protocol)
Domain(domain), SockType(sockType), if fd.SocketHandle == osInvalidSocket:
raiseOSError(osLastError())
result = newAsyncSocket(fd, Domain(domain), SockType(sockType),
Protocol(protocol), buffered) Protocol(protocol), buffered)
when defineSsl: when defineSsl:

View file

@ -1,21 +1,31 @@
template newAsyncNativeSocketImpl(domain, sockType, protocol) = template createAsyncNativeSocketImpl(domain, sockType, protocol) =
let handle = newNativeSocket(domain, sockType, protocol) let handle = newNativeSocket(domain, sockType, protocol)
if handle == osInvalidSocket: if handle == osInvalidSocket:
raiseOSError(osLastError()) return osInvalidSocket.AsyncFD
handle.setBlocking(false) handle.setBlocking(false)
when defined(macosx) and not defined(nimdoc): when defined(macosx) and not defined(nimdoc):
handle.setSockOptInt(SOL_SOCKET, SO_NOSIGPIPE, 1) handle.setSockOptInt(SOL_SOCKET, SO_NOSIGPIPE, 1)
result = handle.AsyncFD result = handle.AsyncFD
register(result) register(result)
proc newAsyncNativeSocket*(domain: cint, sockType: cint, proc createAsyncNativeSocket*(domain: cint, sockType: cint,
protocol: cint): AsyncFD = protocol: cint): AsyncFD =
newAsyncNativeSocketImpl(domain, sockType, protocol) createAsyncNativeSocketImpl(domain, sockType, protocol)
proc createAsyncNativeSocket*(domain: Domain = Domain.AF_INET,
sockType: SockType = SOCK_STREAM,
protocol: Protocol = IPPROTO_TCP): AsyncFD =
createAsyncNativeSocketImpl(domain, sockType, protocol)
proc newAsyncNativeSocket*(domain: cint, sockType: cint,
protocol: cint): AsyncFD {.deprecated.} =
createAsyncNativeSocketImpl(domain, sockType, protocol)
proc newAsyncNativeSocket*(domain: Domain = Domain.AF_INET, proc newAsyncNativeSocket*(domain: Domain = Domain.AF_INET,
sockType: SockType = SOCK_STREAM, sockType: SockType = SOCK_STREAM,
protocol: Protocol = IPPROTO_TCP): AsyncFD = protocol: Protocol = IPPROTO_TCP): AsyncFD
newAsyncNativeSocketImpl(domain, sockType, protocol) {.deprecated.} =
createAsyncNativeSocketImpl(domain, sockType, protocol)
when defined(windows) or defined(nimdoc): when defined(windows) or defined(nimdoc):
proc bindToDomain(handle: SocketHandle, domain: Domain) = proc bindToDomain(handle: SocketHandle, domain: Domain) =

View file

@ -184,13 +184,13 @@ proc toSockType*(protocol: Protocol): SockType =
of IPPROTO_IP, IPPROTO_IPV6, IPPROTO_RAW, IPPROTO_ICMP: of IPPROTO_IP, IPPROTO_IPV6, IPPROTO_RAW, IPPROTO_ICMP:
SOCK_RAW SOCK_RAW
proc newNativeSocket*(domain: Domain = AF_INET, proc createNativeSocket*(domain: Domain = AF_INET,
sockType: SockType = SOCK_STREAM, sockType: SockType = SOCK_STREAM,
protocol: Protocol = IPPROTO_TCP): SocketHandle = protocol: Protocol = IPPROTO_TCP): SocketHandle =
## Creates a new socket; returns `osInvalidSocket` if an error occurs. ## Creates a new socket; returns `osInvalidSocket` if an error occurs.
socket(toInt(domain), toInt(sockType), toInt(protocol)) socket(toInt(domain), toInt(sockType), toInt(protocol))
proc newNativeSocket*(domain: cint, sockType: cint, proc createNativeSocket*(domain: cint, sockType: cint,
protocol: cint): SocketHandle = protocol: cint): SocketHandle =
## Creates a new socket; returns `osInvalidSocket` if an error occurs. ## Creates a new socket; returns `osInvalidSocket` if an error occurs.
## ##
@ -198,6 +198,26 @@ proc newNativeSocket*(domain: cint, sockType: cint,
## not contain what you need. ## not contain what you need.
socket(domain, sockType, protocol) socket(domain, sockType, protocol)
proc newNativeSocket*(domain: Domain = AF_INET,
sockType: SockType = SOCK_STREAM,
protocol: Protocol = IPPROTO_TCP): SocketHandle
{.deprecated.} =
## Creates a new socket; returns `osInvalidSocket` if an error occurs.
##
## **Deprecated since v0.18.0:** Use ``createNativeSocket`` instead.
createNativeSocket(domain, sockType, protocol)
proc newNativeSocket*(domain: cint, sockType: cint,
protocol: cint): SocketHandle
{.deprecated.} =
## Creates a new socket; returns `osInvalidSocket` if an error occurs.
##
## Use this overload if one of the enums specified above does
## not contain what you need.
##
## **Deprecated since v0.18.0:** Use ``createNativeSocket`` instead.
createNativeSocket(domain, sockType, protocol)
proc close*(socket: SocketHandle) = proc close*(socket: SocketHandle) =
## closes a socket. ## closes a socket.
when useWinVersion: when useWinVersion:

View file

@ -221,7 +221,7 @@ proc newSocket*(domain, sockType, protocol: cint, buffered = true): Socket =
## Creates a new socket. ## Creates a new socket.
## ##
## If an error occurs EOS will be raised. ## If an error occurs EOS will be raised.
let fd = newNativeSocket(domain, sockType, protocol) let fd = createNativeSocket(domain, sockType, protocol)
if fd == osInvalidSocket: if fd == osInvalidSocket:
raiseOSError(osLastError()) raiseOSError(osLastError())
result = newSocket(fd, domain.Domain, sockType.SockType, protocol.Protocol, result = newSocket(fd, domain.Domain, sockType.SockType, protocol.Protocol,
@ -232,7 +232,7 @@ proc newSocket*(domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM,
## Creates a new socket. ## Creates a new socket.
## ##
## If an error occurs EOS will be raised. ## If an error occurs EOS will be raised.
let fd = newNativeSocket(domain, sockType, protocol) let fd = createNativeSocket(domain, sockType, protocol)
if fd == osInvalidSocket: if fd == osInvalidSocket:
raiseOSError(osLastError()) raiseOSError(osLastError())
result = newSocket(fd, domain, sockType, protocol, buffered) result = newSocket(fd, domain, sockType, protocol, buffered)
@ -1544,7 +1544,7 @@ proc dial*(address: string, port: Port,
domain = domainOpt.unsafeGet() domain = domainOpt.unsafeGet()
lastFd = fdPerDomain[ord(domain)] lastFd = fdPerDomain[ord(domain)]
if lastFd == osInvalidSocket: if lastFd == osInvalidSocket:
lastFd = newNativeSocket(domain, sockType, protocol) lastFd = createNativeSocket(domain, sockType, protocol)
if lastFd == osInvalidSocket: if lastFd == osInvalidSocket:
# we always raise if socket creation failed, because it means a # we always raise if socket creation failed, because it means a
# network system problem (e.g. not enough FDs), and not an unreachable # network system problem (e.g. not enough FDs), and not an unreachable

View file

@ -315,6 +315,21 @@ else:
else: else:
include ioselects/ioselectors_poll include ioselects/ioselectors_poll
{.deprecated: [setEvent: trigger].} proc register*[T](s: Selector[T], fd: int | SocketHandle,
{.deprecated: [register: registerHandle].} events: set[Event], data: T) {.deprecated.} =
{.deprecated: [update: updateHandle].} ## **Deprecated since v0.18.0:** Use ``registerHandle`` instead.
s.registerHandle(fd, events, data)
proc setEvent*(ev: SelectEvent) {.deprecated.} =
## Trigger event ``ev``.
##
## **Deprecated since v0.18.0:** Use ``trigger`` instead.
ev.trigger()
proc update*[T](s: Selector[T], fd: int | SocketHandle,
events: set[Event]) {.deprecated.} =
## Update file/socket descriptor ``fd``, registered in selector
## ``s`` with new events set ``event``.
##
## **Deprecated since v0.18.0:** Use ``updateHandle`` instead.
s.updateHandle()