如果没有人监听指定的端口,make-network-process将永久挂起

时间:2012-11-14 17:19:56

标签: sockets emacs tcp elisp

有没有办法缓解这种行为?如果它返回nil或抛出错误将是最好的。

即。假设我正在尝试连接到localhost上的未绑定套接字 - 在这种情况下make-network-process阻塞,即使之后有人绑定套接字,它也没有意识到它发生了,因此,它基本上被卡住了。

1 个答案:

答案 0 :(得分:0)

好的,必须更加注意文档。有一个:nowait键参数,可以防止这种行为。下面是一个处理它的示例代码:

(defun haxe-network-process-sentinel (process input)
  (message "haxe-network-process-sentinel <%s>" input)
  (when (stringp input)
      (cond
       ((or
         (haxe-string-starts-with input "failed with code")
         (haxe-string-starts-with input "connection broken by"))
        (setq haxe-network-status 'error))
       ((string= input "open")
        (setq haxe-network-status 'open))
       (t (setq haxe-network-status 'open)
        (haxe-append-server-response input)))))

;;;###autoload
(defun haxe-connect-to-compiler-server ()
  "Starts HaXe compilations server and connects to it.
This function is bound to \\[haxe-connect-to-compiler-server]"
  (interactive)
  (let ((old-proc (get-process haxe-compiler-process)))
    (if (and old-proc (equal (process-status old-proc) 'open))
        (setq haxe-network-process old-proc)
      (haxe-log 3 "Trying to connect to HaXe compiler on %s:%s"
                haxe-server-host haxe-server-port)
      (while (not (eql haxe-network-status 'open))
        (setq haxe-network-process
              (make-network-process
               :name haxe-compiler-process
               :family 'ipv4
               :host haxe-server-host
               :nowait t
               :service haxe-server-port
               ;; :buffer haxe-network-process-buffer
               :sentinel #'haxe-network-process-sentinel
               :filter #'haxe-listen-filter))
        (sleep-for 1))
      (haxe-log 3 "Connected to HaXe compiler"))))