Abstract socket-verification

This commit is contained in:
Jaidyn Lev 2019-01-07 00:08:44 -06:00
parent 9696ecb825
commit 1b7120486a
2 changed files with 21 additions and 19 deletions

View File

@ -11,7 +11,6 @@
(let* ((client-id (socket-to-client socket))
(input-stack (client-data-get client-id "input")))
(client-data-set
client-id "input"
(concatenate 'list input-stack
@ -24,7 +23,6 @@
(let ((sstream (usocket:socket-stream socket))
(i 0))
(loop
:while (< i (length bytes))
:do
@ -43,11 +41,12 @@
(socket-write-bytes
socket
(babel:string-to-octets
(if line-break
(format nil "~A~%" string)
string)
:encoding :utf-8)))
(ignore-errors
(babel:string-to-octets
(if line-break
(format nil "~A~%" string)
string)
:encoding :utf-8))))
;; STRING [BOOLEAN] [SOCKET] --> NIL
@ -138,6 +137,14 @@
:initial-contents list
:element-type '(unsigned-byte 8)))
;; -------------------------------------
;; SOCKET --> BOOLEAN
(defun socket-connectp (socket)
"Return whether or not a socket is still connected."
(listen (usocket:socket-stream socket)))
;; -------------------------------------
;; MISC.
@ -159,13 +166,3 @@
(eq command-byte last-byte)))
;; STRING STRING
(defun strequal (str1 str2)
"Returns whether or not strings are equal-- in their UTF bytes."
(let ((str1-u
(delete 0 (tu8:string-to-utf-8-bytes str1 :encoding :utf-8)))
(str2-u
(delete 0 (tu8:string-to-utf-8-bytes str2 :encoding :utf-8))))
(equalp str1-u str2-u)))

View File

@ -30,7 +30,7 @@
;; STRING NUMBER CHARACTER FUNCTION-NAME FUNCTION-NAME FUNCTION-NAME --> NIL
(defun server
(host port connecting disconnecting input-handler
&key (command-byte 10) (halting 'halt-ex))
&key (command-byte 10) (halting 'halt-ex) (init 'blank))
"Runs the basic server on `host`:`port`, running `connecting` when a new
client connects, `disconnecting` when one disconnects, and `input-handler`
@ -52,6 +52,8 @@
(reset-globals)
(setq *socket-list* (list master-socket))
(funcall init)
(unwind-protect
(loop
(loop
@ -72,7 +74,7 @@
;; ...if functioning old connection...
((listen (usocket:socket-stream socket))
((socket-connectp socket)
(progn (socket-read socket)
;; check if command is complete-- if so, use user-provided
;; input-handler.
@ -128,3 +130,6 @@
(server host port connecting disconnecting input-handler
:command-byte command-byte
:halting halting))
(defun blank ())