27
27
cdef extern from "pyversion_compat.h":
30
from libc.stdlib cimport free, malloc
31
30
from cpython cimport PyBytes_FromStringAndSize
32
31
from cpython cimport PyBytes_AsString, PyBytes_Size
33
32
from cpython cimport Py_DECREF, Py_INCREF
35
34
from buffers cimport asbuffer_r, viewfromobject_r
38
37
from message cimport Message, copy_zmq_msg_bytes
39
from context cimport Context
40
41
cdef extern from "Python.h":
41
42
ctypedef int Py_ssize_t
72
75
# inline some small socket submethods:
73
76
# true methods frequently cannot be inlined, acc. Cython docs
75
cdef inline _check_closed(Socket s):
77
raise ZMQError(ENOTSUP)
78
cdef inline _check_closed(Socket s, bint raise_notsup):
82
cdef size_t sz=sizeof(int)
85
raise ZMQError(ENOTSUP)
89
rc = zmq_getsockopt(s.handle, ZMQ_TYPE, <void *>&stype, &sz)
90
if rc and zmq_errno() == ENOTSOCK:
93
raise ZMQError(ENOTSUP)
79
98
cdef inline Message _recv_message(void *handle, int flags=0, track=False):
80
99
"""Receive a message in a non-copying manner and return a Message."""
156
175
These objects will generally be constructed via the socket() method of a Context object.
177
Note: 0MQ Sockets are *not* threadsafe. **DO NOT** share them across threads.
160
181
context : Context
161
182
The 0MQ Context this Socket belongs to.
162
183
socket_type : int
163
184
The socket type, which can be any of the 0MQ socket types:
164
REQ, REP, PUB, SUB, PAIR, XREQ, XREP, PULL, PUSH, XPUB, XSUB.
185
REQ, REP, PUB, SUB, PAIR, XREQ, DEALER, XREP, ROUTER, PULL, PUSH, XPUB, XSUB.
168
189
.Context.socket : method for creating a socket bound to a Context.
171
def __cinit__(self, object context, int socket_type):
192
def __cinit__(self, Context context, int socket_type, *args, **kwrags):
172
193
cdef Py_ssize_t c_handle
173
194
c_handle = context._handle
179
200
self.handle = zmq_socket(<void *>c_handle, socket_type)
180
201
if self.handle == NULL:
205
context._add_socket(self.handle)
184
207
def __dealloc__(self):
210
def __init__(self, context, socket_type):
215
return _check_closed(self, False)
194
224
garbage collected.
197
if self.handle != NULL and not self.closed:
228
if self.handle != NULL and not self._closed:
199
230
rc = zmq_close(self.handle)
231
if rc != 0 and zmq_errno() != ENOTSOCK:
232
# ignore ENOTSOCK (closed by Context)
234
self.context._remove_socket(self.handle)
202
235
self.handle = NULL
205
238
def setsockopt(self, int option, optval):
206
239
"""s.setsockopt(option, optval)
208
241
Set socket options.
210
See the 0MQ documentation for details on specific options.
243
See the 0MQ API documentation for details on specific options.
215
The name of the option to set. Can be any of: SUBSCRIBE,
216
UNSUBSCRIBE, IDENTITY, HWM, SWAP, AFFINITY, RATE,
217
RECOVERY_IVL, MCAST_LOOP, SNDBUF, RCVBUF.
248
The option to set. Available values will depend on your
249
version of libzmq. Examples include:
250
zmq.SUBSCRIBE, UNSUBSCRIBE, IDENTITY, HWM, LINGER, FD
251
optval : int or bytes
219
252
The value of the option to set.
221
254
cdef int64_t optval_int64_c
224
257
cdef char* optval_c
225
258
cdef Py_ssize_t sz
260
_check_closed(self, True)
228
261
if isinstance(optval, unicode):
229
262
raise TypeError("unicode not allowed, use setsockopt_unicode")
231
264
if option in constants.bytes_sockopts:
232
265
if not isinstance(optval, bytes):
233
raise TypeError('expected str, got: %r' % optval)
266
raise TypeError('expected bytes, got: %r' % optval)
234
267
optval_c = PyBytes_AsString(optval)
235
268
sz = PyBytes_Size(optval)
268
301
Get the value of a socket option.
270
See the 0MQ documentation for details on specific options.
303
See the 0MQ API documentation for details on specific options.
275
The name of the option to set. Can be any of:
276
IDENTITY, HWM, SWAP, AFFINITY, RATE,
277
RECOVERY_IVL, MCAST_LOOP, SNDBUF, RCVBUF, RCVMORE.
308
The option to set. Available values will depend on your
309
version of libzmq. Examples include:
310
zmq.SUBSCRIBE, UNSUBSCRIBE, IDENTITY, HWM, LINGER, FD
282
The value of the option as a string or int.
314
optval : int or bytes
315
The value of the option as a bytestring or int.
284
317
cdef int64_t optval_int64_c
285
318
cdef int optval_int_c
319
cdef fd_t optval_fd_c
286
320
cdef char identity_str_c [255]
324
_check_closed(self, True)
292
326
if option in constants.bytes_sockopts:
357
398
The value of the option as a unicode string.
359
if option not in [IDENTITY]:
401
if option not in [ZMQ_IDENTITY]:
360
402
raise TypeError("option %i will not return a string to be decoded"%option)
361
403
return self.getsockopt(option).decode(encoding)
405
def __setattr__(self, key, value):
406
"""set sockopts by attr"""
409
opt = getattr(constants, key.upper())
410
except AttributeError:
411
# allow subclasses to have extended attributes
412
if self.__class__.__module__ != 'zmq.core.socket':
413
self._attrs[key] = value
415
raise AttributeError("Socket has no such option: %s"%key.upper())
417
self.setsockopt(opt, value)
419
def __getattr__(self, key):
420
"""set sockopts by attr"""
421
if key in self._attrs:
422
# `key` is subclass extended attribute
423
return self._attrs[key]
426
opt = getattr(constants, key)
427
except AttributeError:
428
raise AttributeError("Socket has no such option: %s"%key)
430
return self.getsockopt(opt)
363
432
def bind(self, addr):
451
_check_closed(self, True)
383
452
if isinstance(addr, unicode):
384
453
addr = addr.encode('utf-8')
385
454
if not isinstance(addr, bytes):
512
_check_closed(self, True)
444
513
if isinstance(addr, unicode):
445
514
addr = addr.encode('utf-8')
446
515
if not isinstance(addr, bytes):
491
560
If the send does not succeed for any reason.
563
_check_closed(self, True)
496
565
if isinstance(data, unicode):
497
566
raise TypeError("unicode not allowed, use send_unicode")