1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
5 ;; This code is free software; you can redistribute it and/or
6 ;; modify it under the terms of the version 2.1 of
7 ;; the GNU Lesser General Public License as published by
8 ;; the Free Software Foundation, as clarified by the
9 ;; preamble found here:
10 ;; http://opensource.franz.com/preamble.html
12 ;; This program 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 Lesser General
18 ;; Public License along with this library; if not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA
22 (in-package :net.sockets
)
24 ;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Socket buffers ;;;
28 ;;;;;;;;;;;;;;;;;;;;;;
30 (deftype stream-buffer
()
33 (deftype buffer-index
()
37 (:constructor %make-iobuf
()))
38 (data (null-pointer) :type stream-buffer
)
39 (size 0 :type buffer-index
)
40 (start 0 :type buffer-index
)
41 (end 0 :type buffer-index
))
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;; Bivalent socket Gray stream ;;;
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 (deftype stream-position
()
52 (defclass dual-channel-fd-stream-mixin
()
53 ((input-fd :initform nil
:accessor input-fd-of
)
54 (output-fd :initform nil
:accessor output-fd-of
)))
56 (defclass dual-channel-single-fd-stream-mixin
(dual-channel-fd-stream-mixin) ())
58 (defgeneric fd-of
(stream)
59 (:method
((stream dual-channel-single-fd-stream-mixin
))
60 (with-accessors ((fd-in input-fd-of
)
61 (fd-out output-fd-of
)) stream
62 (assert (eql fd-in fd-out
))
64 (defgeneric (setf fd-of
) (fd stream
)
65 (:method
(fd (stream dual-channel-single-fd-stream-mixin
))
66 (with-accessors ((fd-in input-fd-of
)
67 (fd-out output-fd-of
)) stream
68 (assert (eql fd-in fd-out
))
69 (setf fd-in fd fd-out fd
)
72 (defclass dual-channel-gray-stream
(dual-channel-fd-stream-mixin
73 fundamental-binary-input-stream
74 fundamental-binary-output-stream
75 fundamental-character-input-stream
76 fundamental-character-output-stream
)
77 ((external-format :initform
(find-external-format :default
)
78 :accessor external-format-of
)
80 (input-buffer :initform nil
:type
(or iobuf null
)
81 :accessor input-buffer-of
)
83 (output-buffer :initform nil
:type
(or iobuf null
)
84 :accessor output-buffer-of
)
85 ;; Flag used by stream-force-output
86 (must-flush-output :initform nil
:type boolean
87 :accessor must-flush-output-p
)
88 ;; Last read char buffer index
89 (ibuf-unread-index :initform
0 :type buffer-index
90 :accessor ibuf-unread-index-of
)))
99 (defclass socket
(dual-channel-single-fd-stream-mixin)
100 ((family :initarg
:family
:accessor socket-family
)
101 (protocol :initarg
:protocol
:accessor socket-protocol
)))
103 (defgeneric socket-fd
(socket))
104 (defgeneric (setf socket-fd
) (fd socket
))
106 (defgeneric socket-type
(socket))
108 (defgeneric socket-non-blocking
(socket))
109 (defgeneric (setf socket-non-blocking
) (value socket
))
111 (defgeneric socket-open-p
(socket))
113 (defgeneric local-name
(socket))
115 (defgeneric socket-address
(socket))
117 (defgeneric socket-port
(socket))
119 (defgeneric remote-name
(socket))
121 (defgeneric get-socket-option
(socket option-name
))
123 (defgeneric set-socket-option
(socket option-name
&key
&allow-other-keys
))
125 (defclass stream-socket
(socket) ()
126 (:default-initargs
:type
:stream
))
128 (defclass datagram-socket
(socket) ()
129 (:default-initargs
:type
:datagram
))
131 (defgeneric unconnect
(socket))
133 (defclass internet-socket
(socket) ()
134 (:default-initargs
:family
(if *ipv6
* :ipv6
:ipv4
)))
136 (defclass local-socket
(socket) ()
137 (:default-initargs
:family
:local
))
139 (defclass active-socket
(socket dual-channel-gray-stream
) ())
141 (defgeneric connect
(socket address
&key
&allow-other-keys
))
143 (defgeneric socket-connected-p
(socket))
145 (defgeneric shutdown
(socket direction
))
147 (defgeneric socket-send
(buffer socket
&key
&allow-other-keys
))
149 (defgeneric socket-receive
(buffer socket
&key
&allow-other-keys
))
151 (defclass passive-socket
(socket)
152 ((bound :initform nil
:reader socket-bound-p
:type boolean
)
153 (listening :initform nil
:reader socket-listening-p
:type boolean
)
154 (active-class :initarg
:active-class
:reader active-class
155 :type symbol
:allocation
:class
)))
157 (defgeneric bind-address
(socket address
&key
&allow-other-keys
))
159 (defgeneric socket-listen
(socket &key backlog
&allow-other-keys
))
161 (defgeneric accept-connection
(passive-socket &key wait
&allow-other-keys
))
163 (defclass socket-stream-internet-active
(active-socket stream-socket internet-socket
) ())
165 (defclass socket-stream-internet-passive
(passive-socket stream-socket internet-socket
) ()
166 (:default-initargs
:active-class
'socket-stream-internet-active
))
168 (defclass socket-stream-local-active
(active-socket stream-socket local-socket
) ())
170 (defclass socket-stream-local-passive
(passive-socket stream-socket local-socket
) ()
171 (:default-initargs
:active-class
'socket-stream-local-active
))
173 (defclass socket-datagram-local-active
(active-socket datagram-socket local-socket
) ())
175 (defclass socket-datagram-internet-active
(active-socket datagram-socket internet-socket
) ())