Added stream-write-char and stream-write-string by Francois-Rene Rideau.
[iolib.git] / sockets / base-sockets.lisp
bloba93c1a6678ca3774df9260cfa15f2c6a8735ade2
1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
4 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;
25 ;;; ;;;
26 ;;; Socket buffers ;;;
27 ;;; ;;;
28 ;;;;;;;;;;;;;;;;;;;;;;
30 (deftype stream-buffer ()
31 'et:foreign-pointer)
33 (deftype buffer-index ()
34 '(unsigned-byte 24))
36 (defstruct (iobuf
37 (:constructor %make-iobuf ()))
38 (data (cffi:null-pointer) :type stream-buffer)
39 (size 0 :type buffer-index)
40 (start 0 :type buffer-index)
41 (end 0 :type buffer-index))
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;; ;;;
45 ;;; Bivalent socket Gray stream ;;;
46 ;;; ;;;
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 (deftype stream-position ()
50 '(unsigned-byte 64))
52 (defclass dual-channel-gray-stream (fundamental-binary-input-stream
53 fundamental-binary-output-stream
54 fundamental-character-input-stream
55 fundamental-character-output-stream)
56 ((external-format :initform (find-external-format :default))
57 ;; Input buffer.
58 (input-buffer :initform nil :type (or iobuf null))
59 ;; Output buffer.
60 (output-buffer :initform nil :type (or iobuf null))
61 ;; Last read char buffer index
62 (ibuf-unread-index :initform 0 :type buffer-index)
63 ;; Input stream position
64 (istream-pos :initform 0 :type stream-position)
65 ;; Output stream position
66 (ostream-pos :initform 0 :type stream-position)))
69 ;;;;;;;;;;;;;;;
70 ;;; ;;;
71 ;;; Sockets ;;;
72 ;;; ;;;
73 ;;;;;;;;;;;;;;;
75 (defclass socket ()
76 ((fd :initform nil :reader socket-fd)
77 (family :initarg :family :reader socket-family)
78 (protocol :initarg :protocol :reader socket-protocol)))
80 (defgeneric socket-type (socket))
82 (defgeneric socket-non-blocking (socket))
83 (defgeneric (setf socket-non-blocking) (value socket))
85 (defgeneric socket-open-p (socket))
87 (defgeneric local-name (socket))
89 (defgeneric socket-address (socket))
91 (defgeneric socket-port (socket))
93 (defgeneric remote-name (socket))
95 (defgeneric get-socket-option (socket option-name))
97 (defgeneric set-socket-option (socket option-name &key &allow-other-keys))
99 (defclass stream-socket (socket) ()
100 (:default-initargs :type :stream))
102 (defclass datagram-socket (socket) ()
103 (:default-initargs :type :datagram))
105 (defgeneric unconnect (socket))
107 (defclass internet-socket (socket) ()
108 (:default-initargs :family (if *ipv6* :ipv6 :ipv4)))
110 (defclass local-socket (socket) ()
111 (:default-initargs :family :local))
113 (defclass active-socket (socket dual-channel-gray-stream) ())
115 (defgeneric connect (socket address &key &allow-other-keys))
117 (defgeneric socket-connected-p (socket))
119 (defgeneric shutdown (socket direction))
121 (defgeneric socket-send (buffer socket &key &allow-other-keys))
123 (defgeneric socket-receive (buffer socket &key &allow-other-keys))
125 (defclass passive-socket (socket)
126 ((bound :initform nil :reader socket-bound-p :type boolean)
127 (listening :initform nil :reader socket-listening-p :type boolean)
128 (active-class :initarg :active-class :reader active-class
129 :type symbol :allocation :class)))
131 (defgeneric bind-address (socket address &key &allow-other-keys))
133 (defgeneric socket-listen (socket &key backlog &allow-other-keys))
135 (defgeneric accept-connection (passive-socket &key wait &allow-other-keys))
137 (defclass socket-stream-internet-active (active-socket stream-socket internet-socket) ())
139 (defclass socket-stream-internet-passive (passive-socket stream-socket internet-socket) ()
140 (:default-initargs :active-class 'socket-stream-internet-active))
142 (defclass socket-stream-local-active (active-socket stream-socket local-socket) ())
144 (defclass socket-stream-local-passive (passive-socket stream-socket local-socket) ()
145 (:default-initargs :active-class 'socket-stream-local-active))
147 (defclass socket-datagram-local-active (active-socket datagram-socket local-socket) ())
149 (defclass socket-datagram-internet-active (active-socket datagram-socket internet-socket) ())