From 8cef264610e067b6da2f17005bac031f772b281b Mon Sep 17 00:00:00 2001 From: Stelian Ionescu Date: Sun, 20 Jan 2008 00:31:29 +0100 Subject: [PATCH] Renamed socket-send to send-to and socket-receive to receive-from, also slightly changed their signatures. Signed-off-by: Stelian Ionescu --- sockets/base-sockets.lisp | 4 ++-- sockets/dns/query.lisp | 29 ++++++++++++------------ sockets/pkgdcl.lisp | 4 ++-- sockets/socket-methods.lisp | 55 ++++++++++++++++++++++++--------------------- tests/sockets.lisp | 23 +++++++++---------- 5 files changed, 58 insertions(+), 57 deletions(-) diff --git a/sockets/base-sockets.lisp b/sockets/base-sockets.lisp index f7c2072..2626066 100644 --- a/sockets/base-sockets.lisp +++ b/sockets/base-sockets.lisp @@ -75,9 +75,9 @@ (defgeneric shutdown (socket direction)) -(defgeneric socket-send (buffer socket &rest args &key &allow-other-keys)) +(defgeneric receive-from (socket &rest args &key &allow-other-keys)) -(defgeneric socket-receive (buffer socket &rest args &key &allow-other-keys)) +(defgeneric send-to (socket buffer &rest args &key &allow-other-keys)) (defclass passive-socket (socket) ((listening :initform nil :reader socket-listening-p :type boolean) diff --git a/sockets/dns/query.lisp b/sockets/dns/query.lisp index 548db28..9586f43 100644 --- a/sockets/dns/query.lisp +++ b/sockets/dns/query.lisp @@ -222,16 +222,15 @@ (defconstant +dns-port+ 53) (defun do-udp-dns-query (buffer length nameserver timeout) - (let ((input-buffer (make-array +dns-max-datagram-size+ :element-type 'ub8))) - (with-open-stream - (socket (make-socket :connect :active :type :datagram - :remote-host nameserver :remote-port +dns-port+ - :ipv6 (ipv6-address-p nameserver))) - (socket-send buffer socket :end length) - (iomux:wait-until-fd-ready (fd-of socket) :read timeout t) - (multiple-value-bind (buf len) - (socket-receive input-buffer socket) - (values buf len))))) + (with-open-stream + (socket (make-socket :connect :active :type :datagram + :remote-host nameserver :remote-port +dns-port+ + :ipv6 (ipv6-address-p nameserver))) + (send-to socket buffer :end length) + (iomux:wait-until-fd-ready (fd-of socket) :read timeout t) + (multiple-value-bind (buf len) + (receive-from socket :size +dns-max-datagram-size+) + (values buf len)))) (defun wait-until-socket-connected (socket timeout) (if (nth-value 1 (iomux:wait-until-fd-ready (fd-of socket) :write timeout)) @@ -244,12 +243,12 @@ ;; two-octet length prefix (replace minibuf (ub16-to-vector length)) (replace minibuf buffer :start1 2 :end2 length) - (socket-send minibuf socket :end (+ length 2)))) + (send-to socket minibuf :end (+ length 2)))) (defun get-tcp-query-length (socket timeout) - (let ((minibuf (make-array 2 :element-type 'ub8))) - (iomux:wait-until-fd-ready (fd-of socket) :read timeout t) - (socket-receive minibuf socket) + (iomux:wait-until-fd-ready (fd-of socket) :read timeout t) + (multiple-value-bind (minibuf) + (receive-from socket :size 2) (+ (ash (aref minibuf 0) 8) (aref minibuf 1)))) @@ -259,7 +258,7 @@ (input-buffer (make-array message-length :element-type 'ub8))) (loop :with off := 0 :do (iomux:wait-until-fd-ready fd :read (funcall time-fn) t) - (let ((inbytes (nth-value 1 (socket-receive input-buffer socket :start off)))) + (let ((inbytes (nth-value 1 (receive-from socket :buffer input-buffer :start off)))) (incf off inbytes) (when (= off message-length) (return (values input-buffer off)))))))) diff --git a/sockets/pkgdcl.lisp b/sockets/pkgdcl.lisp index 5e75d2c..c63c4c0 100644 --- a/sockets/pkgdcl.lisp +++ b/sockets/pkgdcl.lisp @@ -185,9 +185,11 @@ #:local-port #:make-socket #:make-socket-stream + #:receive-from #:remote-address #:remote-name #:remote-port + #:send-to #:shutdown #:socket-connected-p #:socket-family @@ -196,8 +198,6 @@ #:socket-open-p #:socket-option #:socket-protocol - #:socket-receive - #:socket-send #:socket-type #:with-open-socket #:with-accept-connection diff --git a/sockets/socket-methods.lisp b/sockets/socket-methods.lisp index f280c17..046a54d 100644 --- a/sockets/socket-methods.lisp +++ b/sockets/socket-methods.lisp @@ -345,7 +345,7 @@ (declare (ignore direction)) (error "You cannot shut down passive sockets.")) -;;;; SEND +;;;; SENDTO (eval-when (:compile-toplevel :load-toplevel :execute) (defun compute-flags (flags args) @@ -395,7 +395,7 @@ (vector (values (coerce buff 'ub8-sarray) start (- end start))))) -(defun %socket-send (buffer socket start end remote-address remote-port flags) +(defun %socket-send (socket buffer start end remote-address remote-port flags) (when (typep socket 'passive-socket) (error "You cannot send data on a passive socket.")) (check-type start unsigned-byte "a non-negative integer") @@ -416,20 +416,19 @@ (if remote-address ss (null-pointer)) (if remote-address size-of-sockaddr-storage 0)))))) -(defmethod socket-send ((buffer array) (socket active-socket) &rest args - &key (start 0) end remote-address (remote-port 0) &allow-other-keys) - (%socket-send buffer socket start end remote-address remote-port +(defmethod send-to ((socket active-socket) buffer &rest args + &key (start 0) end remote-address (remote-port 0)) + (%socket-send socket buffer start end remote-address remote-port (compute-flags *sendmsg-flags* args))) -(define-compiler-macro socket-send (&whole form buffer socket &rest args - &key (start 0) end remote-address (remote-port 0) - &allow-other-keys) +(define-compiler-macro send-to (&whole form socket buffer &rest args + &key (start 0) end remote-address (remote-port 0)) (let ((flags (compute-flags *sendmsg-flags* args))) - (cond (flags `(%socket-send ,buffer ,socket ,start ,end + (cond (flags `(%socket-send ,socket ,buffer ,start ,end ,remote-address ,remote-port ,flags)) (t form)))) -;;;; RECV +;;;; RECVFROM (eval-when (:compile-toplevel :load-toplevel :execute) (defparameter *recvfrom-flags* nil) @@ -453,7 +452,7 @@ (etypecase buff (ub8-sarray (values buff start (- end start))))) -(defun %socket-receive-bytes (buffer ss fd flags start end) +(defun %socket-receive-bytes (fd buffer start end flags ss) (multiple-value-bind (buff start-offset bufflen) (%normalize-receive-buffer buffer start end) (with-socklen (size size-of-sockaddr-storage) @@ -463,39 +462,43 @@ (%recvfrom fd buff-sap bufflen flags ss size))))) (declaim (inline %socket-receive-stream-socket)) -(defun %socket-receive-stream-socket (buffer socket start end flags) +(defun %socket-receive-stream-socket (socket buffer start end flags) (with-sockaddr-storage (ss) - (let ((bytes-received (%socket-receive-bytes buffer ss (fd-of socket) flags - start end))) + (let ((bytes-received (%socket-receive-bytes (fd-of socket) buffer + start end flags ss))) (values buffer bytes-received)))) (declaim (inline %socket-receive-datagram-socket)) -(defun %socket-receive-datagram-socket (buffer socket start end flags) +(defun %socket-receive-datagram-socket (socket buffer start end flags) (with-sockaddr-storage (ss) - (let ((bytes-received (%socket-receive-bytes buffer ss (fd-of socket) flags - start end))) + (let ((bytes-received (%socket-receive-bytes (fd-of socket) buffer + start end flags ss))) (multiple-value-bind (remote-address remote-port) (sockaddr-storage->sockaddr ss) (values buffer bytes-received remote-address remote-port))))) -(defun %socket-receive (buffer socket start end flags) +(defun %socket-receive (socket buffer start end size flags) (when (typep socket 'passive-socket) (error "You cannot receive data from a passive socket.")) + (unless buffer + (check-type size unsigned-byte "a non-negative integer") + (setf buffer (make-array size :element-type 'ub8) + start 0 end size)) (etypecase socket (stream-socket (%socket-receive-stream-socket - buffer socket start end flags)) + socket buffer start end flags)) (datagram-socket (%socket-receive-datagram-socket - buffer socket start end flags)))) + socket buffer start end flags)))) -(defmethod socket-receive ((buffer array) (socket active-socket) - &rest args &key (start 0) end &allow-other-keys) - (%socket-receive buffer socket start end +(defmethod receive-from ((socket active-socket) + &rest args &key buffer size (start 0) end) + (%socket-receive socket buffer start end size (compute-flags *recvfrom-flags* args))) -(define-compiler-macro socket-receive (&whole form buffer socket &rest args - &key (start 0) end &allow-other-keys) +(define-compiler-macro receive-from (&whole form socket &rest args + &key buffer size (start 0) end) (let ((flags (compute-flags *recvfrom-flags* args))) - (cond (flags `(%socket-receive ,buffer ,socket ,start ,end ,flags)) + (cond (flags `(%socket-receive ,socket ,buffer ,start ,end ,size ,flags)) (t form)))) ;;;; Datagram Sockets diff --git a/tests/sockets.lisp b/tests/sockets.lisp index 874db9e..ba32f67 100644 --- a/tests/sockets.lisp +++ b/tests/sockets.lisp @@ -365,21 +365,20 @@ (is-true (with-open-socket (s :remote-host *echo-address* :remote-port *echo-port* :type :datagram :family :ipv4) - (let ((data (make-array '(200) :element-type '(unsigned-byte 8)))) - (socket-send "here is some text" s) - (socket-receive data s) - ;; (format t "~&Got ~S back from UDP echo server~%" data) - (> (length data) 0))))) + (send-to s "here is some text") + (multiple-value-bind (data nbytes) + (receive-from s :size 200) + (plusp nbytes))))) (test simple-udp-client.2 (is-true (with-open-socket (s :type :datagram :family :ipv4) - (let ((data (make-array 100 :element-type '(unsigned-byte 8)))) - (socket-send "here is some more text" s - :remote-address *echo-address* - :remote-port *echo-port*) - (socket-receive data s) - (> (length data) 0))))) + (send-to s "here is some more text" + :remote-address *echo-address* + :remote-port *echo-port*) + (multiple-value-bind (data nbytes) + (receive-from s :size 200) + (plusp nbytes))))) (test simple-local-sockets (is (string= (let ((file (namestring @@ -455,6 +454,6 @@ (with-open-socket (s :type :datagram :local-port port) (loop (multiple-value-bind (buf len address port) - (socket-receive s nil 500) + (receive-from s :size 500) (format t "Received ~A bytes from ~A:~A - ~A ~%" len address port (subseq buf 0 (min 10 len))))))) -- 2.11.4.GIT