Minor change.
[iolib.git] / net.sockets / base-sockets.lisp
blob311ab8875dffe40b696a8ec90b8304ae744bc44b
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; base-sockets.lisp --- Base socket 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 :net.sockets)
26 ;;;; Sockets
28 (defclass socket (dual-channel-single-fd-mixin)
29 ((family :initarg :family :accessor socket-family)
30 (protocol :initarg :protocol :accessor socket-protocol)
31 (bound :initform nil :reader socket-bound-p :type boolean)))
33 (defgeneric socket-fd (socket))
34 (defgeneric (setf socket-fd) (fd socket))
35 (defgeneric socket-type (socket))
36 (defgeneric socket-open-p (socket))
37 (defgeneric local-name (socket))
38 (defgeneric local-address (socket))
39 (defgeneric local-port (socket))
40 (defgeneric remote-name (socket))
41 (defgeneric remote-address (socket))
42 (defgeneric remote-port (socket))
43 (defgeneric socket-option (socket option-name))
45 (defclass stream-socket (socket) ()
46 (:default-initargs :type :stream))
48 (defclass datagram-socket (socket) ()
49 (:default-initargs :type :datagram))
51 (defgeneric disconnect (socket))
53 (define-symbol-macro +default-inet-family+
54 (if *ipv6* :ipv6 :ipv4))
56 (defclass internet-socket (socket) ()
57 (:default-initargs :family +default-inet-family+))
59 (defclass local-socket (socket) ()
60 (:default-initargs :family :local))
62 (defun socket-read-fn (fd buffer nbytes)
63 (%recvfrom fd buffer nbytes 0 (null-pointer) (null-pointer)))
65 (defun socket-write-fn (fd buffer nbytes)
66 (%sendto fd buffer nbytes 0 (null-pointer) 0))
68 (defclass active-socket (socket dual-channel-gray-stream) ()
69 (:default-initargs :read-fn 'socket-read-fn
70 :write-fn 'socket-write-fn))
72 (defgeneric connect (socket address &key &allow-other-keys))
74 (defgeneric socket-connected-p (socket))
76 (defgeneric shutdown (socket direction))
78 (defgeneric receive-from (socket &rest args &key &allow-other-keys))
80 (defgeneric send-to (socket buffer &rest args &key &allow-other-keys))
82 (defclass passive-socket (socket)
83 ((listening :initform nil :reader socket-listening-p :type boolean)
84 (external-format :initarg :external-format :reader external-format-of)
85 (active-class :initarg :active-class :reader active-class
86 :type symbol :allocation :class))
87 (:default-initargs :external-format :default))
89 (defgeneric bind-address (socket address &key &allow-other-keys))
91 (defgeneric socket-listen (socket &key &allow-other-keys))
93 (defgeneric accept-connection (passive-socket &key &allow-other-keys))
95 (defclass socket-stream-internet-active
96 (active-socket stream-socket internet-socket)
97 ())
99 (defclass socket-stream-internet-passive
100 (passive-socket stream-socket internet-socket)
102 (:default-initargs :active-class 'socket-stream-internet-active))
104 (defclass socket-stream-local-active (active-socket stream-socket local-socket)
107 (defclass socket-stream-local-passive
108 (passive-socket stream-socket local-socket)
110 (:default-initargs :active-class 'socket-stream-local-active))
112 (defclass socket-datagram-local-active
113 (active-socket datagram-socket local-socket)
116 (defclass socket-datagram-internet-active
117 (active-socket datagram-socket internet-socket)