Less verbosity, tweaks
This commit is contained in:
parent
a7f5301e87
commit
9696ecb825
|
@ -21,7 +21,7 @@
|
||||||
(defun input-handle-ex (socket client-id input-string)
|
(defun input-handle-ex (socket client-id input-string)
|
||||||
"Example input-handler. Reverses user input and sends it back at them!"
|
"Example input-handler. Reverses user input and sends it back at them!"
|
||||||
|
|
||||||
(if input-string
|
(if (< 0 (length input-string))
|
||||||
(let* ((reversed-input
|
(let* ((reversed-input
|
||||||
(nih:char-string (reverse (nih:char-list input-string))))
|
(nih:char-string (reverse (nih:char-list input-string))))
|
||||||
|
|
||||||
|
|
143
src/io.lisp
143
src/io.lisp
|
@ -43,122 +43,129 @@
|
||||||
|
|
||||||
(socket-write-bytes
|
(socket-write-bytes
|
||||||
socket
|
socket
|
||||||
(tu8:string-to-utf-8-bytes
|
(babel:string-to-octets
|
||||||
(if line-break
|
(if line-break
|
||||||
(format nil "~A~%" string)
|
(format nil "~A~%" string)
|
||||||
string))))
|
string)
|
||||||
|
:encoding :utf-8)))
|
||||||
|
|
||||||
|
|
||||||
;; STRING [BOOLEAN] [SOCKET] --> NIL
|
;; STRING [BOOLEAN] [SOCKET] --> NIL
|
||||||
(defun socket-broadcast (string &optional (line-break nil) (exception nil))
|
(defun socket-broadcast (string &optional (line-break nil) (exception nil))
|
||||||
"Writes a `string` to all client sockets (aside from an `exception`--
|
"Writes a `string` to all client sockets (aside from an `exception`)."
|
||||||
w/o line-break, by default."
|
|
||||||
|
|
||||||
(client-broadcast string line-break
|
(client-broadcast string line-break
|
||||||
(ignore-errors (socket-to-client exception))))
|
(socket-to-client exception)))
|
||||||
|
|
||||||
|
|
||||||
;; SOCKET --> NIL
|
;; SOCKET --> NIL
|
||||||
(defun socket-input-flush (socket)
|
(defun socket-input-flush (socket)
|
||||||
"Clean all input from a socket."
|
"Clean all input from a socket."
|
||||||
|
|
||||||
(let ((client-id (socket-to-client socket)))
|
(let ((client-id (socket-to-client socket)))
|
||||||
(client-data-set client-id "input" '())))
|
(client-data-set client-id "input" '())))
|
||||||
|
|
||||||
|
|
||||||
;; SOCKET --> STRING
|
;; SOCKET --> STRING
|
||||||
(defun socket-input-string (socket)
|
(defun socket-input-string (socket)
|
||||||
"Get input from a socket as a string."
|
"Get input from a socket as a string."
|
||||||
|
|
||||||
(client-input-string (socket-to-client socket)))
|
(client-input-string (socket-to-client socket)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; -------------------------------------
|
;; -------------------------------------
|
||||||
;; CLIENT I/O
|
;; CLIENT I/O
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; NUMBER --> NIL
|
;; NUMBER --> NIL
|
||||||
(defun client-read (client)
|
(defun client-read (client)
|
||||||
"Read new input from a client to their stack of input bytes."
|
"Read new input from a client to their stack of input bytes."
|
||||||
|
|
||||||
(socket-read (client-to-socket client)))
|
(socket-read (client-to-socket client)))
|
||||||
|
|
||||||
|
|
||||||
;; NUMBER LIST --> NIL
|
;; NUMBER LIST --> NIL
|
||||||
(defun client-write-bytes (client bytes)
|
(defun client-write-bytes (client bytes)
|
||||||
"Write bytes to a client's socket."
|
"Write bytes to a client's socket."
|
||||||
(format t "CLIENT-TO-SOCKET: " (client-to-socket client))
|
(format t "CLIENT-TO-SOCKET: " (client-to-socket client))
|
||||||
|
|
||||||
(socket-write-bytes (client-to-socket client) bytes))
|
(socket-write-bytes (client-to-socket client) bytes))
|
||||||
|
|
||||||
|
|
||||||
;; NUMBER STRING [BOOLEAN] --> NIL
|
;; NUMBER STRING [BOOLEAN] --> NIL
|
||||||
(defun client-write (client string &optional (line-break nil))
|
(defun client-write (client string &optional (line-break nil))
|
||||||
"Writes a string to a client's socket-- w/o line-break, default."
|
"Writes a string to a client's socket-- w/o line-break, default."
|
||||||
|
|
||||||
(socket-write (client-to-socket client) string line-break))
|
(socket-write (client-to-socket client) string line-break))
|
||||||
|
|
||||||
|
|
||||||
;; NUMBER --> NIL
|
;; NUMBER --> NIL
|
||||||
(defun client-input-flush (client)
|
(defun client-input-flush (client)
|
||||||
"Clean up input from a client."
|
"Clean up input from a client."
|
||||||
|
|
||||||
(socket-input-flush (client-to-socket client)))
|
(socket-input-flush (client-to-socket client)))
|
||||||
|
|
||||||
|
|
||||||
;; STRING [BOOLEAN] [SOCKET] --> NIL
|
;; STRING [BOOLEAN] [SOCKET] --> NIL
|
||||||
(defun client-broadcast (string &optional (line-break nil) (exception nil))
|
(defun client-broadcast (string &optional (line-break nil) (exception nil))
|
||||||
"Writes a `string` to all client sockets (aside from an `exception`--
|
"Writes a `string` to all client sockets (aside from an `exception`-- w/o line-break, by default."
|
||||||
w/o line-break, by default."
|
|
||||||
|
|
||||||
(mapcar
|
(mapcar
|
||||||
(lambda (client)
|
(lambda (client)
|
||||||
(if (not (eq client exception))
|
(if (not (eq client exception))
|
||||||
(client-write client string line-break)))
|
(client-write client string line-break)))
|
||||||
*client-list*))
|
*client-list*))
|
||||||
|
|
||||||
|
|
||||||
;; NUMBER --> STRING
|
;; NUMBER --> STRING
|
||||||
(defun client-input-string (client)
|
(defun client-input-string (client)
|
||||||
"Get input from a client as a string."
|
"Get input from a client as a string."
|
||||||
|
|
||||||
(let* ((input-bytes (client-data-get client "input"))
|
(let* ((input-bytes (client-data-get client "input"))
|
||||||
(sanitized-bytes (remove-newline-bytes input-bytes)))
|
(sanitized-bytes (remove-newline-bytes input-bytes))
|
||||||
|
(byte-vector (list-to-byte-vector sanitized-bytes)))
|
||||||
|
|
||||||
(ignore-errors (trivial-utf-8:utf-8-bytes-to-string sanitized-bytes))))
|
(if sanitized-bytes
|
||||||
|
(ignore-errors (babel:octets-to-string byte-vector :encoding :utf-8))
|
||||||
|
"")))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; -------------------------------------
|
(defun list-to-byte-vector (list)
|
||||||
;; MISC.
|
(make-array (list (length list))
|
||||||
|
:initial-contents list
|
||||||
|
:element-type '(unsigned-byte 8)))
|
||||||
|
|
||||||
|
;; -------------------------------------
|
||||||
|
;; MISC.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; LIST --> LIST
|
;; LIST --> LIST
|
||||||
(defun remove-newline-bytes (bytes)
|
(defun remove-newline-bytes (bytes)
|
||||||
"Remove undesired bytes-- null, LF, CR, etc, from a list of bytes."
|
"Remove undesired bytes-- null, LF, CR, etc, from a list of bytes."
|
||||||
|
|
||||||
(remove 0 (remove 10 (remove 13 bytes))))
|
(remove 0 (remove 10 (remove 13 bytes))))
|
||||||
|
|
||||||
|
|
||||||
;; SOCKET
|
;; SOCKET
|
||||||
(defun commandp (byte-list command-byte)
|
(defun commandp (byte-list command-byte)
|
||||||
"Returns whether or not a command is complete, judging on it's bytes."
|
"Returns whether or not a command is complete, judging on it's bytes."
|
||||||
|
|
||||||
(let* ((last-byte (car (last byte-list))))
|
(let* ((last-byte (car (last byte-list))))
|
||||||
|
|
||||||
(eq command-byte last-byte)))
|
(eq command-byte last-byte)))
|
||||||
|
|
||||||
|
|
||||||
;; STRING STRING
|
;; STRING STRING
|
||||||
(defun strequal (str1 str2)
|
(defun strequal (str1 str2)
|
||||||
"Returns whether or not strings are equal-- in their UTF bytes."
|
"Returns whether or not strings are equal-- in their UTF bytes."
|
||||||
|
|
||||||
(let ((str1-u
|
(let ((str1-u
|
||||||
(delete 0 (tu8:string-to-utf-8-bytes str1)))
|
(delete 0 (tu8:string-to-utf-8-bytes str1 :encoding :utf-8)))
|
||||||
(str2-u
|
(str2-u
|
||||||
(delete 0 (tu8:string-to-utf-8-bytes str2))))
|
(delete 0 (tu8:string-to-utf-8-bytes str2 :encoding :utf-8))))
|
||||||
|
|
||||||
(equalp str1-u str2-u)))
|
(equalp str1-u str2-u)))
|
||||||
|
|
|
@ -2,9 +2,13 @@
|
||||||
|
|
||||||
|
|
||||||
;; DATA [STRING] --> STRING
|
;; DATA [STRING] --> STRING
|
||||||
(defun journal (data &optional (name "unnamed"))
|
(defun journal (data &optional (name "unnamed") (second-name "-"))
|
||||||
"Print out a piece of data for logging on stdout."
|
"Print out a piece of data for logging on stdout."
|
||||||
(format t "~A | ~A~%" (force-string-length name 10) data))
|
(format t "~A | ~A | ~A | ~A~%"
|
||||||
|
(string-date (get-universal-time))
|
||||||
|
(force-string-length name 10)
|
||||||
|
(force-string-length second-name 10)
|
||||||
|
data))
|
||||||
|
|
||||||
|
|
||||||
(defun standard-journaling ())
|
(defun standard-journaling ())
|
||||||
|
@ -13,13 +17,29 @@
|
||||||
;; -------------------------------------
|
;; -------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
(defun string-date (universal-time)
|
||||||
|
(multiple-value-bind (second minute hour day month year)
|
||||||
|
(decode-universal-time universal-time)
|
||||||
|
|
||||||
|
(nih:string-combine
|
||||||
|
(nih:string-combine year (make-digits month 2) (make-digits day 2)
|
||||||
|
:seperator "-")
|
||||||
|
" "
|
||||||
|
(nih:string-combine (make-digits hour 2) (make-digits minute 2)
|
||||||
|
(make-digits second 2) :seperator ":"))))
|
||||||
|
|
||||||
|
|
||||||
|
(defun make-digits (string number)
|
||||||
|
(nih:min-string-length string number :prefix "0"))
|
||||||
|
|
||||||
|
|
||||||
;; LIST --> STRING
|
;; LIST --> STRING
|
||||||
(defun print-bytes (bytes)
|
(defun print-bytes (bytes)
|
||||||
"Print a list of (UTF-8) bytes as a string to stdout."
|
"Print a list of (UTF-8) bytes as a string to stdout."
|
||||||
|
|
||||||
(if bytes
|
(if bytes
|
||||||
(format t "~A"
|
(format t "~A"
|
||||||
(ignore-errors (tu8:utf-8-bytes-to-string bytes)))))
|
(ignore-errors (babel:octets-to-string bytes :encoding :utf-8)))))
|
||||||
|
|
||||||
|
|
||||||
;; STRING NUMBER [STRING} --> STRING
|
;; STRING NUMBER [STRING} --> STRING
|
||||||
|
|
|
@ -48,7 +48,7 @@
|
||||||
(let* ((master-socket
|
(let* ((master-socket
|
||||||
(usocket:socket-listen host port
|
(usocket:socket-listen host port
|
||||||
:reuse-address 'T
|
:reuse-address 'T
|
||||||
:element-type 'unsigned-byte )))
|
:element-type '(unsigned-byte 8))))
|
||||||
(reset-globals)
|
(reset-globals)
|
||||||
(setq *socket-list* (list master-socket))
|
(setq *socket-list* (list master-socket))
|
||||||
|
|
||||||
|
@ -79,7 +79,6 @@
|
||||||
(let* ((client-id (socket-to-client socket))
|
(let* ((client-id (socket-to-client socket))
|
||||||
(client-bytes (client-data-get client-id "input"))
|
(client-bytes (client-data-get client-id "input"))
|
||||||
(client-input (client-input-string client-id)))
|
(client-input (client-input-string client-id)))
|
||||||
(journal client-input "Client Input")
|
|
||||||
|
|
||||||
;; if reached *command-byte*, handle and flush input
|
;; if reached *command-byte*, handle and flush input
|
||||||
(if (commandp client-bytes command-byte)
|
(if (commandp client-bytes command-byte)
|
||||||
|
|
Ŝarĝante…
Reference in New Issue