GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - c-cube/ldl: [TOY] loop-dee-loop, an event loop

BranchesTags
Open more actions menu

Latest commit

 

History

8 Commits
8 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
example
example
 
 
src
src
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.ocamlformat
.ocamlformat
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
dune
dune
 
 
dune-project
dune-project
 
 
ldl.opam
ldl.opam
 
 

Repository files navigation

LDL

A toy event loop using poll and OCaml 5's effect handlers. I'm learning.

Some toy programs can be found in 'examples'. I didn't bother adding buffering or proper channels, it's all raw unix FDs.

cat

let () =
  Ldl.run @@ fun () ->
  let stdin = Ldl.File.stdin () in
  let stdout = Ldl.File.stdout () in

  let buf = Bytes.create 32 in

  let continue = ref true in
  while !continue do
    let n = Ldl.FD.read stdin buf 0 (Bytes.length buf) in
    if n = 0 then
      continue := false
    else
      Ldl.FD.write_all stdout buf 0 n
  done

TCP echo server

let () =
  let port = ref 8085 in
  let debug = ref false in

  let opts =
    [ "-p", Arg.Set_int port, " port"; "-d", Arg.Set debug, " debug" ]
    |> Arg.align
  in
  Arg.parse opts ignore "";

  epf "listen on port %d\n%!" !port;
  let server_sock = Ldl.Net.listen_local ~port:!port () in

  Ldl.run @@ fun () ->
  while true do
    let client_sock, client_addr = Ldl.Net.accept server_sock in

    let client_ip, client_port =
      match client_addr with
      | Unix.ADDR_INET (ip, port) -> Unix.string_of_inet_addr ip, port
      | _ -> assert false
    in
    if !debug then epf "got client on %s:%d!\n%!" client_ip client_port;

    ignore
      (Ldl.spawn (fun () ->
          let buf = Bytes.create 128 in
          let continue = ref true in
          while !continue do
            let n = Ldl.FD.read client_sock buf 0 (Bytes.length buf) in
            if !debug then
              epf "got %d bytes from client %s:%d\n%!" n client_ip client_port;
            if n > 0 then
              Ldl.FD.write_all client_sock buf 0 n
            else
              continue := false
          done;
          if !debug then
            epf "client %s:%d disconnected\n%!" client_ip client_port)
        : Ldl.Fut.t);
  done

About

[TOY] loop-dee-loop, an event loop

Topics

epollevent-loopkqueueocamlocaml5toy

Resources

Readme
Activity

Stars

3 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.