Fix typo.
[iolib.git] / io.streams / classes.lisp
blob58684e77b160f08778815643d2c6e4b5d16a723c
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; classes.lisp --- fd-streams classes.
4 ;;;
5 ;;; Copyright (C) 2006-2008, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :io.streams)
26 ;;;; Types
28 (deftype ub8 () '(unsigned-byte 8))
29 (deftype ub16 () '(unsigned-byte 16))
30 (deftype ub32 () '(unsigned-byte 32))
31 (deftype sb8 () '(signed-byte 8))
32 (deftype sb16 () '(signed-byte 16))
33 (deftype sb32 () '(signed-byte 32))
35 (deftype ub8-sarray (&optional (size '*))
36 `(simple-array ub8 (,size)))
38 (deftype ub8-vector () '(vector ub8))
40 (deftype ub16-sarray (&optional (size '*))
41 `(simple-array ub16 (,size)))
43 ;;;; Socket Buffers
45 (deftype stream-buffer () 'foreign-pointer)
46 (deftype buffer-index () '(unsigned-byte 24))
48 (defstruct (iobuf (:constructor %make-iobuf ()))
49 (data (null-pointer) :type stream-buffer)
50 (size 0 :type buffer-index)
51 (start 0 :type buffer-index)
52 (end 0 :type buffer-index))
54 ;;;; File-Descriptor Mixins
56 (deftype stream-position () '(unsigned-byte 64))
58 (defun default-read-fn (fd buf nbytes)
59 (nix:read fd buf nbytes))
61 (defun default-write-fn (fd buf nbytes)
62 (nix:write fd buf nbytes))
64 (defclass dual-channel-fd-mixin ()
65 ((input-fd :initform nil :initarg :input-fd :accessor input-fd-of
66 :documentation "placeholder")
67 (read-fn :initform 'default-read-fn :initarg :read-fn :accessor read-fn-of)
68 (output-fd :initform nil :initarg :output-fd :accessor output-fd-of
69 :documentation "placeholder")
70 (write-fn :initform 'default-write-fn :initarg :write-fn :accessor write-fn-of))
71 (:documentation "placeholder"))
73 (defgeneric input-fd-non-blocking (socket)
74 (:documentation "placeholder"))
76 (defgeneric (setf input-fd-non-blocking) (mode fd-mixin))
78 (defgeneric output-fd-non-blocking (socket)
79 (:documentation "placeholder"))
81 (defgeneric (setf output-fd-non-blocking) (mode fd-mixin))
83 (defclass dual-channel-single-fd-mixin (dual-channel-fd-mixin)
85 (:documentation "placeholder"))
87 (defgeneric fd-of (stream)
88 (:documentation "placeholder")
89 (:method ((stream dual-channel-single-fd-mixin))
90 (with-accessors ((fd-in input-fd-of)
91 (fd-out output-fd-of)) stream
92 (assert (eql fd-in fd-out) (fd-in fd-out)
93 "Input and output FDs must be equal: ~A, ~A" fd-in fd-out)
94 (values fd-in))))
96 (defgeneric (setf fd-of) (fd stream)
97 (:documentation "placeholder")
98 (:method (fd (stream dual-channel-single-fd-mixin))
99 (with-accessors ((fd-in input-fd-of)
100 (fd-out output-fd-of)) stream
101 (setf fd-in fd fd-out fd)
102 (values fd-in))))
104 (defgeneric fd-non-blocking (fd-mixin)
105 (:documentation "placeholder"))
107 (defgeneric (setf fd-non-blocking) (mode fd-mixin))
109 ;;;; Bivalent Socket Gray Stream
111 (defclass dual-channel-gray-stream (trivial-gray-stream-mixin
112 dual-channel-fd-mixin
113 fundamental-binary-input-stream
114 fundamental-binary-output-stream
115 fundamental-character-input-stream
116 fundamental-character-output-stream)
117 ((external-format :initform :default :initarg :external-format
118 :reader external-format-of
119 :documentation "placehold")
120 ;; Input buffer.
121 (input-buffer :initform nil :type (or iobuf null)
122 :accessor input-buffer-of)
123 ;; Output buffer.
124 (output-buffer :initform nil :type (or iobuf null)
125 :accessor output-buffer-of)
126 ;; Flag used by stream-force-output.
127 (dirty :initform nil :type boolean :accessor dirtyp)
128 ;; Last read char buffer index.
129 (ibuf-unread-index :initform 0 :type buffer-index
130 :accessor ibuf-unread-index-of))
131 (:documentation "placeholder"))
133 (defgeneric (setf external-format-of) (external-format stream)
134 (:documentation "placeholder"))
136 (defclass dual-channel-single-fd-gray-stream
137 (dual-channel-gray-stream dual-channel-single-fd-mixin)
139 (:documentation "placeholder"))