Quic-simple: Quick-start wrappers for QUIC

If you want, for some reason, to interleave rapid-fire RPC with file transfer while hopping between the networks (or just dislike websockets), and/but can’t be bothered with setting up a proper TLS PKI…

Here’s a quic-and-dirty example:

data ClientMessage
  = Hello
  | Bye
  deriving (Eq, Show, Ord, Generic)

instance QUIC.Serialise ClientMessage

data ServerMessage
  = Ok Int
  deriving (Eq, Show, Ord, Generic)

instance QUIC.Serialise ServerMessage

serverSimple :: IO ()
serverSimple = do
  counter <- newIORef 0
  QUIC.runServerSimple "127.0.0.1" 14443 \case
    Hello -> do
      putStrLn "Server got Hello"
      n <- atomicModifyIORef' counter \old -> (old + 1, old)
      pure $ Ok n
    Bye -> do
      putStrLn "Server got Bye"
      error "Whelp, the serverSimple must reply, but the protocol must stop. Needs a redesign."

clientSimple :: IO ()
clientSimple = do
  (stop, call) <- QUIC.startClientSimple "127.0.0.1" "14443"
  replicateM_ 5 do
    Ok n <- call Hello
    putStrLn $ "Client got reply " <> show n
  timeout 1000000 (call Bye) >>= mapM_ \reply ->
    putStrLn $ "Shouldn't happen, the server errors out on this: " <> show reply
  putStrLn "Stopping"
  stop

The code is separated into layers and is intended to be used as a template. Replace the wrappers with code and adjust it as you move from a prototype to a working system with requirements.

10 Likes

After some feedback and actually using the thing I found that the simple wrappers ended up being too simple.
So quic-simple-0.1.1.0 now provides more flexible clients and servers, while still cutting some boilerplate.

The extra hooks appear to be powerful enough to accommodate some real-world-ish designs:
https://gitlab.com/dpwiz/quic-simple/-/blob/2909aa15d983a1bb47fd610cb44fbf4430462fa7/test/Spec.hs#L235

3 Likes

do you plan to also publish a full example app using that? looks like I can be one of your downstreams (a WIP http server testing DSL that will support http 3 eventually) @wiz

Like, making a http3 client/server over QUIC? Kazu is already on this: http3: HTTP/3 library

My goal here is to make QUIC a ready-to-go library for multiplayer games instead of reinventing it over UDP or using specialized netcode libraries for that. I’ll try to make some demo at an appropriate scale, but I feel that would require implementing RFC 9221 - An Unreliable Datagram Extension to QUIC first.

3 Likes