changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/net/tcp.lisp

changeset 356: aac665e2f5bf
parent: f3d814fb136a
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 21 May 2024 17:13:34 -0400
permissions: -rw-r--r--
description: stashed and revert some obj/color changes. added x/wayland feature splits, WITH-TCP-CLIENT and WITH-UDP-CLIENT impl (no tests)
1 ;;; net/tcp.lisp --- TCP utilities
2 
3 ;;
4 
5 ;;; Code:
6 (in-package :net/tcp)
7 
8 (defun tcp-server (port)
9  (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
10  (socket-bind s #(0 0 0 0) port)
11  (loop
12  (multiple-value-bind (buf len addr port) (socket-receive s nil 500)
13  (format t "Received ~A bytes from ~A:~A - ~A ~%"
14  len addr port (subseq buf 0 (min 10 len)))))))
15 
16 (defvar *tcp-ping-size* 512)
17 
18 (defun tcp-ping-server (port &key (count 16))
19  (let ((s (make-instance 'inet-socket :type :stream :protocol :tcp)))
20  (socket-bind s #(0 0 0 0) port)
21  (loop for i from 0 upto count
22  do (multiple-value-bind (buf len address port) (socket-receive s nil *tcp-ping-size*)
23  (format t "(~A) Received ~A bytes from ~A:~A - ~A ~%"
24  i len address port (subseq buf 0 (min 10 len))))
25  finally (socket-close s))))
26 
27 (defmacro with-tcp-client ((socket-var &key (addr #(0 0 0 0)) (port 0) peer) &body body)
28  `(let ((,socket-var (make-instance 'inet-socket :type :stream :protocol :tcp)))
29  (unwind-protect
30  (progn
31  (socket-bind ,socket-var ,addr ,port)
32  ,(when peer `(apply #'socket-connect ,socket-var ,peer))
33  ,@body)
34  (socket-close ,socket-var))))