Revert 3db460f504 as requested by @Araq.

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

View File

@@ -14,13 +14,8 @@
module.
- `newAsyncSocket` taking an `AsyncFD` now runs `setBlocking(false)` on the
fd.
### Library changes
- The `ReadyKey` type in the selectors module now contains an ``errorCode``
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
``nativesockets``.
- Implemented ``getIoHandler`` proc in the ``asyncdispatch`` module that allows

View File

@@ -222,7 +222,7 @@ when defined(windows) or defined(nimdoc):
PCustomOverlapped* = ref CustomOverlapped
AsyncFD* = distinct int ## An FD that is registered in the dispatcher.
AsyncFD* = distinct int
PostCallbackData = object
ioPort: Handle
@@ -270,22 +270,14 @@ when defined(windows) or defined(nimdoc):
## (Unix) for the specified dispatcher.
return disp.ioPort
proc register*(fd: cint | SocketHandle | AsyncFD): AsyncFD {.discardable.} =
proc register*(fd: AsyncFD) =
## 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()
when fd is AsyncFD:
if fd in p.handles:
return
if createIoCompletionPort(fd.Handle, p.ioPort,
cast[CompletionKey](fd), 1) == 0:
raiseOSError(osLastError())
p.handles.incl(fd.AsyncFD)
return fd.AsyncFD
p.handles.incl(fd)
proc verifyPresence(fd: AsyncFD) =
## Ensures that file descriptor has been registered with the dispatcher.
@@ -771,8 +763,8 @@ when defined(windows) or defined(nimdoc):
## Unregisters ``fd``.
getGlobalDispatcher().handles.excl(fd)
proc contains*(disp: PDispatcher, fd: AsyncFd | SocketHandle): bool =
return fd.SocketHandle in disp.handles
proc contains*(disp: PDispatcher, fd: AsyncFD): bool =
return fd in disp.handles
{.push stackTrace:off.}
proc waitableCallback(param: pointer,
@@ -994,7 +986,7 @@ when defined(windows) or defined(nimdoc):
proc newAsyncEvent*(): AsyncEvent =
## 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``.
var sa = SECURITY_ATTRIBUTES(
nLength: sizeof(SECURITY_ATTRIBUTES).cint,
@@ -1115,14 +1107,10 @@ else:
proc getIoHandler*(disp: PDispatcher): Selector[AsyncData] =
return disp.selector
proc register*(fd: cint | SocketHandle | AsyncFD): AsyncFD {.discardable.} =
proc register*(fd: AsyncFD) =
let p = getGlobalDispatcher()
when fd is AsyncFD:
if fd.SocketHandle in p.selector:
return
var data = newAsyncData()
p.selector.registerHandle(fd.SocketHandle, {}, data)
return fd.AsyncFD
proc closeSocket*(sock: AsyncFD) =
let disp = getGlobalDispatcher()
@@ -1135,7 +1123,7 @@ else:
proc unregister*(ev: AsyncEvent) =
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
proc addRead*(fd: AsyncFD, cb: Callback) =

View File

@@ -81,10 +81,11 @@ proc getFileSize*(f: AsyncFile): int64 =
else:
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`.
new result
result.fd = register(fd)
result.fd = fd
register(fd)
proc openAsync*(filename: string, mode = fmRead): AsyncFile =
## 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:
raiseOSError(osLastError())
result = newAsyncFile(fd.cint)
result = newAsyncFile(fd.AsyncFd)
if mode == fmAppend:
result.offset = getFileSize(result)
@@ -115,10 +116,10 @@ proc openAsync*(filename: string, mode = fmRead): AsyncFile =
# RW (Owner), RW (Group), R (Other)
let perm = S_IRUSR or S_IWUSR or S_IRGRP or S_IWGRP or S_IROTH
let fd = open(filename, flags, perm)
if fd.cint == -1:
if fd == -1:
raiseOSError(osLastError())
result = newAsyncFile(fd)
result = newAsyncFile(fd.AsyncFd)
proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] =
## 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
## async dispatcher. You need to do this manually. If you have used
## ``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
new(result)
result.fd = fd.SocketHandle