Fixed up Unix sockets a bit.
[lisp-utils.git] / common-net.lisp
index fadf080..57a5b84 100644 (file)
 
 (define-condition network-error (error) ())
 
+(define-condition simple-network-error (network-error simple-error) ())
+
+(defun simple-network-error (format &rest args)
+  (error 'simple-network-error :format-control format :format-arguments args))
+
 (define-condition socket-error (socket-condition network-error) ())
 
 (define-condition address-busy (network-error)
   (declare (type stream-socket socket))
   (unless (eq (stream-socket-mode socket) :character)
     (error 'stream-mode-error :stream socket :socket socket :expected-mode :character))
-  (case (fill-char-buffer socket 1)
+  (case (fill-char-buffer socket 1 t)
     ((nil) (return-from gray-stream-read-char-no-hang :eof))
     ((:wait) (return-from gray-stream-read-char-no-hang nil)))
   (with-slots (char-buffer char-read-pos) socket
 
 (defmethod connected-address-p ((address tcp-address)) t)
 
-(export '(tcp-address tcp4-address tcp6-address))
+(defun tcp-address-for (host-address port)
+  (check-type port (unsigned-byte 16))
+  (etypecase host-address
+    (ipv4-address (make-instance 'tcp4-address :host-address host-address :port port))
+    (ipv6-address (make-instance 'tcp6-address :host-address host-address :port port))))
+
+(export '(tcp-address tcp4-address tcp6-address tcp-address-for))
 
 ;;; UDP code
 
 
 (defmethod connected-address-p ((address tcp-address)) nil)
 
-(export '(udp-address udp4-address udp6-address))
+(defun udp-address-for (host-address port)
+  (check-type port (unsigned-byte 16))
+  (etypecase host-address
+    (ipv4-address (make-instance 'udp4-address :host-address host-address :port port))
+    (ipv6-address (make-instance 'udp6-address :host-address host-address :port port))))
+
+(export '(udp-address udp4-address udp6-address udp-address-for))
 
 ;;; Unix sockets
 
 (defclass local-address (address)
-  ((path :type pathname)))
+  ((path :type (or pathname nil))))
 
 (defmethod initialize-instance :after ((instance local-address) &key path)
-  (setf (slot-value instance 'path) (pathname path)))
+  (setf (slot-value instance 'path) (and path (pathname path))))
 
 (defmethod format-address ((address local-address))
-  (namestring (slot-value address 'path)))
+  (let ((path (slot-value address 'path)))
+    (and path (namestring path))))
 
 (defclass local-stream-address (local-address) ())
 (defclass local-seq-address (local-address) ())