Ejemplo n.º 1
0
 def set(self, option, value):
     """set a context option
     
     see zmq_ctx_set
     """
     rc = C.zmq_ctx_set(self._zmq_ctx, option, value)
     _check_rc(rc)
Ejemplo n.º 2
0
    def set(self, option, value):
        """set a context option

        see zmq_ctx_set
        """
        rc = C.zmq_ctx_set(self._zmq_ctx, option, value)
        _check_rc(rc)
Ejemplo n.º 3
0
def zmq_poll(sockets, timeout):
    cffi_pollitem_list = []
    low_level_to_socket_obj = {}
    for item in sockets:
        if isinstance(item[0], int):
            low_level_to_socket_obj[item[0]] = item
            cffi_pollitem_list.append(
                _make_zmq_pollitem_fromfd(item[0], item[1]))
        else:
            low_level_to_socket_obj[item[0]._zmq_socket] = item
            cffi_pollitem_list.append(_make_zmq_pollitem(item[0], item[1]))
    items = ffi.new('zmq_pollitem_t[]', cffi_pollitem_list)
    list_length = ffi.cast('int', len(cffi_pollitem_list))
    c_timeout = ffi.cast('long', timeout)
    rc = C.zmq_poll(items, list_length, c_timeout)
    _check_rc(rc)
    result = []
    for index in range(len(items)):
        if not items[index].socket == ffi.NULL:
            if items[index].revents > 0:
                result.append((low_level_to_socket_obj[items[index].socket][0],
                               items[index].revents))
        else:
            result.append((items[index].fd, items[index].revents))
    return result
Ejemplo n.º 4
0
def proxy(frontend, backend, capture=None):
    if isinstance(capture, Socket):
        capture = capture._zmq_socket
    else:
        capture = ffi.NULL

    rc = C.zmq_proxy(frontend._zmq_socket, backend._zmq_socket, capture)
    _check_rc(rc)
Ejemplo n.º 5
0
def proxy(frontend, backend, capture=None):
    if isinstance(capture, Socket):
        capture = capture._zmq_socket
    else:
        capture = ffi.NULL

    rc = C.zmq_proxy(frontend._zmq_socket, backend._zmq_socket, capture)
    _check_rc(rc)
Ejemplo n.º 6
0
    def get(self, option):
        """get context option

        see zmq_ctx_get
        """
        rc = C.zmq_ctx_get(self._zmq_ctx, option)
        _check_rc(rc, error_without_errno=False)
        return rc
Ejemplo n.º 7
0
 def get(self, option):
     """get context option
     
     see zmq_ctx_get
     """
     rc = C.zmq_ctx_get(self._zmq_ctx, option)
     _check_rc(rc)
     return rc
Ejemplo n.º 8
0
 def get(self, option):
     """get context option
     
     see zmq_ctx_get
     """
     rc = C.zmq_ctx_get(self._zmq_ctx, option)
     _check_rc(rc)
     return rc
Ejemplo n.º 9
0
def _retry_sys_call(f, *args, **kwargs):
    """make a call, retrying if interrupted with EINTR"""
    while True:
        rc = f(*args)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            continue
        else:
            break
Ejemplo n.º 10
0
 def close(self, linger=None):
     rc = 0
     if not self._closed and hasattr(self, '_zmq_socket'):
         if self._zmq_socket is not None:
             if linger is not None:
                 self.set(zmq.LINGER, linger)
             rc = C.zmq_close(self._zmq_socket)
         self._closed = True
     if rc < 0:
         _check_rc(rc)
Ejemplo n.º 11
0
def _retry_sys_call(f, *args, **kwargs):
    """make a call, retrying if interrupted with EINTR"""
    while True:
        rc = f(*args)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            continue
        else:
            break
Ejemplo n.º 12
0
    def term(self):
        rc = zmq_ctx_destroy(self.handle)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            # ignore interrupted term
            # see PEP 475 notes about close & EINTR for why
            pass

        self.handle = None
        self._closed = True
Ejemplo n.º 13
0
    def close(self, linger = 0):
        if self._closed or self.handle is None:
            return

        if linger is not None:
            self.setsockopt(LINGER, linger)

        rc = zmq_close(self.handle)
        self._closed = True
        self.handle = None
        self.context._rm_socket(self._ref)
        _check_rc(rc)
Ejemplo n.º 14
0
 def close(self, linger=None):
     rc = 0
     if not self._closed and hasattr(self, '_zmq_socket'):
         if self._zmq_socket is not None:
             if linger is not None:
                 self.set(zmq.LINGER, linger)
             rc = C.zmq_close(self._zmq_socket)
         self._closed = True
         if self.context:
             self.context._rm_socket(self._ref)
     if rc < 0:
         _check_rc(rc)
Ejemplo n.º 15
0
def zmq_poll(sockets, timeout):
    cffi_pollitem_list = []
    low_level_to_socket_obj = {}
    from zmq import Socket

    for item in sockets:
        if isinstance(item[0], Socket):
            low_level_to_socket_obj[item[0]._zmq_socket] = item
            cffi_pollitem_list.append(_make_zmq_pollitem(item[0], item[1]))
        else:
            if not isinstance(item[0], int):
                # not an FD, get it from fileno()
                item = (item[0].fileno(), item[1])
            low_level_to_socket_obj[item[0]] = item
            cffi_pollitem_list.append(_make_zmq_pollitem_fromfd(item[0], item[1]))
    items = ffi.new('zmq_pollitem_t[]', cffi_pollitem_list)
    list_length = ffi.cast('int', len(cffi_pollitem_list))
    while True:
        c_timeout = ffi.cast('long', timeout)
        start = monotonic()
        rc = C.zmq_poll(items, list_length, c_timeout)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            if timeout > 0:
                ms_passed = int(1000 * (monotonic() - start))
                if ms_passed < 0:
                    # don't allow negative ms_passed,
                    # which can happen on old Python versions without time.monotonic.
                    warnings.warn(
                        "Negative elapsed time for interrupted poll: %s."
                        "  Did the clock change?" % ms_passed,
                        RuntimeWarning,
                    )
                    ms_passed = 0
                timeout = max(0, timeout - ms_passed)
            continue
        else:
            break
    result = []
    for index in range(len(items)):
        if items[index].revents > 0:
            if not items[index].socket == ffi.NULL:
                result.append(
                    (
                        low_level_to_socket_obj[items[index].socket][0],
                        items[index].revents,
                    )
                )
            else:
                result.append((items[index].fd, items[index].revents))
    return result
Ejemplo n.º 16
0
    def get(self, option):
        c_data = new_pointer_from_opt(option, length=255)

        c_value_pointer = c_data[0]
        c_sizet_pointer = c_data[1]

        rc = C.zmq_getsockopt(self._zmq_socket, option, c_value_pointer, c_sizet_pointer)
        _check_rc(rc)

        sz = c_sizet_pointer[0]
        v = value_from_opt_pointer(option, c_value_pointer, sz)
        if option != zmq.IDENTITY and option in zmq.constants.bytes_sockopts and v.endswith(b"\0"):
            v = v[:-1]
        return v
Ejemplo n.º 17
0
    def _send_copy(self, buf, flags):
        """Send a copy of a bufferable"""
        zmq_msg = ffi.new('zmq_msg_t*')
        if not isinstance(buf, bytes):
            # cast any bufferable data to bytes via memoryview
            buf = memoryview(buf).tobytes()

        c_message = ffi.new('char[]', buf)
        rc = C.zmq_msg_init_size(zmq_msg, len(buf))
        _check_rc(rc)
        C.memcpy(C.zmq_msg_data(zmq_msg), c_message, len(buf))
        _retry_sys_call(C.zmq_msg_send, zmq_msg, self._zmq_socket, flags)
        rc2 = C.zmq_msg_close(zmq_msg)
        _check_rc(rc2)
Ejemplo n.º 18
0
 def bind(self, address):
     rc = C.zmq_bind(self._zmq_socket, address)
     if rc < 0:
         if IPC_PATH_MAX_LEN and C.zmq_errno() == errno_mod.ENAMETOOLONG:
             # py3compat: address is bytes, but msg wants str
             if str is unicode:
                 address = address.decode('utf-8', 'replace')
             path = address.split('://', 1)[-1]
             msg = ('ipc path "{0}" is longer than {1} '
                             'characters (sizeof(sockaddr_un.sun_path)).'
                             .format(path, IPC_PATH_MAX_LEN))
             raise ZMQError(C.zmq_errno(), msg=msg)
         else:
             _check_rc(rc)
Ejemplo n.º 19
0
 def bind(self, address):
     rc = C.zmq_bind(self._zmq_socket, address)
     if rc < 0:
         if IPC_PATH_MAX_LEN and C.zmq_errno() == errno_mod.ENAMETOOLONG:
             # py3compat: address is bytes, but msg wants str
             if str is unicode:
                 address = address.decode('utf-8', 'replace')
             path = address.split('://', 1)[-1]
             msg = ('ipc path "{0}" is longer than {1} '
                    'characters (sizeof(sockaddr_un.sun_path)).'.format(
                        path, IPC_PATH_MAX_LEN))
             raise ZMQError(C.zmq_errno(), msg=msg)
         else:
             _check_rc(rc)
Ejemplo n.º 20
0
    def term(self):
        if self.closed:
            return

        rc = C.zmq_ctx_destroy(self._zmq_ctx)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            # ignore interrupted term
            # see PEP 475 notes about close & EINTR for why
            pass

        self._zmq_ctx = None
        self._closed = True
Ejemplo n.º 21
0
    def term(self):
        if self.closed:
            return
        
        rc = C.zmq_ctx_destroy(self._zmq_ctx)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            # ignore interrupted term
            # see PEP 475 notes about close & EINTR for why
            pass

        self._zmq_ctx = None
        self._closed = True
Ejemplo n.º 22
0
def curve_keypair():
    """generate a Z85 keypair for use with zmq.CURVE security
    
    Requires libzmq (≥ 4.0) to have been linked with libsodium.
    
    Returns
    -------
    (public, secret) : two bytestrings
        The public and private keypair as 40 byte z85-encoded bytestrings.
    """
    public = ffi.new('char[64]')
    private = ffi.new('char[64]')
    rc = C.zmq_curve_keypair(public, private)
    _check_rc(rc)
    return ffi.buffer(public)[:40], ffi.buffer(private)[:40]
Ejemplo n.º 23
0
def curve_keypair():
    """generate a Z85 keypair for use with zmq.CURVE security
    
    Requires libzmq (≥ 4.0) to have been linked with libsodium.
    
    Returns
    -------
    (public, secret) : two bytestrings
        The public and private keypair as 40 byte z85-encoded bytestrings.
    """
    public = ffi.new('char[64]')
    private = ffi.new('char[64]')
    rc = C.zmq_curve_keypair(public, private)
    _check_rc(rc)
    return ffi.buffer(public)[:40], ffi.buffer(private)[:40]
Ejemplo n.º 24
0
def curve_keypair():
    """generate a Z85 keypair for use with zmq.CURVE security

    Requires libzmq (≥ 4.0) to have been built with CURVE support.

    Returns
    -------
    (public, secret) : two bytestrings
        The public and private keypair as 40 byte z85-encoded bytestrings.
    """
    _check_version((3, 2), "curve_keypair")
    public = ffi.new('char[64]')
    private = ffi.new('char[64]')
    rc = C.zmq_curve_keypair(public, private)
    _check_rc(rc)
    return ffi.buffer(public)[:40], ffi.buffer(private)[:40]
Ejemplo n.º 25
0
 def bind(self, address):
     if isinstance(address, unicode):
         address = address.encode("utf8")
     rc = C.zmq_bind(self._zmq_socket, address)
     if rc < 0:
         if IPC_PATH_MAX_LEN and C.zmq_errno() == errno_mod.ENAMETOOLONG:
             # py3compat: address is bytes, but msg wants str
             if str is unicode:
                 address = address.decode("utf-8", "replace")
             path = address.split("://", 1)[-1]
             msg = 'ipc path "{0}" is longer than {1} ' "characters (sizeof(sockaddr_un.sun_path)).".format(
                 path, IPC_PATH_MAX_LEN
             )
             raise ZMQError(C.zmq_errno(), msg=msg)
         else:
             _check_rc(rc)
Ejemplo n.º 26
0
def curve_keypair():
    """generate a Z85 keypair for use with zmq.CURVE security
    
    Requires libzmq (≥ 4.0) to have been built with CURVE support.
    
    Returns
    -------
    (public, secret) : two bytestrings
        The public and private keypair as 40 byte z85-encoded bytestrings.
    """
    _check_version((3,2), "monitor")
    public = ffi.new('char[64]')
    private = ffi.new('char[64]')
    rc = C.zmq_curve_keypair(public, private)
    _check_rc(rc)
    return ffi.buffer(public)[:40], ffi.buffer(private)[:40]
Ejemplo n.º 27
0
    def get(self, option):
        c_data = new_pointer_from_opt(option, length=255)

        c_value_pointer = c_data[0]
        c_sizet_pointer = c_data[1]

        rc = C.zmq_getsockopt(self._zmq_socket, option, c_value_pointer,
                              c_sizet_pointer)
        _check_rc(rc)

        sz = c_sizet_pointer[0]
        v = value_from_opt_pointer(option, c_value_pointer, sz)
        if option != zmq.IDENTITY and option in zmq.constants.bytes_sockopts and v.endswith(
                b'\0'):
            v = v[:-1]
        return v
Ejemplo n.º 28
0
def zmq_poll(sockets, timeout):
    cffi_pollitem_list = []
    low_level_to_socket_obj = {}
    from zmq import Socket
    for item in sockets:
        if isinstance(item[0], Socket):
            low_level_to_socket_obj[item[0]._zmq_socket] = item
            cffi_pollitem_list.append(_make_zmq_pollitem(item[0], item[1]))
        else:
            if not isinstance(item[0], int):
                # not an FD, get it from fileno()
                item = (item[0].fileno(), item[1])
            low_level_to_socket_obj[item[0]] = item
            cffi_pollitem_list.append(_make_zmq_pollitem_fromfd(item[0], item[1]))
    items = ffi.new('zmq_pollitem_t[]', cffi_pollitem_list)
    list_length = ffi.cast('int', len(cffi_pollitem_list))
    while True:
        c_timeout = ffi.cast('long', timeout)
        start = monotonic()
        rc = C.zmq_poll(items, list_length, c_timeout)
        try:
            _check_rc(rc)
        except InterruptedSystemCall:
            if timeout > 0:
                ms_passed = int(1000 * (monotonic() - start))
                if ms_passed < 0:
                    # don't allow negative ms_passed,
                    # which can happen on old Python versions without time.monotonic.
                    warnings.warn(
                        "Negative elapsed time for interrupted poll: %s."
                        "  Did the clock change?" % ms_passed,
                        RuntimeWarning)
                    ms_passed = 0
                timeout = max(0, timeout - ms_passed)
            continue
        else:
            break
    result = []
    for index in range(len(items)):
        if items[index].revents > 0:
            if not items[index].socket == ffi.NULL:
                result.append((low_level_to_socket_obj[items[index].socket][0],
                            items[index].revents))
            else:
                result.append((items[index].fd, items[index].revents))
    return result
Ejemplo n.º 29
0
    def set(self, option, value):
        length = None
        if isinstance(value, unicode):
            raise TypeError("unicode not allowed, use bytes")

        if isinstance(value, bytes):
            if option not in zmq.constants.bytes_sockopts:
                raise TypeError("not a bytes sockopt: %s" % option)
            length = len(value)

        c_data = initialize_opt_pointer(option, value, length)

        c_value_pointer = c_data[0]
        c_sizet = c_data[1]

        rc = C.zmq_setsockopt(self._zmq_socket, option, ffi.cast("void*", c_value_pointer), c_sizet)
        _check_rc(rc)
Ejemplo n.º 30
0
    def recv(self, flags=0, copy=True, track=False):

        zmq_msg = byref(zmq_msg_t())
        zmq_msg_init(zmq_msg)

        try:
            _retry_sys_call(zmq_msg_recv, zmq_msg, self.handle, flags)
        except Exception:
            zmq_msg_close(zmq_msg)
            raise

        data = zmq_msg_data(zmq_msg)
        size = zmq_msg_size(zmq_msg)
        buf = string_at(data, size)
        rc = zmq_msg_close(zmq_msg)
        _check_rc(rc)
        return buf
Ejemplo n.º 31
0
    def recv(self, flags=0, copy=True, track=False):
        zmq_msg = ffi.new('zmq_msg_t*')
        C.zmq_msg_init(zmq_msg)

        rc = C.zmq_msg_recv(zmq_msg, self._zmq_socket, flags)

        if rc < 0:
            C.zmq_msg_close(zmq_msg)
            _check_rc(rc)

        value = ffi.buffer(C.zmq_msg_data(zmq_msg), C.zmq_msg_size(zmq_msg))[:]

        frame = Frame(value, track=track)
        frame.more = self.getsockopt(RCVMORE)
        if copy:
            return frame.bytes
        else:
            return frame
Ejemplo n.º 32
0
    def get(self, option, length=0):
        from zmq.sugar.constants import bytes_sockopts
        if option in bytes_sockopts:
            length = 255

        try:
            low_level_data = new_pointer_from_opt(option, length=length)
        except ValueError:
            raise ZMQError(EINVAL)

        low_level_value_pointer = low_level_data[0]
        low_level_sizet_pointer = low_level_data[1]

        rc = C.zmq_getsockopt(self._zmq_socket, option,
                              low_level_value_pointer, low_level_sizet_pointer)
        _check_rc(rc)

        return value_from_opt_pointer(option, low_level_value_pointer)
Ejemplo n.º 33
0
    def send(self, message, flags=0, copy=False, track=False):
        if isinstance(message, unicode):
            raise TypeError("Message must be in bytes, not an unicode Object")

        if isinstance(message, Frame):
            message = message.bytes

        zmq_msg = ffi.new('zmq_msg_t*')
        c_message = ffi.new('char[]', message)
        rc = C.zmq_msg_init_size(zmq_msg, len(message))
        C.memcpy(C.zmq_msg_data(zmq_msg), c_message, len(message))

        rc = C.zmq_msg_send(zmq_msg, self._zmq_socket, flags)
        C.zmq_msg_close(zmq_msg)
        _check_rc(rc)

        if track:
            return zmq.MessageTracker()
Ejemplo n.º 34
0
    def set(self, option, value):
        length = None
        if isinstance(value, unicode):
            raise TypeError("unicode not allowed, use bytes")

        if isinstance(value, bytes):
            if option not in zmq.constants.bytes_sockopts:
                raise TypeError("not a bytes sockopt: %s" % option)
            length = len(value)

        c_data = initialize_opt_pointer(option, value, length)

        c_value_pointer = c_data[0]
        c_sizet = c_data[1]

        rc = C.zmq_setsockopt(self._zmq_socket, option,
                              ffi.cast('void*', c_value_pointer), c_sizet)
        _check_rc(rc)
Ejemplo n.º 35
0
    def send(self, message, flags=0, copy=False, track=False):
        if isinstance(message, unicode):
            raise TypeError("Message must be in bytes, not an unicode Object")

        if isinstance(message, Frame):
            message = message.bytes

        zmq_msg = ffi.new('zmq_msg_t*')
        c_message = ffi.new('char[]', message)
        rc = C.zmq_msg_init_size(zmq_msg, len(message))
        C.memcpy(C.zmq_msg_data(zmq_msg), c_message, len(message))

        rc = C.zmq_msg_send(zmq_msg, self._zmq_socket, flags)
        C.zmq_msg_close(zmq_msg)
        _check_rc(rc)

        if track:
            return zmq.MessageTracker()
Ejemplo n.º 36
0
    def get(self, option, length=0):
        from zmq.sugar.constants import bytes_sockopts
        if option in bytes_sockopts:
            length = 255

        try:
            low_level_data = new_pointer_from_opt(option, length=length)
        except ValueError:
            raise ZMQError(EINVAL)

        low_level_value_pointer = low_level_data[0]
        low_level_sizet_pointer = low_level_data[1]

        rc = C.zmq_getsockopt(self._zmq_socket,
                               option,
                               low_level_value_pointer,
                               low_level_sizet_pointer)
        _check_rc(rc)

        return value_from_opt_pointer(option, low_level_value_pointer)
Ejemplo n.º 37
0
    def recv(self, flags=0, copy=True, track=False):
        zmq_msg = ffi.new('zmq_msg_t*')
        C.zmq_msg_init(zmq_msg)

        rc = C.zmq_msg_recv(zmq_msg, self._zmq_socket, flags)

        if rc < 0:
            C.zmq_msg_close(zmq_msg)
            _check_rc(rc)

        _buffer = ffi.buffer(C.zmq_msg_data(zmq_msg), C.zmq_msg_size(zmq_msg))
        value = _buffer[:]
        C.zmq_msg_close(zmq_msg)

        frame = Frame(value, track=track)
        frame.more = self.getsockopt(RCVMORE)

        if copy:
            return frame.bytes
        else:
            return frame
Ejemplo n.º 38
0
    def send(self, message, flags=0, copy=False, track=False):
        if isinstance(message, unicode):
            raise TypeError("Message must be in bytes, not an unicode Object")

        if isinstance(message, Frame):
            message = message.bytes

        zmq_msg = ffi.new('zmq_msg_t*')
        if not isinstance(message, bytes):
            # cast any bufferable data to bytes via memoryview
            message = memoryview(message).tobytes()

        c_message = ffi.new('char[]', message)
        rc = C.zmq_msg_init_size(zmq_msg, len(message))
        _check_rc(rc)
        C.memcpy(C.zmq_msg_data(zmq_msg), c_message, len(message))
        _retry_sys_call(C.zmq_msg_send, zmq_msg, self._zmq_socket, flags)
        rc2 = C.zmq_msg_close(zmq_msg)
        _check_rc(rc2)

        if track:
            return zmq.MessageTracker()
Ejemplo n.º 39
0
    def recv(self, flags=0, copy=True, track=False):
        zmq_msg = ffi.new("zmq_msg_t*")
        C.zmq_msg_init(zmq_msg)

        try:
            _retry_sys_call(C.zmq_msg_recv, zmq_msg, self._zmq_socket, flags)
        except Exception:
            C.zmq_msg_close(zmq_msg)
            raise

        _buffer = ffi.buffer(C.zmq_msg_data(zmq_msg), C.zmq_msg_size(zmq_msg))
        value = _buffer[:]
        rc = C.zmq_msg_close(zmq_msg)
        _check_rc(rc)

        frame = Frame(value, track=track)
        frame.more = self.getsockopt(RCVMORE)

        if copy:
            return frame.bytes
        else:
            return frame
Ejemplo n.º 40
0
    def set(self, option, value):
        length = None
        str_value = False

        if isinstance(value, str):
            length = len(value)
            str_value = True

        try:
            low_level_data = initialize_opt_pointer(option, value, length)
        except ValueError:
            if not str_value:
                raise ZMQError(EINVAL)
            raise TypeError("Invalid Option")

        low_level_value_pointer = low_level_data[0]
        low_level_sizet = low_level_data[1]

        rc = C.zmq_setsockopt(self._zmq_socket, option,
                              ffi.cast('void*', low_level_value_pointer),
                              low_level_sizet)
        _check_rc(rc)
Ejemplo n.º 41
0
    def recv(self, flags=0, copy=True, track=False):
        zmq_msg = ffi.new('zmq_msg_t*')
        C.zmq_msg_init(zmq_msg)

        try:
            _retry_sys_call(C.zmq_msg_recv, zmq_msg, self._zmq_socket, flags)
        except Exception:
            C.zmq_msg_close(zmq_msg)
            raise

        _buffer = ffi.buffer(C.zmq_msg_data(zmq_msg), C.zmq_msg_size(zmq_msg))
        value = _buffer[:]
        rc = C.zmq_msg_close(zmq_msg)
        _check_rc(rc)

        frame = Frame(value, track=track)
        frame.more = self.getsockopt(RCVMORE)

        if copy:
            return frame.bytes
        else:
            return frame
Ejemplo n.º 42
0
 def bind(self, address):
     if isinstance(address, unicode):
         address_b = address.encode('utf8')
     else:
         address_b = address
     if isinstance(address, bytes):
         address = address_b.decode('utf8')
     rc = C.zmq_bind(self._zmq_socket, address_b)
     if rc < 0:
         if IPC_PATH_MAX_LEN and C.zmq_errno() == errno_mod.ENAMETOOLONG:
             path = address.split('://', 1)[-1]
             msg = ('ipc path "{0}" is longer than {1} '
                    'characters (sizeof(sockaddr_un.sun_path)).'.format(
                        path, IPC_PATH_MAX_LEN))
             raise ZMQError(C.zmq_errno(), msg=msg)
         elif C.zmq_errno() == errno_mod.ENOENT:
             path = address.split('://', 1)[-1]
             msg = 'No such file or directory for ipc path "{0}".'.format(
                 path)
             raise ZMQError(C.zmq_errno(), msg=msg)
         else:
             _check_rc(rc)
Ejemplo n.º 43
0
def curve_public(private):
    """Compute the public key corresponding to a private key for use
    with zmq.CURVE security

    Requires libzmq (≥ 4.2) to have been built with CURVE support.

    Parameters
    ----------
    private
        The private key as a 40 byte z85-encoded bytestring
    Returns
    -------
    bytestring
        The public key as a 40 byte z85-encoded bytestring.
    """
    if isinstance(private, unicode):
        private = private.encode('utf8')
    _check_version((4, 2), "curve_public")
    public = ffi.new('char[64]')
    rc = C.zmq_curve_public(public, private)
    _check_rc(rc)
    return ffi.buffer(public)[:40]
Ejemplo n.º 44
0
    def set(self, option, value):
        length = None
        str_value = False

        if isinstance(value, str):
            length = len(value)
            str_value = True

        try:
            low_level_data = initialize_opt_pointer(option, value, length)
        except ValueError:
            if not str_value:
                raise ZMQError(EINVAL)
            raise TypeError("Invalid Option")

        low_level_value_pointer = low_level_data[0]
        low_level_sizet = low_level_data[1]

        rc = C.zmq_setsockopt(self._zmq_socket,
                               option,
                               ffi.cast('void*', low_level_value_pointer),
                               low_level_sizet)
        _check_rc(rc)
Ejemplo n.º 45
0
    def recv(self, flags=0, copy=True, track=False):
        if copy:
            zmq_msg = ffi.new('zmq_msg_t*')
            C.zmq_msg_init(zmq_msg)
        else:
            frame = zmq.Frame(track=track)
            zmq_msg = frame.zmq_msg

        try:
            _retry_sys_call(C.zmq_msg_recv, zmq_msg, self._zmq_socket, flags)
        except Exception:
            if copy:
                C.zmq_msg_close(zmq_msg)
            raise

        if not copy:
            return frame

        _buffer = ffi.buffer(C.zmq_msg_data(zmq_msg), C.zmq_msg_size(zmq_msg))
        _bytes = _buffer[:]
        rc = C.zmq_msg_close(zmq_msg)
        _check_rc(rc)
        return _bytes
Ejemplo n.º 46
0
def zmq_poll(sockets, timeout):
    cffi_pollitem_list = []
    low_level_to_socket_obj = {}
    for item in sockets:
        if isinstance(item[0], int):
            low_level_to_socket_obj[item[0]] = item
            cffi_pollitem_list.append(_make_zmq_pollitem_fromfd(item[0], item[1]))
        else:
            low_level_to_socket_obj[item[0]._zmq_socket] = item
            cffi_pollitem_list.append(_make_zmq_pollitem(item[0], item[1]))
    items = ffi.new('zmq_pollitem_t[]', cffi_pollitem_list)
    list_length = ffi.cast('int', len(cffi_pollitem_list))
    c_timeout = ffi.cast('long', timeout)
    rc = C.zmq_poll(items, list_length, c_timeout)
    _check_rc(rc)
    result = []
    for index in range(len(items)):
        if not items[index].socket == ffi.NULL:
            if items[index].revents > 0:
                result.append((low_level_to_socket_obj[items[index].socket][0],
                            items[index].revents))
        else:
            result.append((items[index].fd, items[index].revents))
    return result
Ejemplo n.º 47
0
 def disconnect(self, address):
     _check_version((3, 2), "disconnect")
     if isinstance(address, unicode):
         address = address.encode('utf8')
     rc = C.zmq_disconnect(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 48
0
 def connect(self, address):
     if isinstance(address, unicode):
         address = address.encode('utf8')
     rc = C.zmq_connect(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 49
0
 def unbind(self, address):
     _check_version((3, 2), "unbind")
     if isinstance(address, unicode):
         address = address.encode('utf8')
     rc = C.zmq_unbind(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 50
0
 def unbind(self, address):
     rc = C.zmq_unbind(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 51
0
 def disconnect(self, address):
     if isinstance(address, unicode):
         address = address.encode('utf8')
     rc = C.zmq_disconnect(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 52
0
 def unbind(self, address):
     if isinstance(address, unicode):
         address = address.encode('utf8')
     rc = C.zmq_unbind(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 53
0
 def unbind(self, address):
     _check_version((3, 2), "unbind")
     if isinstance(address, unicode):
         address = address.encode("utf8")
     rc = C.zmq_unbind(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 54
0
 def disconnect(self, address):
     _check_version((3, 2), "disconnect")
     if isinstance(address, unicode):
         address = address.encode("utf8")
     rc = C.zmq_disconnect(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 55
0
 def disconnect(self, address):
     rc = C.zmq_disconnect(self._zmq_socket, address)
     _check_rc(rc)
Ejemplo n.º 56
0
def device(device_type, frontend, backend):
    rc = C.zmq_proxy(frontend._zmq_socket, backend._zmq_socket, ffi.NULL)
    _check_rc(rc)