Browse code

Abstract socket-verification

Jaidyn Lev authored on 2019-01-07 06:08:44
Showing 2 changed files
... ...
@@ -11,7 +11,6 @@
11 11
 
12 12
   (let* ((client-id   (socket-to-client socket))
13 13
 	 (input-stack (client-data-get client-id "input")))
14
-
15 14
     (client-data-set
16 15
       client-id "input"
17 16
       (concatenate 'list input-stack
... ...
@@ -24,7 +23,6 @@
24 23
 
25 24
   (let ((sstream (usocket:socket-stream socket))
26 25
 	(i 0))
27
-
28 26
     (loop
29 27
       :while (< i (length bytes))
30 28
       :do
... ...
@@ -43,11 +41,12 @@
43 41
 
44 42
   (socket-write-bytes
45 43
     socket
46
-    (babel:string-to-octets
47
-      (if line-break
48
-	(format nil "~A~%" string)
49
-	string)
50
-      :encoding :utf-8)))
44
+    (ignore-errors
45
+      (babel:string-to-octets
46
+	(if line-break
47
+	  (format nil "~A~%" string)
48
+	  string)
49
+	:encoding :utf-8))))
51 50
 
52 51
 
53 52
 ;; STRING [BOOLEAN] [SOCKET] --> NIL
... ...
@@ -138,6 +137,14 @@
138 137
 	      :initial-contents list
139 138
 	      :element-type '(unsigned-byte 8)))
140 139
 
140
+
141
+;; -------------------------------------
142
+
143
+;; SOCKET --> BOOLEAN
144
+(defun socket-connectp (socket)
145
+  "Return whether or not a socket is still connected."
146
+  (listen (usocket:socket-stream socket)))
147
+
141 148
 ;; -------------------------------------
142 149
 ;; MISC.
143 150
 
... ...
@@ -159,13 +166,3 @@
159 166
     (eq command-byte last-byte)))
160 167
 
161 168
 
162
-;; STRING STRING
163
-(defun strequal (str1 str2)
164
-  "Returns whether or not strings are equal-- in their UTF bytes."
165
-
166
-  (let ((str1-u
167
-	  (delete 0 (tu8:string-to-utf-8-bytes str1 :encoding :utf-8)))
168
-	(str2-u
169
-	  (delete 0 (tu8:string-to-utf-8-bytes str2 :encoding :utf-8))))
170
-
171
-    (equalp str1-u str2-u)))
... ...
@@ -30,7 +30,7 @@
30 30
 ;; STRING NUMBER CHARACTER FUNCTION-NAME FUNCTION-NAME FUNCTION-NAME --> NIL
31 31
 (defun server
32 32
   (host port connecting disconnecting input-handler
33
-	&key (command-byte 10) (halting 'halt-ex))
33
+	&key (command-byte 10) (halting 'halt-ex) (init 'blank))
34 34
 
35 35
   "Runs the basic server on `host`:`port`, running `connecting` when a new
36 36
   client connects, `disconnecting` when one disconnects, and `input-handler`
... ...
@@ -52,6 +52,8 @@
52 52
     (reset-globals)
53 53
     (setq *socket-list* (list master-socket))
54 54
 
55
+    (funcall init)
56
+
55 57
     (unwind-protect
56 58
       (loop 
57 59
 	(loop
... ...
@@ -72,7 +74,7 @@
72 74
 
73 75
 
74 76
 	    ;; ...if functioning old connection...
75
-	    ((listen (usocket:socket-stream socket))
77
+	    ((socket-connectp socket)
76 78
 	     (progn (socket-read socket)
77 79
 		    ;; check if command is complete-- if so, use user-provided
78 80
 		    ;; input-handler.
... ...
@@ -128,3 +130,6 @@
128 130
   (server host port connecting disconnecting input-handler
129 131
 	  :command-byte command-byte
130 132
 	  :halting halting))
133
+
134
+
135
+(defun blank ())