You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.5 KiB
40 lines
1.5 KiB
6 years ago
|
;; Simple sipc server
|
||
6 years ago
|
|
||
6 years ago
|
(let ((*standard-output* (make-broadcast-stream)))
|
||
|
(ql:quickload :cl-sipc))
|
||
6 years ago
|
|
||
|
(defparameter *socket-file* "sipc.socket")
|
||
6 years ago
|
(defparameter *respond* t) ;; should the server echo responses to client?
|
||
6 years ago
|
|
||
|
(when (probe-file *socket-file*)
|
||
|
(delete-file *socket-file*))
|
||
|
|
||
6 years ago
|
(defparameter *socket* (cl-sipc:bind *socket-file*)) ;;attempt to bind to this file
|
||
6 years ago
|
|
||
6 years ago
|
(when (not *socket*)
|
||
6 years ago
|
(format t "[e] binding failed ~a~%" *socket-file*) (quit))
|
||
6 years ago
|
|
||
6 years ago
|
(format t "[+] listening on ~a...~%" *socket-file*)
|
||
6 years ago
|
|
||
|
;;block until the listener is done
|
||
6 years ago
|
(let ((rc (cl-sipc:hook *socket*
|
||
6 years ago
|
#'(lambda (err) ;; Callback ran if there is an error
|
||
6 years ago
|
(format t "Error: ~a~%" err)
|
||
6 years ago
|
nil) ;;returning NIL to the listener stops
|
||
|
#'(lambda (type message) ;; Callback ran when a message is received
|
||
6 years ago
|
(when *respond*
|
||
|
(format t
|
||
|
" -> ~a~%"
|
||
6 years ago
|
(sipc:respond ;; send the response as a formatted string
|
||
6 years ago
|
(if (eql type :binary)
|
||
|
(format nil "~a" (sipc:pointer-to-array message))
|
||
|
(format nil "~a" message)))))
|
||
6 years ago
|
(if (eql type :binary)
|
||
|
(format t " <- (~a) ~a (size: ~a)~%" type (sipc:pointer-to-array message) (sipc:pointer-size message)) ;;print the binary message as an array of bytes, the type, & the size
|
||
|
(format t " <- (~a) ~a~%" type message)) ;;print the message & type
|
||
6 years ago
|
(not (eql :close type)))))) ;;returning NIL if the type is :CLOSE to stop the listener
|
||
6 years ago
|
(format t "[-] listen rc ~a~%" rc)
|
||
6 years ago
|
(cl-sipc:release *socket*)) ;;finally, release the socket
|
||
6 years ago
|
|
||
|
(quit)
|