具有挂断条件的iolib在读取线时未被捕获

时间:2013-01-21 13:23:11

标签: lisp common-lisp

我是普通lisp的新手,当我使用iolib编写服务器时,我发现当telnet到服务器,然后断开telnet,服务器抛出

未知类型说明符:HANGUP

当我打印回溯时,我发现它在make-server-line-echoer中的read-line执行时出现。我在那里使用handler-case,但不行。有什么帮助吗?

这是我的代码:

(ql:quickload 'iolib)

(defpackage :com.server.test
  (:use :sockets :iomux :common-lisp))
(in-package :com.server.test)

(defvar *server-event-base* nil)

(defun make-server-line-echoer (socket)
  (lambda (fd event exception)
    (handler-case
        (progn
          (let ((line (read-line socket)))
            (format t "Read ~A~%" line)
            (format socket "~A~%" line)
            (finish-output socket)))
      (socket-connection-reset-error ()
        (format t "Connection reset by peer~%")
        (close socket))
      (hangup ()
        (format t "Client close connection on write~%")
        (close socket))
      (end-of-file ()
        (format t "Client closed connection on read~%")
        (close socket)))))

(defun make-server-listener-handler (socket)
  (lambda (fd event exception)
    (let ((client (accept-connection socket :wait t)))
      (when client
        (multiple-value-bind (who port)
            (remote-name client)
          (format t "Accept a client from ~A:~A~%" who port)
          (set-io-handler *server-event-base*
                          (socket-os-fd client)
                          :read
                          (make-server-line-echoer
                           client)))))))

(defun run-server (&key port)
  (setf *server-event-base* (make-instance 'event-base))
  (let ((listener (make-socket :connect :passive
                               :address-family :internet
                               :type :stream
                               :ipv6 nil
                               :external-format '(:utf-8 :eol-style :crlf))))
    (bind-address listener +ipv4-unspecified+ :port port :reuse-addr t)
    (listen-on listener :backlog 5)
    (format t "Listening on socket bound to: ~A:~A~%"
            (local-host listener)
            (local-port listener))
    (set-io-handler *server-event-base*
                    (socket-os-fd listener)
                    :read
                    (make-server-listener-handler listener))
    (handler-case
        (event-dispatch *server-event-base*)
      (socket-connection-reset-error ()
        (format t "Connection reset by peer~%"))
      (hangup ()
        (format t "Client close connection on write~%"))
      (end-of-file ()
        (format t "Client closed connection on read~%")))
    (format t "Close connection now~%")))

(run-server :port 3000)

1 个答案:

答案 0 :(得分:2)

您必须use iolib.streams,因为hangup条件已在该套餐中定义。