-
Notifications
You must be signed in to change notification settings - Fork 5
/
shampoo-networking.el
63 lines (51 loc) · 1.84 KB
/
shampoo-networking.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
;;; shampoo-networking.el --- Shampoo network layer
;;
;; Copyright (C) 2010 - 2012 Dmitry Matveev <[email protected]>
;;
;; This software is released under terms of the MIT license,
;; please refer to the LICENSE file for details.
(eval-when-compile (require 'cl))
(require 'shampoo-auth)
(defstruct shampoo-connection
process)
(defun shampoo-net-sentinel (connection user-down-fcn)
(lexical-let ((c connection)
(f user-down-fcn))
(lambda (process event)
(when (not (shampoo-net-is-alive c))
(funcall f)))))
(defun shampoo-net-receiver (connection user-recv-fcn)
(lexical-let ((f user-recv-fcn))
(lambda (process str)
(funcall f str))))
(defun* shampoo-connection-setup
(connection &key on-receive on-shutdown)
(let ((p (shampoo-connection-process connection)))
(when on-receive
(set-process-filter
p
(shampoo-net-receiver connection on-receive)))
(when on-shutdown
(set-process-sentinel
p
(shampoo-net-sentinel connection on-shutdown)))))
(defun shampoo-net-connect (connect-info)
(make-shampoo-connection
:process (open-network-stream "shampoo" nil
(shampoo-connect-info-host connect-info)
(shampoo-connect-info-port connect-info))))
(defun shampoo-net-is-alive (connection)
(let ((proc (shampoo-connection-process connection)))
(not (eq 'closed (process-status proc)))))
(defun shampoo-net-mkmsg (str)
(format "Content-Length: %d\r\n\r\n%s" (length str) str))
(defun shampoo-net-send (connection msg)
; (message "Sending: %s" msg)
(process-send-string
(shampoo-connection-process connection)
(shampoo-net-mkmsg msg)))
(defun shampoo-net-disconnect (connection)
(when connection
(delete-process (shampoo-connection-process connection))))
(provide 'shampoo-networking)
;;; shampoo-networking.el ends here.