Asyncio2 doc improvements. Changed recvLine's behaviour.
recvLine had an edge case where it would return a partial line if the socket were to be disconnected in the middle of a line being read. The behaviour now is that the data is simply lost as it likely will be unparsable by the protocol anyway.
This commit is contained in:
parent
a4f30c3682
commit
ca6b4bf0f9
1 changed files with 9 additions and 11 deletions
|
|
@ -93,18 +93,15 @@ proc read*[T](future: PFuture[T]): T =
|
||||||
proc finished*[T](future: PFuture[T]): bool =
|
proc finished*[T](future: PFuture[T]): bool =
|
||||||
## Determines whether ``future`` has completed.
|
## Determines whether ``future`` has completed.
|
||||||
##
|
##
|
||||||
## ``True`` may indicate an error or a value. Use ``hasError`` to distinguish.
|
## ``True`` may indicate an error or a value. Use ``failed`` to distinguish.
|
||||||
future.finished
|
future.finished
|
||||||
|
|
||||||
proc failed*[T](future: PFuture[T]): bool =
|
proc failed*[T](future: PFuture[T]): bool =
|
||||||
## Determines whether ``future`` completed with an error.
|
## Determines whether ``future`` completed with an error.
|
||||||
future.error != nil
|
future.error != nil
|
||||||
|
|
||||||
# TODO: Get rid of register. Do it implicitly.
|
|
||||||
|
|
||||||
when defined(windows) or defined(nimdoc):
|
when defined(windows) or defined(nimdoc):
|
||||||
import winlean, sets, hashes
|
import winlean, sets, hashes
|
||||||
#from hashes import THash
|
|
||||||
type
|
type
|
||||||
TCompletionKey = dword
|
TCompletionKey = dword
|
||||||
|
|
||||||
|
|
@ -794,6 +791,9 @@ proc getName(node: PNimrodNode): string {.compileTime.} =
|
||||||
assert false
|
assert false
|
||||||
|
|
||||||
macro async*(prc: stmt): stmt {.immediate.} =
|
macro async*(prc: stmt): stmt {.immediate.} =
|
||||||
|
## Macro which processes async procedures into the appropriate
|
||||||
|
## iterators and yield statements.
|
||||||
|
|
||||||
expectKind(prc, nnkProcDef)
|
expectKind(prc, nnkProcDef)
|
||||||
|
|
||||||
hint("Processing " & prc[0].getName & " as an async proc.")
|
hint("Processing " & prc[0].getName & " as an async proc.")
|
||||||
|
|
@ -893,6 +893,10 @@ proc recvLine*(p: PDispatcher, socket: TSocketHandle): PFuture[string] {.async.}
|
||||||
## will be set to it.
|
## will be set to it.
|
||||||
##
|
##
|
||||||
## If the socket is disconnected, ``line`` will be set to ``""``.
|
## If the socket is disconnected, ``line`` will be set to ``""``.
|
||||||
|
##
|
||||||
|
## If the socket is disconnected in the middle of a line (before ``\r\L``
|
||||||
|
## is read) then line will be set to ``""``.
|
||||||
|
## The partial line **will be lost**.
|
||||||
|
|
||||||
template addNLIfEmpty(): stmt =
|
template addNLIfEmpty(): stmt =
|
||||||
if result.len == 0:
|
if result.len == 0:
|
||||||
|
|
@ -901,13 +905,9 @@ proc recvLine*(p: PDispatcher, socket: TSocketHandle): PFuture[string] {.async.}
|
||||||
result = ""
|
result = ""
|
||||||
var c = ""
|
var c = ""
|
||||||
while true:
|
while true:
|
||||||
#echo("1")
|
|
||||||
c = await p.recv(socket, 1)
|
c = await p.recv(socket, 1)
|
||||||
#echo("Received ", c.len)
|
|
||||||
if c.len == 0:
|
if c.len == 0:
|
||||||
#echo("returning")
|
return ""
|
||||||
return
|
|
||||||
#echo("2")
|
|
||||||
if c == "\r":
|
if c == "\r":
|
||||||
c = await p.recv(socket, 1, MSG_PEEK)
|
c = await p.recv(socket, 1, MSG_PEEK)
|
||||||
if c.len > 0 and c == "\L":
|
if c.len > 0 and c == "\L":
|
||||||
|
|
@ -917,9 +917,7 @@ proc recvLine*(p: PDispatcher, socket: TSocketHandle): PFuture[string] {.async.}
|
||||||
elif c == "\L":
|
elif c == "\L":
|
||||||
addNLIfEmpty()
|
addNLIfEmpty()
|
||||||
return
|
return
|
||||||
#echo("3")
|
|
||||||
add(result.string, c)
|
add(result.string, c)
|
||||||
#echo("4")
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue