有没有办法减轻这种行为?最好是返回 nil 或抛出错误。
即假设我正在尝试连接到本地主机上的未绑定套接字 - 在这种情况下make-network-process
会阻塞,即使之后有人绑定了套接字,它也没有意识到它发生了,所以它基本上被卡住了。
好的,必须更加注意文档。有一个: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"))))