From ctian at common-lisp.net Wed Dec 8 04:43:06 2010 From: ctian at common-lisp.net (Chun Tian (binghe)) Date: Tue, 07 Dec 2010 23:43:06 -0500 Subject: [usocket-cvs] r570 - in usocket/trunk: . backend Message-ID: Author: ctian Date: Tue Dec 7 23:43:05 2010 New Revision: 570 Log: LispWorks: concurrent recv/send on mutiple UDP sockets. Patched by Kamil Shakirov Modified: usocket/trunk/backend/lispworks.lisp usocket/trunk/usocket.lisp Modified: usocket/trunk/backend/lispworks.lisp ============================================================================== --- usocket/trunk/backend/lispworks.lisp (original) +++ usocket/trunk/backend/lispworks.lisp Tue Dec 7 23:43:05 2010 @@ -358,48 +358,41 @@ "Additional socket-close method for datagram-usocket" (setf (%open-p socket) nil)) -(defvar *message-send-buffer* - (make-array +max-datagram-packet-size+ - :element-type '(unsigned-byte 8) - :allocation :static)) +(defmethod initialize-instance :after ((socket datagram-usocket) &key) + (setf (slot-value socket 'send-buffer) + (make-array +max-datagram-packet-size+ + :element-type '(unsigned-byte 8) + :allocation :static)) + (setf (slot-value socket 'recv-buffer) + (make-array +max-datagram-packet-size+ + :element-type '(unsigned-byte 8) + :allocation :static))) -(defvar *message-send-lock* - (mp:make-lock :name "USOCKET message send lock")) - -(defun send-message (socket-fd buffer &optional (length (length buffer)) host service) +(defun send-message (socket-fd message buffer &optional (length (length buffer)) host service) "Send message to a socket, using sendto()/send()" (declare (type integer socket-fd) (type sequence buffer)) - (let ((message *message-send-buffer*)) - (fli:with-dynamic-foreign-objects ((client-addr (:struct comm::sockaddr_in)) - (len :int - #-(or lispworks4 lispworks5.0) ; <= 5.0 - :initial-element - (fli:size-of '(:struct comm::sockaddr_in)))) - (fli:with-dynamic-lisp-array-pointer (ptr message :type '(:unsigned :byte)) - (mp:with-lock (*message-send-lock*) - (replace message buffer :end2 length) - (if (and host service) - (progn - (comm::initialize-sockaddr_in client-addr comm::*socket_af_inet* host service "udp") - (%sendto socket-fd ptr (min length +max-datagram-packet-size+) 0 - (fli:copy-pointer client-addr :type '(:struct comm::sockaddr)) - (fli:dereference len))) - (comm::%send socket-fd ptr (min length +max-datagram-packet-size+) 0))))))) + (fli:with-dynamic-foreign-objects ((client-addr (:struct comm::sockaddr_in)) + (len :int + #-(or lispworks4 lispworks5.0) ; <= 5.0 + :initial-element + (fli:size-of '(:struct comm::sockaddr_in)))) + (fli:with-dynamic-lisp-array-pointer (ptr message :type '(:unsigned :byte)) + (replace message buffer :end2 length) + (if (and host service) + (progn + (comm::initialize-sockaddr_in client-addr comm::*socket_af_inet* host service "udp") + (%sendto socket-fd ptr (min length +max-datagram-packet-size+) 0 + (fli:copy-pointer client-addr :type '(:struct comm::sockaddr)) + (fli:dereference len))) + (comm::%send socket-fd ptr (min length +max-datagram-packet-size+) 0))))) (defmethod socket-send ((socket datagram-usocket) buffer length &key host port) - (let ((s (socket socket))) - (send-message s buffer length (and host (host-to-hbo host)) port))) - -(defvar *message-receive-buffer* - (make-array +max-datagram-packet-size+ - :element-type '(unsigned-byte 8) - :allocation :static)) - -(defvar *message-receive-lock* - (mp:make-lock :name "USOCKET message receive lock")) + (send-message (socket socket) + (slot-value socket 'send-buffer) + buffer length (and host (host-to-hbo host)) port)) -(defun receive-message (socket-fd &optional buffer (length (length buffer)) +(defun receive-message (socket-fd message &optional buffer (length (length buffer)) &key read-timeout (max-buffer-size +max-datagram-packet-size+)) "Receive message from socket, read-timeout is a float number in seconds. @@ -410,8 +403,7 @@ 4. remote port" (declare (type integer socket-fd) (type sequence buffer)) - (let ((message *message-receive-buffer*) - old-timeout) + (let (old-timeout) (fli:with-dynamic-foreign-objects ((client-addr (:struct comm::sockaddr_in)) (len :int #-(or lispworks4 lispworks5.0) ; <= 5.0 @@ -422,40 +414,40 @@ (when read-timeout (setf old-timeout (get-socket-receive-timeout socket-fd)) (set-socket-receive-timeout socket-fd read-timeout)) - (mp:with-lock (*message-receive-lock*) - (let ((n (%recvfrom socket-fd ptr max-buffer-size 0 - (fli:copy-pointer client-addr :type '(:struct comm::sockaddr)) - len))) - ;; restore old read timeout - (when (and read-timeout (/= old-timeout read-timeout)) - (set-socket-receive-timeout socket-fd old-timeout)) - (if (plusp n) - (values (if buffer - (replace buffer message - :end1 (min length max-buffer-size) - :end2 (min n max-buffer-size)) + (let ((n (%recvfrom socket-fd ptr max-buffer-size 0 + (fli:copy-pointer client-addr :type '(:struct comm::sockaddr)) + len))) + ;; restore old read timeout + (when (and read-timeout (/= old-timeout read-timeout)) + (set-socket-receive-timeout socket-fd old-timeout)) + (if (plusp n) + (values (if buffer + (replace buffer message + :end1 (min length max-buffer-size) + :end2 (min n max-buffer-size)) (subseq message 0 (min n max-buffer-size))) - (min n max-buffer-size) - (comm::ntohl (fli:foreign-slot-value - (fli:foreign-slot-value client-addr - 'comm::sin_addr - :object-type '(:struct comm::sockaddr_in) - :type '(:struct comm::in_addr) - :copy-foreign-object nil) - 'comm::s_addr - :object-type '(:struct comm::in_addr))) - (comm::ntohs (fli:foreign-slot-value client-addr - 'comm::sin_port - :object-type '(:struct comm::sockaddr_in) - :type '(:unsigned :short) - :copy-foreign-object nil))) - (values nil n 0 0)))))))) + (min n max-buffer-size) + (comm::ntohl (fli:foreign-slot-value + (fli:foreign-slot-value client-addr + 'comm::sin_addr + :object-type '(:struct comm::sockaddr_in) + :type '(:struct comm::in_addr) + :copy-foreign-object nil) + 'comm::s_addr + :object-type '(:struct comm::in_addr))) + (comm::ntohs (fli:foreign-slot-value client-addr + 'comm::sin_port + :object-type '(:struct comm::sockaddr_in) + :type '(:unsigned :short) + :copy-foreign-object nil))) + (values nil n 0 0))))))) (defmethod socket-receive ((socket datagram-usocket) buffer length &key) - (let ((s (socket socket))) - (multiple-value-bind (buffer size host port) - (receive-message s buffer length) - (values buffer size host port)))) + (multiple-value-bind (buffer size host port) + (receive-message (socket socket) + (slot-value socket 'recv-buffer) + buffer length) + (values buffer size host port))) (defmethod get-local-name ((usocket usocket)) (multiple-value-bind Modified: usocket/trunk/usocket.lisp ============================================================================== --- usocket/trunk/usocket.lisp (original) +++ usocket/trunk/usocket.lisp Tue Dec 7 23:43:05 2010 @@ -104,7 +104,13 @@ :accessor %open-p :initform t :documentation "Flag to indicate if usocket is open, -for GC on implementions operate on raw socket fd.")) +for GC on implementions operate on raw socket fd.") + #+lispworks + (recv-buffer + :documentation "Private RECV buffer.") + #+lispworks + (send-buffer + :documentation "Private SEND buffer.")) (:documentation "UDP (inet-datagram) socket")) (defun usocket-p (socket) From ctian at common-lisp.net Wed Dec 8 06:33:02 2010 From: ctian at common-lisp.net (Chun Tian (binghe)) Date: Wed, 08 Dec 2010 01:33:02 -0500 Subject: [usocket-cvs] r571 - usocket/trunk/backend Message-ID: Author: ctian Date: Wed Dec 8 01:33:01 2010 New Revision: 571 Log: LispWorks: UDP receive timeout parameter (patched by Kamil Shakirov) Modified: usocket/trunk/backend/lispworks.lisp Modified: usocket/trunk/backend/lispworks.lisp ============================================================================== --- usocket/trunk/backend/lispworks.lisp (original) +++ usocket/trunk/backend/lispworks.lisp Wed Dec 8 01:33:01 2010 @@ -442,11 +442,12 @@ :copy-foreign-object nil))) (values nil n 0 0))))))) -(defmethod socket-receive ((socket datagram-usocket) buffer length &key) +(defmethod socket-receive ((socket datagram-usocket) buffer length &key timeout) (multiple-value-bind (buffer size host port) (receive-message (socket socket) (slot-value socket 'recv-buffer) - buffer length) + buffer length + :read-timeout timeout) (values buffer size host port))) (defmethod get-local-name ((usocket usocket)) From ctian at common-lisp.net Wed Dec 8 08:17:40 2010 From: ctian at common-lisp.net (Chun Tian (binghe)) Date: Wed, 08 Dec 2010 03:17:40 -0500 Subject: [usocket-cvs] r572 - usocket/trunk/backend Message-ID: Author: ctian Date: Wed Dec 8 03:17:40 2010 New Revision: 572 Log: LispWorks: SOCKET-CONNECT's TIMEOUT argument should be used to set up UDP read timeout (RCVTIMEO), fixes for other platforms still needed. Modified: usocket/trunk/backend/lispworks.lisp Modified: usocket/trunk/backend/lispworks.lisp ============================================================================== --- usocket/trunk/backend/lispworks.lisp (original) +++ usocket/trunk/backend/lispworks.lisp Wed Dec 8 03:17:40 2010 @@ -300,9 +300,11 @@ (if (and host port) (connect-to-udp-server (host-to-hostname host) port :local-address (and local-host (host-to-hostname local-host)) - :local-port local-port) + :local-port local-port + :read-timeout timeout) (open-udp-socket :local-address (and local-host (host-to-hostname local-host)) - :local-port local-port)) + :local-port local-port + :read-timeout timeout)) :connected-p t))) (hcl:flag-special-free-action usocket) usocket))))