changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / examples/net/yoctochat.lisp

changeset 41: 81b7333f27f8
author: Richard Westhaver <ellis@rwest.io>
date: Sun, 16 Jun 2024 22:15:04 -0400
permissions: -rw-r--r--
description: more examples
1 ;;; examples/net/yoctochat.lisp --- Yoctochat Implementation
2 
3 ;; The tiniest (lisp) chat server on earth!
4 
5 ;; based on https://github.com/robn/yoctochat
6 
7 ;; A 'yoctochat' server will:
8 
9 ;; - take a single commandline argument, the port to listen on
10 ;; - open a listening port
11 ;; - handle multiple connections and disconnections on that port
12 ;; - receive text on a connection, and forward it on to all ofhter connections
13 ;; - produce simple output about what it's doing
14 ;; - demonstrate a single IO multiplexing technique as simply as possible
15 ;; - be well commented!
16 
17 ;;; Commentary:
18 
19 ;; This implementation is based on the yc_uring.c implementation which
20 ;; uses io_uring. To use io_uring from Lisp, we use the high-level IO
21 ;; package, which internally calls foreign functions defined in the
22 ;; URING package.
23 
24 ;;
25 
26 ;;; Code:
27 (defpackage :examples/yoctochat
28  (:use :cl :std :net :cli/clap :io :log :sb-alien)
29  (:import-from :uring :load-uring))
30 
31 (in-package :examples/yoctochat)
32 
33 ;; To start using the IO package we should make sure the liburing
34 ;; shared library is properly loaded. This function takes care of that
35 ;; and arranges for the library to be remembered when entering a saved
36 ;; lisp image such that it will be automatically re-opened.
37 (load-uring t)
38 
39 ;; Initialize a simple logger to report on what's happening.
40 ;; (setq *logger* (make-logger nil))
41 
42 ;; Define some parameters for the queue depth and maximum number of
43 ;; connections allowed on a single server.
44 (defparameter *num-conns* 128)
45 
46 (defparameter *queue-depth* (* 2 *num-conns*))
47 
48 (defclass yc-server (server)
49  ((connections :initform nil ::type sequence))
50  (:documentation "The Yoctochat Server. "))
51 
52 ;; The main loop of our yoctochat server. The 'defmain' macro will
53 ;; produce a function 'main' which can be saved as an executable
54 ;; entry-point.
55 (defmain ()
56  (init-io *queue-depth*)
57  (setf *io* nil))
58 
59 
60