1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- Device common functions.
6 (in-package :io.zeta-streams
)
8 ;;;-----------------------------------------------------------------------------
9 ;;; Device Classes and Types
10 ;;;-----------------------------------------------------------------------------
13 ((input-handle :initarg
:input-handle
:accessor input-handle-of
)
14 (output-handle :initarg
:output-handle
:accessor output-handle-of
)))
16 (defclass single-channel-device
(device) ())
18 (defclass dual-channel-device
(device) ())
20 (defclass direct-device
(single-channel-device) ())
22 (defclass memory-buffer-device
(direct-device) ())
24 (defclass socket-device
(dual-channel-device)
25 ((domain :initarg
:domain
)
27 (protocol :initarg
:protocol
)))
29 (deftype device-timeout
()
32 (deftype stream-position
() '(unsigned-byte 64))
35 ;;;-----------------------------------------------------------------------------
37 ;;;-----------------------------------------------------------------------------
39 (defgeneric device-open
(device &rest initargs
))
41 (defgeneric device-close
(device &optional abort
))
43 (defgeneric device-read
(device vector start end
&optional timeout
))
45 (defgeneric read-octets
/non-blocking
(device vector start end
))
47 (defgeneric read-octets
/timeout
(device vector start end timeout
))
49 (defgeneric device-write
(device vector start end
&optional timeout
))
51 (defgeneric write-octets
/non-blocking
(device vector start end
))
53 (defgeneric write-octets
/timeout
(device vector start end timeout
))
55 (defgeneric device-position
(device))
57 (defgeneric (setf device-position
) (position device
&rest args
))
59 (defgeneric device-length
(device))
61 (defgeneric device-poll-input
(device &optional timeout
))
63 (defgeneric device-poll-output
(device &optional timeout
))
66 ;;;-----------------------------------------------------------------------------
68 ;;;-----------------------------------------------------------------------------
70 (defmacro with-device
((name) &body body
)
71 `(let ((*device
* ,name
))
72 (declare (special *device
*))
76 ;;;-----------------------------------------------------------------------------
77 ;;; Default no-op methods
78 ;;;-----------------------------------------------------------------------------
80 (defmethod device-position ((device device
))
83 (defmethod (setf device-position
) (position (device device
) &rest args
)
84 (declare (ignore position args
))
87 (defmethod device-length ((device device
))
91 ;;;-----------------------------------------------------------------------------
92 ;;; Default DEVICE-READ
93 ;;;-----------------------------------------------------------------------------
95 (defmethod device-read :around
((device device
) vector start end
&optional timeout
)
96 (declare (ignore timeout
))
97 (if (= start end
) 0 (call-next-method)))
99 (defmethod device-read ((device device
) vector start end
&optional timeout
)
100 (if (and timeout
(zerop timeout
))
101 (read-octets/non-blocking device vector start end
)
102 (read-octets/timeout device vector start end timeout
)))
105 ;;;-----------------------------------------------------------------------------
106 ;;; Default DEVICE-WRITE
107 ;;;-----------------------------------------------------------------------------
109 (defmethod device-write :around
((device device
) vector start end
&optional timeout
)
110 (declare (ignore timeout
))
111 (if (= start end
) 0 (call-next-method)))
113 (defmethod device-write ((device device
) vector start end
&optional timeout
)
114 (if (and timeout
(zerop timeout
))
115 (write-octets/non-blocking device vector start end
)
116 (write-octets/timeout device vector start end timeout
)))