Revert 3db460f504 as requested by @Araq.

This commit is contained in:
Dominik Picheta 2018-01-17 16:48:02 +00:00
commit 86fb8bf723
4 changed files with 14 additions and 32 deletions

View file

@ -14,13 +14,8 @@
module. module.
- `newAsyncSocket` taking an `AsyncFD` now runs `setBlocking(false)` on the - `newAsyncSocket` taking an `AsyncFD` now runs `setBlocking(false)` on the
fd. fd.
### Library changes
- The `ReadyKey` type in the selectors module now contains an ``errorCode`` - The `ReadyKey` type in the selectors module now contains an ``errorCode``
field to help distinguish between ``Event.Error`` events. field to help distinguish between ``Event.Error`` events.
- The `AsyncFD` type now reflects the fact that the underlying FD is registered
in the async dispatcher.
- Implemented an `accept` proc that works on a `SocketHandle` in - Implemented an `accept` proc that works on a `SocketHandle` in
``nativesockets``. ``nativesockets``.
- Implemented ``getIoHandler`` proc in the ``asyncdispatch`` module that allows - Implemented ``getIoHandler`` proc in the ``asyncdispatch`` module that allows

View file

@ -222,7 +222,7 @@ when defined(windows) or defined(nimdoc):
PCustomOverlapped* = ref CustomOverlapped PCustomOverlapped* = ref CustomOverlapped
AsyncFD* = distinct int ## An FD that is registered in the dispatcher. AsyncFD* = distinct int
PostCallbackData = object PostCallbackData = object
ioPort: Handle ioPort: Handle
@ -270,22 +270,14 @@ when defined(windows) or defined(nimdoc):
## (Unix) for the specified dispatcher. ## (Unix) for the specified dispatcher.
return disp.ioPort return disp.ioPort
proc register*(fd: cint | SocketHandle | AsyncFD): AsyncFD {.discardable.} = proc register*(fd: AsyncFD) =
## Registers ``fd`` with the dispatcher. ## Registers ``fd`` with the dispatcher.
##
## By convention, an ``AsyncFD`` is said to be already registered in the
## dispatcher. This procedure will raise an exception if ``fd`` has already
## been registered, but only if the type of the ``fd`` isn't ``AsyncFD``.
let p = getGlobalDispatcher() let p = getGlobalDispatcher()
when fd is AsyncFD:
if fd in p.handles:
return
if createIoCompletionPort(fd.Handle, p.ioPort, if createIoCompletionPort(fd.Handle, p.ioPort,
cast[CompletionKey](fd), 1) == 0: cast[CompletionKey](fd), 1) == 0:
raiseOSError(osLastError()) raiseOSError(osLastError())
p.handles.incl(fd.AsyncFD) p.handles.incl(fd)
return fd.AsyncFD
proc verifyPresence(fd: AsyncFD) = proc verifyPresence(fd: AsyncFD) =
## Ensures that file descriptor has been registered with the dispatcher. ## Ensures that file descriptor has been registered with the dispatcher.
@ -771,8 +763,8 @@ when defined(windows) or defined(nimdoc):
## Unregisters ``fd``. ## Unregisters ``fd``.
getGlobalDispatcher().handles.excl(fd) getGlobalDispatcher().handles.excl(fd)
proc contains*(disp: PDispatcher, fd: AsyncFd | SocketHandle): bool = proc contains*(disp: PDispatcher, fd: AsyncFD): bool =
return fd.SocketHandle in disp.handles return fd in disp.handles
{.push stackTrace:off.} {.push stackTrace:off.}
proc waitableCallback(param: pointer, proc waitableCallback(param: pointer,
@ -994,7 +986,7 @@ when defined(windows) or defined(nimdoc):
proc newAsyncEvent*(): AsyncEvent = proc newAsyncEvent*(): AsyncEvent =
## Creates a new thread-safe ``AsyncEvent`` object. ## Creates a new thread-safe ``AsyncEvent`` object.
## ##
## New ``AsyncEvent`` object is not automatically registered with # TODO: Why? -- DP ## New ``AsyncEvent`` object is not automatically registered with
## dispatcher like ``AsyncSocket``. ## dispatcher like ``AsyncSocket``.
var sa = SECURITY_ATTRIBUTES( var sa = SECURITY_ATTRIBUTES(
nLength: sizeof(SECURITY_ATTRIBUTES).cint, nLength: sizeof(SECURITY_ATTRIBUTES).cint,
@ -1115,14 +1107,10 @@ else:
proc getIoHandler*(disp: PDispatcher): Selector[AsyncData] = proc getIoHandler*(disp: PDispatcher): Selector[AsyncData] =
return disp.selector return disp.selector
proc register*(fd: cint | SocketHandle | AsyncFD): AsyncFD {.discardable.} = proc register*(fd: AsyncFD) =
let p = getGlobalDispatcher() let p = getGlobalDispatcher()
when fd is AsyncFD:
if fd.SocketHandle in p.selector:
return
var data = newAsyncData() var data = newAsyncData()
p.selector.registerHandle(fd.SocketHandle, {}, data) p.selector.registerHandle(fd.SocketHandle, {}, data)
return fd.AsyncFD
proc closeSocket*(sock: AsyncFD) = proc closeSocket*(sock: AsyncFD) =
let disp = getGlobalDispatcher() let disp = getGlobalDispatcher()
@ -1135,7 +1123,7 @@ else:
proc unregister*(ev: AsyncEvent) = proc unregister*(ev: AsyncEvent) =
getGlobalDispatcher().selector.unregister(SelectEvent(ev)) getGlobalDispatcher().selector.unregister(SelectEvent(ev))
proc contains*(disp: PDispatcher, fd: AsyncFd | SocketHandle): bool = proc contains*(disp: PDispatcher, fd: AsyncFd): bool =
return fd.SocketHandle in disp.selector return fd.SocketHandle in disp.selector
proc addRead*(fd: AsyncFD, cb: Callback) = proc addRead*(fd: AsyncFD, cb: Callback) =

View file

@ -81,10 +81,11 @@ proc getFileSize*(f: AsyncFile): int64 =
else: else:
result = lseek(f.fd.cint, 0, SEEK_END) result = lseek(f.fd.cint, 0, SEEK_END)
proc newAsyncFile*(fd: cint | AsyncFd): AsyncFile = proc newAsyncFile*(fd: AsyncFd): AsyncFile =
## Creates `AsyncFile` with a previously opened file descriptor `fd`. ## Creates `AsyncFile` with a previously opened file descriptor `fd`.
new result new result
result.fd = register(fd) result.fd = fd
register(fd)
proc openAsync*(filename: string, mode = fmRead): AsyncFile = proc openAsync*(filename: string, mode = fmRead): AsyncFile =
## Opens a file specified by the path in ``filename`` using ## Opens a file specified by the path in ``filename`` using
@ -105,7 +106,7 @@ proc openAsync*(filename: string, mode = fmRead): AsyncFile =
if fd == INVALID_HANDLE_VALUE: if fd == INVALID_HANDLE_VALUE:
raiseOSError(osLastError()) raiseOSError(osLastError())
result = newAsyncFile(fd.cint) result = newAsyncFile(fd.AsyncFd)
if mode == fmAppend: if mode == fmAppend:
result.offset = getFileSize(result) result.offset = getFileSize(result)
@ -115,10 +116,10 @@ proc openAsync*(filename: string, mode = fmRead): AsyncFile =
# RW (Owner), RW (Group), R (Other) # RW (Owner), RW (Group), R (Other)
let perm = S_IRUSR or S_IWUSR or S_IRGRP or S_IWGRP or S_IROTH let perm = S_IRUSR or S_IWUSR or S_IRGRP or S_IWGRP or S_IROTH
let fd = open(filename, flags, perm) let fd = open(filename, flags, perm)
if fd.cint == -1: if fd == -1:
raiseOSError(osLastError()) raiseOSError(osLastError())
result = newAsyncFile(fd) result = newAsyncFile(fd.AsyncFd)
proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] = proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] =
## Read ``size`` bytes from the specified file asynchronously starting at ## Read ``size`` bytes from the specified file asynchronously starting at

View file

@ -146,8 +146,6 @@ proc newAsyncSocket*(fd: AsyncFD, domain: Domain = AF_INET,
## **Note**: This procedure will **NOT** register ``fd`` with the global ## **Note**: This procedure will **NOT** register ``fd`` with the global
## async dispatcher. You need to do this manually. If you have used ## async dispatcher. You need to do this manually. If you have used
## ``newAsyncNativeSocket`` to create ``fd`` then it's already registered. ## ``newAsyncNativeSocket`` to create ``fd`` then it's already registered.
## The reason for this is that the ``AsyncFD`` type is a special type for an
## FD that signifies that its been registered.
assert fd != osInvalidSocket.AsyncFD assert fd != osInvalidSocket.AsyncFD
new(result) new(result)
result.fd = fd.SocketHandle result.fd = fd.SocketHandle