Allow async lambdas. Other socket modifications.
This commit is contained in:
parent
390bbbdd05
commit
7ebbc09578
4 changed files with 25 additions and 12 deletions
|
|
@ -27,9 +27,11 @@ export TPort
|
||||||
|
|
||||||
|
|
||||||
# TODO: Discarded void PFutures need to checked for exception.
|
# TODO: Discarded void PFutures need to checked for exception.
|
||||||
# TODO: Exceptions are currently uncatchable due to the limitation that
|
# TODO: ``except`` statement (without `try`) does not work.
|
||||||
# you cannot have yield in a try stmt. Perhaps I can get the macro to put
|
# TODO: Multiple exception names in a ``except`` don't work.
|
||||||
# a user's try except around ``future.read``.
|
# TODO: The effect system (raises: []) has trouble with my try transformation.
|
||||||
|
# TODO: Can't await in a 'except' body
|
||||||
|
|
||||||
|
|
||||||
# -- Futures
|
# -- Futures
|
||||||
|
|
||||||
|
|
@ -922,14 +924,17 @@ proc getName(node: PNimrodNode): string {.compileTime.} =
|
||||||
return $node[1].ident
|
return $node[1].ident
|
||||||
of nnkIdent:
|
of nnkIdent:
|
||||||
return $node.ident
|
return $node.ident
|
||||||
|
of nnkEmpty:
|
||||||
|
return "anonymous"
|
||||||
else:
|
else:
|
||||||
assert false
|
error("Unknown name.")
|
||||||
|
|
||||||
macro async*(prc: stmt): stmt {.immediate.} =
|
macro async*(prc: stmt): stmt {.immediate.} =
|
||||||
## Macro which processes async procedures into the appropriate
|
## Macro which processes async procedures into the appropriate
|
||||||
## iterators and yield statements.
|
## iterators and yield statements.
|
||||||
|
if prc.kind notin {nnkProcDef, nnkLambda}:
|
||||||
expectKind(prc, nnkProcDef)
|
error("Cannot transform this node kind into an async proc." &
|
||||||
|
" Proc definition or lambda node expected.")
|
||||||
|
|
||||||
hint("Processing " & prc[0].getName & " as an async proc.")
|
hint("Processing " & prc[0].getName & " as an async proc.")
|
||||||
|
|
||||||
|
|
@ -941,7 +946,9 @@ macro async*(prc: stmt): stmt {.immediate.} =
|
||||||
if $returnType[0] != "PFuture":
|
if $returnType[0] != "PFuture":
|
||||||
error("Expected return type of 'PFuture' got '" & $returnType[0] & "'")
|
error("Expected return type of 'PFuture' got '" & $returnType[0] & "'")
|
||||||
|
|
||||||
let subtypeIsVoid = returnType.kind == nnkEmpty
|
let subtypeIsVoid = returnType.kind == nnkEmpty or
|
||||||
|
(returnType.kind == nnkBracketExpr and
|
||||||
|
returnType[1].kind == nnkIdent and returnType[1].ident == !"void")
|
||||||
|
|
||||||
var outerProcBody = newNimNode(nnkStmtList)
|
var outerProcBody = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
|
|
@ -990,17 +997,19 @@ macro async*(prc: stmt): stmt {.immediate.} =
|
||||||
|
|
||||||
# Remove the 'async' pragma.
|
# Remove the 'async' pragma.
|
||||||
for i in 0 .. <result[4].len:
|
for i in 0 .. <result[4].len:
|
||||||
if result[4][i].ident == !"async":
|
if result[4][i].kind == nnkIdent and result[4][i].ident == !"async":
|
||||||
result[4].del(i)
|
result[4].del(i)
|
||||||
if subtypeIsVoid:
|
if subtypeIsVoid:
|
||||||
# Add discardable pragma.
|
# Add discardable pragma.
|
||||||
result[4].add(newIdentNode("discardable"))
|
if prc.kind == nnkProcDef: # TODO: This is a workaround for #1287
|
||||||
|
result[4].add(newIdentNode("discardable"))
|
||||||
if returnType.kind == nnkEmpty:
|
if returnType.kind == nnkEmpty:
|
||||||
# Add PFuture[void]
|
# Add PFuture[void]
|
||||||
result[3][0] = parseExpr("PFuture[void]")
|
result[3][0] = parseExpr("PFuture[void]")
|
||||||
|
|
||||||
result[6] = outerProcBody
|
result[6] = outerProcBody
|
||||||
|
|
||||||
|
#echo(treeRepr(result))
|
||||||
#echo(toStrLit(result))
|
#echo(toStrLit(result))
|
||||||
|
|
||||||
proc recvLine*(socket: TAsyncFD): PFuture[string] {.async.} =
|
proc recvLine*(socket: TAsyncFD): PFuture[string] {.async.} =
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,13 @@
|
||||||
import strtabs, asyncnet, asyncdispatch, parseutils, parseurl, strutils
|
import strtabs, asyncnet, asyncdispatch, parseutils, parseurl, strutils
|
||||||
type
|
type
|
||||||
TRequest* = object
|
TRequest* = object
|
||||||
client: PAsyncSocket # TODO: Separate this into a Response object?
|
client*: PAsyncSocket # TODO: Separate this into a Response object?
|
||||||
reqMethod*: string
|
reqMethod*: string
|
||||||
headers*: PStringTable
|
headers*: PStringTable
|
||||||
protocol*: tuple[orig: string, major, minor: int]
|
protocol*: tuple[orig: string, major, minor: int]
|
||||||
url*: TURL
|
url*: TURL
|
||||||
hostname*: string ## The hostname of the client that made the request.
|
hostname*: string ## The hostname of the client that made the request.
|
||||||
|
body*: string # TODO
|
||||||
|
|
||||||
PAsyncHttpServer* = ref object
|
PAsyncHttpServer* = ref object
|
||||||
socket: PAsyncSocket
|
socket: PAsyncSocket
|
||||||
|
|
@ -169,6 +170,10 @@ proc serve*(server: PAsyncHttpServer, port: TPort,
|
||||||
var fut = await server.socket.acceptAddr()
|
var fut = await server.socket.acceptAddr()
|
||||||
processClient(fut.client, fut.address, callback)
|
processClient(fut.client, fut.address, callback)
|
||||||
|
|
||||||
|
proc close*(server: PAsyncHttpServer) =
|
||||||
|
## Terminates the async http server instance.
|
||||||
|
server.socket.close()
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var server = newAsyncHttpServer()
|
var server = newAsyncHttpServer()
|
||||||
proc cb(req: TRequest) {.async.} =
|
proc cb(req: TRequest) {.async.} =
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
{.deadCodeElim: on.}
|
{.deadCodeElim: on.}
|
||||||
import rawsockets, os, strutils, unsigned, parseutils, times
|
import rawsockets, os, strutils, unsigned, parseutils, times
|
||||||
export TPort
|
export TPort, `$`
|
||||||
|
|
||||||
const useWinVersion = defined(Windows) or defined(nimdoc)
|
const useWinVersion = defined(Windows) or defined(nimdoc)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,6 @@ export
|
||||||
MSG_PEEK
|
MSG_PEEK
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
TPort* = distinct uint16 ## port type
|
TPort* = distinct uint16 ## port type
|
||||||
|
|
||||||
TDomain* = enum ## domain, which specifies the protocol family of the
|
TDomain* = enum ## domain, which specifies the protocol family of the
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue