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)
|
||||
"Example input-handler. Reverses user input and sends it back at them!"
|
||||
|
||||
(if input-string
|
||||
(if (< 0 (length input-string))
|
||||
(let* ((reversed-input
|
||||
(nih:char-string (reverse (nih:char-list input-string))))
|
||||
|
||||
|
|
29
src/io.lisp
29
src/io.lisp
|
@ -43,19 +43,19 @@
|
|||
|
||||
(socket-write-bytes
|
||||
socket
|
||||
(tu8:string-to-utf-8-bytes
|
||||
(babel:string-to-octets
|
||||
(if line-break
|
||||
(format nil "~A~%" string)
|
||||
string))))
|
||||
string)
|
||||
:encoding :utf-8)))
|
||||
|
||||
|
||||
;; STRING [BOOLEAN] [SOCKET] --> NIL
|
||||
(defun socket-broadcast (string &optional (line-break nil) (exception nil))
|
||||
"Writes a `string` to all client sockets (aside from an `exception`--
|
||||
w/o line-break, by default."
|
||||
"Writes a `string` to all client sockets (aside from an `exception`)."
|
||||
|
||||
(client-broadcast string line-break
|
||||
(ignore-errors (socket-to-client exception))))
|
||||
(socket-to-client exception)))
|
||||
|
||||
|
||||
;; SOCKET --> NIL
|
||||
|
@ -110,8 +110,7 @@
|
|||
|
||||
;; STRING [BOOLEAN] [SOCKET] --> NIL
|
||||
(defun client-broadcast (string &optional (line-break nil) (exception nil))
|
||||
"Writes a `string` to all client sockets (aside from an `exception`--
|
||||
w/o line-break, by default."
|
||||
"Writes a `string` to all client sockets (aside from an `exception`-- w/o line-break, by default."
|
||||
|
||||
(mapcar
|
||||
(lambda (client)
|
||||
|
@ -125,12 +124,20 @@
|
|||
"Get input from a client as a string."
|
||||
|
||||
(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)
|
||||
(make-array (list (length list))
|
||||
:initial-contents list
|
||||
:element-type '(unsigned-byte 8)))
|
||||
|
||||
;; -------------------------------------
|
||||
;; MISC.
|
||||
|
||||
|
@ -157,8 +164,8 @@
|
|||
"Returns whether or not strings are equal-- in their UTF bytes."
|
||||
|
||||
(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
|
||||
(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)))
|
||||
|
|
|
@ -2,9 +2,13 @@
|
|||
|
||||
|
||||
;; 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."
|
||||
(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 ())
|
||||
|
@ -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
|
||||
(defun print-bytes (bytes)
|
||||
"Print a list of (UTF-8) bytes as a string to stdout."
|
||||
|
||||
(if bytes
|
||||
(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
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
(let* ((master-socket
|
||||
(usocket:socket-listen host port
|
||||
:reuse-address 'T
|
||||
:element-type 'unsigned-byte )))
|
||||
:element-type '(unsigned-byte 8))))
|
||||
(reset-globals)
|
||||
(setq *socket-list* (list master-socket))
|
||||
|
||||
|
@ -79,7 +79,6 @@
|
|||
(let* ((client-id (socket-to-client socket))
|
||||
(client-bytes (client-data-get client-id "input"))
|
||||
(client-input (client-input-string client-id)))
|
||||
(journal client-input "Client Input")
|
||||
|
||||
;; if reached *command-byte*, handle and flush input
|
||||
(if (commandp client-bytes command-byte)
|
||||
|
|
Ŝarĝante…
Reference in New Issue