First implementation of the async/await-like system with macros.
This commit is contained in:
parent
84534ce4b8
commit
75893d1b36
3 changed files with 520 additions and 57 deletions
24
tests/compile/tasyncitermacro.nim
Normal file
24
tests/compile/tasyncitermacro.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import sockets, asyncio, strutils
|
||||
proc processRequest(client: PAsyncSocket, test: string) {.async.} =
|
||||
assert test == "ahha"
|
||||
assert client != nil
|
||||
echo(test)
|
||||
client.close()
|
||||
|
||||
proc processServer() {.async.} =
|
||||
var sock = AsyncSocket()
|
||||
|
||||
# blocks:
|
||||
sock.bindAddr(TPort(6667))
|
||||
sock.listen()
|
||||
|
||||
# Accept loop
|
||||
while true:
|
||||
let client: PAsyncSocket = await(accept(sock))
|
||||
assert client != nil
|
||||
await processRequest(client, "ahha")
|
||||
|
||||
var disp = newDispatcher(false)
|
||||
disp.register(processServer, nil)
|
||||
while true:
|
||||
discard disp.poll()
|
||||
69
tests/compile/tasynciterraw.nim
Normal file
69
tests/compile/tasynciterraw.nim
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import asyncio, sockets
|
||||
|
||||
const qwerty = "qwertyuiopasdfghjklzxcvbnm\c\L"
|
||||
|
||||
iterator processRequest(x: PRequest): PRequest {.closure.} =
|
||||
# Read first message
|
||||
let client = x.socket
|
||||
var lineReq = PRequest(socket: client, kind: reqReadLine, line: "")
|
||||
yield lineReq
|
||||
|
||||
if lineReq.line == "HELLO":
|
||||
echo "Got first message."
|
||||
|
||||
var longString = qwerty
|
||||
for i in 0..109000:
|
||||
longString.add($i & qwerty)
|
||||
|
||||
var writeReq = PRequest(socket: client, kind: reqWrite, toWrite: longString)
|
||||
yield writeReq
|
||||
|
||||
# Read second message: No need to create another request object:
|
||||
yield lineReq
|
||||
|
||||
if lineReq.line == "DOM":
|
||||
echo("Got second message.")
|
||||
|
||||
client.close()
|
||||
|
||||
|
||||
iterator processServer(x: PRequest): PRequest {.closure.} =
|
||||
type
|
||||
PTest = ref object of TObject
|
||||
param: string
|
||||
|
||||
let passedInParams = cast[PTest](x.param)
|
||||
assert passedInParams.param == "blah"
|
||||
|
||||
var sock = AsyncSocket()
|
||||
|
||||
# blocks:
|
||||
sock.bindAddr(TPort(6667))
|
||||
sock.listen()
|
||||
|
||||
# Accept loop
|
||||
while true:
|
||||
var acceptReq = PRequest(socket: sock, kind: reqAccept, client: nil)
|
||||
echo("About to yield accept")
|
||||
yield acceptReq
|
||||
echo("after yield")
|
||||
if acceptReq.hasException:
|
||||
echo("Got exception[EAssertionFailed]: ", acceptReq.exc of EAssertionFailed)
|
||||
echo(acceptReq.exc.msg)
|
||||
continue
|
||||
|
||||
let client = acceptReq.client
|
||||
assert client != nil
|
||||
yield PRequest(socket: client, kind: reqReg, param: nil, worker: processRequest)
|
||||
|
||||
when isMainModule:
|
||||
let param = "blah"
|
||||
type
|
||||
PTest = ref object of TObject
|
||||
param: type(param)
|
||||
var test = PTest(param: param)
|
||||
var disp = newDispatcher(false)
|
||||
disp.register(processServer, test)
|
||||
while true:
|
||||
discard disp.poll()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue