1 ;; This file is part of CL-ZMQ.
8 (with-foreign-objects ((major :int
)
11 (%version major minor patch
)
15 (mem-ref patch
:int
))))
19 (defmacro with-context
((context) &body body
)
20 `(let ((,context
(ctx-new)))
23 (ctx-destroy ,context
))))
26 (call-with-error-check
30 (defun ctx-get (context option
)
31 (call-with-error-check
38 (defun ctx-set (context option value
)
39 (call-with-error-check
47 (defun ctx-destroy (context)
48 (call-with-error-check
54 (defun socket (context type
)
55 (call-with-error-check
58 (foreign-enum-value 'socket-types
63 (call-with-error-check
67 (defmacro with-socket
((socket context type
) &body body
)
68 `(let ((,socket
(socket ,context
,type
)))
73 (defun bind (s address
)
74 (with-foreign-string (addr address
)
75 (call-with-error-check #'%bind
(list s addr
))))
77 (defun unbind (s address
)
78 (with-foreign-string (addr address
)
79 (call-with-error-check #'%unbind
(list s addr
))))
81 (defun connect (s address
)
82 (with-foreign-string (addr address
)
83 (call-with-error-check #'%connect
(list s addr
))))
85 (defun disconnect (s address
)
86 (with-foreign-string (addr address
)
87 (call-with-error-check #'%disconnect
(list s addr
))))
89 (defun get-socket-option-value-type (option)
96 :last-endpoint
) :string
)
97 ((:maxmsgsize
) :int64
)
100 (defun getsockopt (socket option
)
101 (let ((return-type (get-socket-option-value-type option
))
102 (option-code (foreign-enum-value 'socket-options option
)))
103 (if (eq return-type
:string
)
104 (with-foreign-objects ((str :char
255)
106 (setf (mem-aref len
'size-t
) (* (foreign-type-size :char
) 255))
107 (%getsockopt socket option-code str len
)
108 (foreign-string-to-lisp str
:count
(mem-aref len
'size-t
)))
109 (with-foreign-objects ((obj return-type
)
111 (setf (mem-aref len
'size-t
) (foreign-type-size return-type
))
112 (%getsockopt socket option-code obj len
)
113 (mem-aref obj return-type
)))))
115 (defun setsockopt (socket option value
)
116 (let ((return-type (get-socket-option-value-type option
))
117 (option-code (foreign-enum-value 'socket-options option
)))
118 (if (eq return-type
:string
)
119 (with-zmq-string ((string len
) value
)
120 (%setsockopt socket option-code string len
))
121 (with-foreign-object (obj return-type
)
122 (setf (mem-aref obj return-type
) value
)
123 (%setsockopt socket option-code obj
124 (foreign-type-size return-type
))))))
128 (defstruct (msg (:constructor %make-msg
))
131 (defun make-msg (&key size data
)
132 (let ((msg (%make-msg
))
133 (raw-msg (foreign-alloc 'c-msg
)))
134 (tg:finalize msg
(lambda ()
136 (foreign-free raw-msg
)))
137 (cond (size (%msg-init-size raw-msg size
))
141 (with-foreign-string (fstr data
)
142 (copy-lisp-string-octets
145 (%msg-init-size raw-msg size
)
146 (%msg-data raw-msg
)))))
147 ((simple-array (unsigned-byte 8))
148 (let ((len (length data
)))
149 (%msg-init-size raw-msg len
)
150 (with-pointer-to-vector-data (ptr data
)
151 (%memcpy
(%msg-data raw-msg
) ptr len
))))
153 (%msg-init-size raw-msg
(length data
))
154 (let ((ptr (%msg-data raw-msg
))
157 (setf (mem-aref ptr
:uchar
(incf i
)) x
))
159 (t (%msg-init raw-msg
)))
160 (setf (msg-raw msg
) raw-msg
)
163 (defun msg-data-as-is (msg)
164 (%msg-data
(msg-raw msg
)))
166 (defun msg-data-as-string (msg)
167 (let ((data (%msg-data
(msg-raw msg
)))
168 (size (%msg-size
(msg-raw msg
))))
169 (unless (zerop (pointer-address data
))
170 (foreign-string-to-lisp data
:count size
))))
172 (defun msg-data-as-array (msg)
173 (let ((data (%msg-data
(msg-raw msg
))))
174 (unless (zerop (pointer-address data
))
175 (let* ((len (msg-size msg
))
176 (arr (#+lispworks sys
:in-static-area
177 #-lispworks cl
:identity
178 (make-array len
:element-type
'(unsigned-byte 8)))))
179 (declare (type (simple-array (unsigned-byte 8)) arr
))
180 (with-pointer-to-vector-data (ptr arr
)
181 (%memcpy ptr data len
))
184 (defun msg-size (msg)
185 (%msg-size
(msg-raw msg
)))
187 (defun msg-move (dst src
)
188 (%msg-move
(msg-raw dst
) (msg-raw src
)))
190 (defun msg-copy (dst src
)
191 (%msg-copy
(msg-raw dst
) (msg-raw src
)))
193 ;; Sending and recieving
195 (defun send (s data
&rest flags
)
196 (with-foreign-string ((buf len
) data
)
197 (%send s buf
(1- len
)
198 (foreign-bitfield-value 'send-recv-options flags
))))
200 (defun recv (s length
&rest flags
)
201 (with-foreign-string ((buf len
) (make-string length
))
202 (%recv s buf
(1- len
)
203 (foreign-bitfield-value 'send-recv-options flags
))
204 (foreign-string-to-lisp buf
)))
206 (defun msg-send (s msg
&rest flags
)
207 (%msg-send
(msg-raw msg
) s
208 (foreign-bitfield-value 'send-recv-options flags
)))
210 (defun msg-recv (s msg
&rest flags
)
211 (%msg-recv
(msg-raw msg
) s
212 (foreign-bitfield-value 'send-recv-options flags
)))
215 ;; (xxx)freiksenet: probably broken, don't use it yet.
217 (defclass pollitem
()
218 ((raw :accessor pollitem-raw
:initform nil
)
219 (socket :accessor pollitem-socket
:initform
(cffi:null-pointer
) :initarg
:socket
)
220 (fd :accessor pollitem-fd
:initform -
1 :initarg
:fd
)
221 (events :accessor pollitem-events
:initform
0 :initarg
:events
)
222 (revents :accessor pollitem-revents
:initform
0)))
224 (defmethod initialize-instance :after
((inst pollitem
) &key
)
225 (let ((obj (foreign-alloc 'pollitem
)))
226 (setf (pollitem-raw inst
) obj
)
227 (tg:finalize inst
(lambda () (foreign-free obj
)))))
229 (defun poll (items &optional
(timeout -
1))
230 (let ((len (length items
)))
231 (with-foreign-object (%items
'c-pollitem len
)
233 (let ((item (nth i items
))
234 (%item
(mem-aref %items
'c-pollitem i
)))
235 (with-foreign-slots ((socket fd events revents
) %item pollitem
)
236 (setf socket
(pollitem-socket item
)
237 fd
(pollitem-fd item
)
238 events
(pollitem-events item
)))))
239 (let ((ret (%poll %items len timeout
)))
243 (loop for i below len
244 for revent
= (foreign-slot-value (mem-aref %items
'c-pollitem i
)
247 collect
(setf (pollitem-revents (nth i items
)) revent
)))
248 (t (error (convert-from-foreign (%strerror
(errno)) :string
))))))))
250 (defmacro with-polls
(list &body body
)
251 `(let ,(loop for
(name . polls
) in list
254 ,@(loop for
(socket . events
) in polls
255 collect
`(make-instance 'pollitem