From 1b7120486a4e4495198d48f6598ed06b7db2f46a Mon Sep 17 00:00:00 2001 From: Jaidyn Lev Date: Mon, 7 Jan 2019 00:08:44 -0600 Subject: [PATCH] Abstract socket-verification --- src/io.lisp | 31 ++++++++++++++----------------- src/server.lisp | 9 +++++++-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/io.lisp b/src/io.lisp index 46c7de1..3dabda1 100644 --- a/src/io.lisp +++ b/src/io.lisp @@ -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))) diff --git a/src/server.lisp b/src/server.lisp index 7dda08b..1349dfa 100644 --- a/src/server.lisp +++ b/src/server.lisp @@ -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 ())