License changed to Lisp Lesser GNU Public License.
[cl-zmq.git] / zeromq.lisp
blob1cbfc90e84c26aedbb4a0c810709002c40ede277
1 ;; Copyright (c) 2009, 2010 Vitaly Mayatskikh <v.mayatskih@gmail.com>
2 ;;
3 ;; This file is part of CL-ZMQ.
4 ;;
5 ;; Vitaly Mayatskikh grants you the rights to distribute
6 ;; and use this software as governed by the terms
7 ;; of the Lisp Lesser GNU Public License
8 ;; (http://opensource.franz.com/preamble.html),
9 ;; known as the LLGPL.
11 (in-package :zeromq)
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 ;; 0MQ errors.
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17 (defconstant hausnumero 156384712)
19 ;; Native 0MQ error codes.
20 (defconstant emthread (+ hausnumero 50))
21 (defconstant efsm (+ hausnumero 51))
22 (defconstant enocompatproto (+ hausnumero 52))
24 (defcfun ("zmq_strerror" %strerror) :pointer
25 (errnum :int))
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; 0MQ message definition.
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (defconstant max-vsm-size 30)
33 ;; Message types. These integers may be stored in 'content' member of the
34 ;; message instead of regular pointer to the data.
35 (defconstant delimiter 31)
36 (defconstant vsm 32)
38 ;; Message flags. ZMQ_MSG_SHARED is strictly speaking not a message flag
39 ;; (it has no equivalent in the wire format), however, making it a flag
40 ;; allows us to pack the stucture tigher and thus improve performance.
41 (defconstant msg-more 1)
42 (defconstant msg-shared 128)
44 (defcstruct (msg)
45 (content :pointer)
46 (shared :uchar)
47 (vsm-size :uchar)
48 (vsm-data :uchar :count 30)) ;; FIXME max-vsm-size
50 (defcfun ("zmq_msg_init" msg-init) :int
51 (msg msg))
53 (defcfun* ("zmq_msg_init_size" %msg-init-size) :int
54 (msg msg)
55 (size :long))
57 (defcallback zmq-free :void ((ptr :pointer) (hint :pointer))
58 (declare (ignorable hint))
59 (foreign-free ptr))
61 (defcfun ("zmq_msg_init_data" msg-init-data) :int
62 (msg msg)
63 (data :pointer)
64 (size :long)
65 (ffn :pointer) ; zmq_free_fn
66 (hint :pointer))
68 (defcfun* ("zmq_msg_close" %msg-close) :int
69 (msg msg))
71 (defcfun ("zmq_msg_move" %msg-move) :int
72 (dest msg)
73 (src msg))
75 (defcfun ("zmq_msg_copy" %msg-copy) :int
76 (dest msg)
77 (src msg))
79 (defcfun ("zmq_msg_data" %msg-data) :pointer
80 (msg msg))
82 (defcfun ("zmq_msg_size" %msg-size) :int
83 (msg msg))
85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 ;; 0MQ infrastructure (a.k.a. context) initialisation & termination.
87 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
89 (defconstant poll 1)
91 (defcfun* ("zmq_init" init) :pointer
92 (app-threads :int)
93 (io-threads :int)
94 (flags :int))
96 (defcfun ("zmq_term" term) :int
97 (context :pointer))
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 ;; 0MQ socket definition.
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 (defconstant p2p 0)
104 (defconstant pub 1)
105 (defconstant sub 2)
106 (defconstant req 3)
107 (defconstant rep 4)
108 (defconstant xreq 5)
109 (defconstant xrep 6)
110 (defconstant upstream 7)
111 (defconstant downstream 8)
113 (defconstant hwm 1)
114 (defconstant lwm 2)
115 (defconstant swap 3)
116 (defconstant affinity 4)
117 (defconstant identity 5)
118 (defconstant subscribe 6)
119 (defconstant unsubscribe 7)
120 (defconstant rate 8)
121 (defconstant recovery-ivl 9)
122 (defconstant mcast-loop 10)
123 (defconstant sndbuf 11)
124 (defconstant rcvbuf 12)
125 (defconstant rcvmore 13)
127 (defconstant noblock 1)
128 (defconstant sndmore 2)
130 (defcfun* ("zmq_socket" socket) :pointer
131 (context :pointer)
132 (type :int))
134 (defcfun ("zmq_close" close) :int
135 (s :pointer))
137 (defcfun* ("zmq_setsockopt" %setsockopt) :int
138 (s :pointer)
139 (option :int)
140 (optval :pointer)
141 (optvallen :long))
143 (defcfun* ("zmq_getsockopt" %getsockopt) :int
144 (s :pointer)
145 (option :int)
146 (optval :pointer)
147 (optvallen :pointer))
149 (defcfun* ("zmq_bind" %bind) :int
150 (s :pointer)
151 (addr :pointer :char))
153 (defcfun* ("zmq_connect" %connect) :int
154 (s :pointer)
155 (addr :pointer :char))
158 (defcfun* ("zmq_send" %send) :int
159 (s :pointer)
160 (msg msg)
161 :optional
162 (flags :int))
164 (defcfun* ("zmq_recv" %recv) :int
165 (s :pointer)
166 (msg msg)
167 :optional
168 (flags :int))
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 ;; I/O multiplexing.
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 (defconstant pollin 1)
175 (defconstant pollout 2)
176 (defconstant pollerr 4)
178 (defcstruct pollitem
179 (socket :pointer)
180 (fd :int)
181 (events :short)
182 (revents :short))
184 (defcfun ("zmq_poll" %poll) :int
185 (items :pointer)
186 (nitems :int)
187 (timeout :long))
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;; Helper functions.
191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 (defcfun ("zmq_stopwatch_start" stopwatch-start) :pointer)
195 (defcfun ("zmq_stopwatch_stop" stopwatch-stop) :ulong
196 (watch :pointer))
198 (defcfun ("zmq_sleep" sleep) :void
199 (seconds :int))
201 (defcfun ("zmq_version" %version) :void
202 (major :pointer)
203 (minor :pointer)
204 (patch :pointer))
206 (defcfun ("zmq_errno" errno) :int)
208 (defcfun* ("zmq_device" %device) :int
209 (device :int)
210 (insocket :pointer)
211 (outsocket :pointer))