Created
May 15, 2019 22:36
-
-
Save jrfondren/bbbc3a7eb3c30d3485a3e2913557d3d2 to your computer and use it in GitHub Desktop.
ways to handle local IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncnet, asyncdispatch, threadpool | |
proc echoStdin = | |
var line = spawn stdin.readLine() | |
while true: | |
if line.isReady(): | |
echo "You said: " & ^line | |
line = spawn stdin.readLine() | |
asyncdispatch.drain(60) | |
proc accept {.async.} = | |
var serv = newAsyncSocket() | |
serv.setSockOpt(OptReuseAddr, true) | |
serv.bindAddr(Port(1079)) | |
serv.listen() | |
while true: | |
let client = await serv.accept() | |
discard | |
asyncCheck accept() | |
echoStdin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncnet, asyncdispatch, threadpool | |
proc echoStdin = | |
var line = spawn stdin.readLine() | |
addTimer(120, false, proc (fd: AsyncFD): bool {.closure, gcsafe.} = | |
if line.isReady(): | |
echo "You said: " & ^line | |
line = spawn stdin.readLine() | |
) | |
proc accept {.async.} = | |
var serv = newAsyncSocket() | |
serv.setSockOpt(OptReuseAddr, true) | |
serv.bindAddr(Port(1079)) | |
serv.listen() | |
while true: | |
let client = await serv.accept() | |
echo "Got a client" | |
discard | |
echoStdin() | |
asyncCheck accept() | |
runForever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment