set-timeouts.lisp
 1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*-
2
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
4
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 (in-package :hunchentoot)
30
31 (defun set-timeouts (usocket read-timeout write-timeout)
32 "Sets up timeouts on the given USOCKET object. READ-TIMEOUT is the
33 read timeout period, WRITE-TIMEOUT is the write timeout, specified in
34 \(fractional) seconds. The timeouts can either be implemented using
35 the low-level socket options SO_RCVTIMEO and SO_SNDTIMEO or some
36 other, implementation specific mechanism. On platforms that do not
37 support separate read and write timeouts, both must be equal or an
38 error will be signaled. READ-TIMEOUT and WRITE-TIMEOUT may be NIL,
39 which means that the corresponding socket timeout value will not be
40 set."
41 (declare (ignorable usocket read-timeout write-timeout))
42 ;; add other Lisps here if necessary
43 #+(or :sbcl :cmu :abcl)
44 (unless (eql read-timeout write-timeout)
45 (parameter-error "Read and write timeouts for socket must be equal."))
46 #+:clisp
47 (when read-timeout
48 (socket:socket-options (usocket:socket usocket) :SO-RCVTIMEO read-timeout))
49 #+:clisp
50 (when write-timeout
51 (socket:socket-options (usocket:socket usocket) :SO-SNDTIMEO write-timeout))
52 #+:ecl
53 (when read-timeout
54 (setf (sb-bsd-sockets:sockopt-receive-timeout (usocket:socket usocket))
55 read-timeout))
56 #+:ecl
57 (when write-timeout
58 (setf (sb-bsd-sockets:sockopt-send-timeout (usocket:socket usocket))
59 write-timeout))
60 #+:openmcl
61 (when read-timeout
62 (setf (ccl:stream-input-timeout (usocket:socket usocket))
63 read-timeout))
64 #+:openmcl
65 (when write-timeout
66 (setf (ccl:stream-output-timeout (usocket:socket usocket))
67 write-timeout))
68 #+:sbcl
69 (when read-timeout
70 (setf (sb-impl::fd-stream-timeout (usocket:socket-stream usocket))
71 (coerce read-timeout 'single-float)))
72 #+:cmu
73 (setf (lisp::fd-stream-timeout (usocket:socket-stream usocket))
74 (coerce read-timeout 'integer))
75 #+:abcl
76 (when read-timeout
77 (java:jcall (java:jmethod "java.net.Socket" "setSoTimeout" "int")
78 (usocket:socket usocket)
79 (* 1000 read-timeout)))
80 #+:abcl
81 (when write-timeout
82 (warn "Unimplemented."))
83 #-(or :clisp :allegro :openmcl :sbcl :lispworks :cmu :ecl :abcl)
84 (not-implemented 'set-timeouts))
85