1 ;;; connection.el -- handling a tcp based connection
3 ;; Author: Torsten Hilbrich <dictionary@myrkr.in-berlin.de>
5 ;; $Id: connection.el,v 1.9 2001-11-24 12:18:26 torsten Exp $
7 ;; This file is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; This file is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
25 (defmacro connection-p (connection)
26 "Returns non-nil if `connection' is a connection object"
27 (list 'get connection ''connection))
29 (defmacro connection-read-point (connection)
30 "Return the read point of the connection object."
31 (list 'get connection ''connection-read-point))
33 (defmacro connection-process (connection)
34 "Return the process of the connection object."
35 (list 'get connection ''connection-process))
37 (defmacro connection-buffer (connection)
38 "Return the buffer of the connection object."
39 (list 'get connection ''connection-buffer))
41 (defmacro connection-set-read-point (connection point)
42 "Set the read-point for `connection' to `point'."
43 (list 'put connection ''connection-read-point point))
45 (defmacro connection-set-process (connection process)
46 "Set the process for `connection' to `process'."
47 (list 'put connection ''connection-process process))
49 (defmacro connection-set-buffer (connection buffer)
50 "Set the buffer for `connection' to `buffer'."
51 (list 'put connection ''connection-buffer buffer))
53 (defun connection-create-data (buffer process point)
54 "Create a new connection data based on `buffer', `process', and `point'."
55 (let ((connection (make-symbol "connection")))
56 (put connection 'connection t)
57 (connection-set-read-point connection point)
58 (connection-set-process connection process)
59 (connection-set-buffer connection buffer)
62 (defun connection-open (server port)
63 "Open a connection to `server' and `port'.
64 A data structure identifing the connection is returned"
66 (let ((process-buffer (generate-new-buffer (format " connection to %s:%s"
71 (set-buffer process-buffer)
72 (setq process (open-network-stream "connection" process-buffer
74 (connection-create-data process-buffer process (point-min)))))
76 (defun connection-status (connection)
77 "Return the status of the connection.
78 Possible return values are the symbols:
79 nil: argument is no connection object
80 'none: argument has no connection
81 'up: connection is open and buffer is existing
82 'down: connection is closed
83 'alone: connection is not associated with a buffer"
84 (if (connection-p connection)
85 (let ((process (connection-process connection))
86 (buffer (connection-buffer connection)))
89 (if (not (buffer-live-p buffer))
91 (if (not (eq (process-status process) 'open))
96 (defun connection-close (connection)
97 "Force closing of the connection."
98 (if (connection-p connection)
100 (let ((buffer (connection-buffer connection))
101 (process (connection-process connection)))
103 (delete-process process))
105 (kill-buffer buffer))
107 (connection-set-process connection nil)
108 (connection-set-buffer connection nil)))))
110 (defun connection-send (connection data)
111 "Send `data' to the process."
112 (unless (eq (connection-status connection) 'up)
113 (error "Connection is not up"))
115 (set-buffer (connection-buffer connection))
116 (goto-char (point-max))
117 (connection-set-read-point connection (point))
118 (process-send-string (connection-process connection) data)))
120 (defun connection-send-crlf (connection data)
121 "Send `data' together with CRLF to the process."
122 (connection-send connection (concat data "\r\n")))
124 (defun connection-read (connection delimiter)
125 "Read data until `delimiter' is found inside the buffer."
126 (unless (eq (connection-status connection) 'up)
127 (error "Connection is not up"))
128 (let ((case-fold-search nil)
131 (set-buffer (connection-buffer connection))
132 (goto-char (connection-read-point connection))
133 ;; Wait until there is enough data
134 (while (not (search-forward-regexp delimiter nil t))
135 (accept-process-output (connection-process connection) 3)
136 (goto-char (connection-read-point connection)))
137 (setq match-end (point))
139 (let ((result (buffer-substring (connection-read-point connection)
141 (connection-set-read-point connection match-end)
144 (defun connection-read-crlf (connection)
145 "Read until a line is completedx with CRLF"
146 (connection-read connection "\015?\012"))
148 (defun connection-read-to-point (connection)
149 "Read until a line is consisting of a single point"
150 (connection-read connection "\015?\012[.]\015?\012"))
152 (provide 'connection)