make-network-process是否在Windows中运行?

时间:2011-07-09 06:04:57

标签: emacs network-programming elisp

我在调试使用emacs lisp充当简单tcp客户端的程序时遇到问题。我把它剥离到最低限度,在这里提出一个问题。此代码在Max OSX和Linux Centos机器上运行良好,但在Windows(所有使用Gnu emacs最新版本)上,永远不会调用sentinel和filter进程的回调函数。阻塞连接工作,而非阻塞连接说不支持在窗口,我可以忍受。服务器显示客户端连接和断开连接正常;但似乎没有输出到服务器。最终服务器断开连接。

有什么想法吗?

(defvar *url-process* nil)
(defvar *url-state* nil)
(defvar *url-response* nil)

(defun url-buffer-message(process message)
  "print a message to the url process buffer"
  (save-excursion 
    (set-buffer (process-buffer process))
    (insert message)))

(defun url-sentinel(process event)
  "sentinal function for url network process which monitors for events"
  (url-buffer-message process (format "sentinel event %s" event))
  (cond 
   ((string-match "open" event)
    (setq *url-state* 'open))))

(defun url-filter(process string)
  "filter function for url network process, which receives output"
  (url-buffer-message process (format "filter %s" string)) ; debug
  (setq *url-response* string))

(defun url-new-process(p)
  (url-delete-process)
  (setf *url-process* p))

(defun url-delete-process()
  (when *url-process*
    (delete-process *url-process*)
    (setq *url-state* 'closed)))

(defun url-hai(host port blocking)
  (setq *url-state* 'opening)
  (url-new-process
   (make-network-process :name "url"
             :host host
             :service port
             :nowait (not blocking)
             :filter #'url-filter
             :sentinel #'url-sentinel
             :buffer (get-buffer-create "*url*"))))

(defun url-kthxbye()
  (url-delete-process))

(defun url-ping()
  (if (and *url-process* (eq *url-state* 'open))
      (process-send-string *url-process* "PING\r\n")))

(defun url-get()
  (if (and *url-process* (eq *url-state* 'open))
      (process-send-string *url-process* (format "GET /\r\n"))))

(defun url-info()
  (if (and *url-process* (eq *url-state* 'open))
      (process-send-string *url-process* "INFO\r\n")))

; usage
; (url-hai "127.0.0.1" 80 t)
; (url-hai "127.0.0.1" 80 nil)
; (url-get)
; (url-kthxbye)

1 个答案:

答案 0 :(得分:3)

这里的问题与make-network-process

有关

当调用此函数时:nowait设置为nil以使调用同步/阻塞sentinel函数永远不会调用状态'open'。可悲的是,我依赖于这种行为,在我发送数据的函数中,我检查连接是否已打开。

一旦我发现修复很明显;如果make-network-process函数成功,则将状态设置为'open,这是一个阻塞调用。下面的工作代码:

(defun url-delete-process()
  (when *url-process*
    (delete-process *url-process*)
    (setq *url-state* 'closed)))

(defun url-hai(host port blocking)
  (setq *url-state* 'opening)
  (url-delete-process)
  (let ((p 
     (make-network-process :name "url"
                   :host host
                   :service port
                   :nowait (not blocking)
                   :filter #'url-filter
                   :sentinel #'url-sentinel
                   :buffer (get-buffer-create "*url*"))))
    (if p 
    (progn
      (if blocking 
          (setf *url-state* 'open))
      (setf *url-process* p)))))

(defun url-kthxbye()
  (url-delete-process))