Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stdio client

# stdio_client.nim

{.push gcsafe, raises: [].}

import std/[os, osproc]
import json_rpc/clients/stdioclient
import ./rpc_format

const serverPath = currentSourcePath().parentDir() / "stdio_server"

createRpcSigsFromNim(RpcClient, RpcConv):
  proc hello(input: string): string

proc main() {.async: (raises: [CancelledError, JsonRpcError]).} =
  let peerExe = serverPath.addFileExt(ExeExt)

  const framing = Framing.httpHeader()
  let client = newRpcStdioClient(framing = framing)
  await client.connect(peerExe)
  defer:
    # Cancel/Timeout kills the server if it has not exited in time
    discard await client.close().withTimeout(5.seconds)
    doAssert client.exitCode() == Opt.some(0)

  let resp = await client.hello("Daisy")
  doAssert resp == "Hello Daisy"

proc buildStdioServer() =
  const mode =
    when defined(release):
      "-d:release"
    elif defined(danger):
      "-d:danger"
    else:
      ""
  const flags = "--threads:on -d:chronicles_log_level=ERROR -d:chronicles_default_output_device=stderr"
  let res = try:
    execCmdEx("nim c " & mode & " " & flags & " " & serverPath)
  except CatchableError as err:
    raiseAssert "Failed to build server: " & err.msg
  doAssert res.exitCode == 0, "Failed to build server: " & res.output

when isMainModule:
  buildStdioServer()
  waitFor main()
  echo "ok"