# -*- coding: utf-8 -*-
"""
Low-level ctypes binding for the ZeroMQ library.
Makes an attempt to emulate pyzmq.core.
"""
# Copyright © 2011 Daniel Holth
#
# Derived from original pyzmq © 2010 Brian Granger
#
# This file is part of pyzmq-ctypes
#
# pyzmq-ctypes is free software; you can redistribute it and/or modify it
# under the terms of the Lesser GNU General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# pyzmq-ctypes is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
# License for more details.
#
# You should have received a copy of the Lesser GNU General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
import random
from ctypes import *
from ctypes.util import find_library
from ctypes_configure import configure
class CConfigure(object):
_compilation_info_ = configure.ExternalCompilationInfo(
includes = ['zmq.h'],
libraries = ['zmq']
)
size_t = configure.SimpleType('size_t', c_long)
for cname in ['ZMQ_AFFINITY', 'ZMQ_DOWNSTREAM', 'EADDRINUSE',
'EADDRNOTAVAIL', 'EAGAIN', 'ECONNREFUSED', 'EFAULT', 'EFSM',
'EINPROGRESS', 'EINVAL', 'EMTHREAD', 'ENETDOWN', 'ENOBUFS',
'ENOCOMPATPROTO', 'ENODEV', 'ENOMEM', 'ENOTSUP', 'EPROTONOSUPPORT',
'ETERM', 'ZMQ_FORWARDER', 'ZMQ_HWM', 'ZMQ_IDENTITY', 'ZMQ_MCAST_LOOP',
'ZMQ_NOBLOCK', 'ZMQ_PAIR', 'ZMQ_POLLERR', 'ZMQ_POLLIN', 'ZMQ_POLLOUT',
'ZMQ_PUB', 'ZMQ_PULL', 'ZMQ_PUSH', 'ZMQ_QUEUE', 'ZMQ_RATE', 'ZMQ_RCVBUF',
'ZMQ_RCVMORE', 'ZMQ_RECOVERY_IVL', 'ZMQ_REP', 'ZMQ_REQ', 'ZMQ_SNDBUF',
'ZMQ_SNDMORE', 'ZMQ_STREAMER', 'ZMQ_SUB', 'ZMQ_SUBSCRIBE', 'ZMQ_SWAP',
'ZMQ_UNSUBSCRIBE', 'ZMQ_UPSTREAM', 'ZMQ_XREP', 'ZMQ_XREQ', 'ZMQ_MAX_VSM_SIZE',
'ZMQ_FD', 'ZMQ_EVENTS', 'ZMQ_TYPE', 'ZMQ_LINGER', 'ZMQ_RECONNECT_IVL',
'ZMQ_BACKLOG', 'ZMQ_DEALER', 'ZMQ_ROUTER']:
pyname = cname.split('_', 1)[-1]
setattr(CConfigure, pyname, configure.ConstantInteger(cname))
info = configure.configure(CConfigure)
globals().update(info)
# collections of sockopts, based on type:
bytes_sockopts = [SUBSCRIBE, UNSUBSCRIBE, IDENTITY]
int64_sockopts = [HWM, SWAP, AFFINITY, RATE, RECOVERY_IVL,
MCAST_LOOP, SNDBUF, RCVBUF, RCVMORE]
int_sockopts = [FD, EVENTS, TYPE, LINGER, RECONNECT_IVL, BACKLOG]
class ZMQBaseError(Exception): pass
class ZMQError(ZMQBaseError):
def __init__(self, errno=None):
if errno is None:
errno = get_errno()
self.strerror = zmq_strerror(errno)
self.errno = errno
def __str__(self):
return self.strerror
def _check_nonzero(result, func, arguments):
if result == -1:
raise ZMQError(get_errno())
return result
def _check_not_null(result, func, arguments):
if result is None:
raise ZMQError(get_errno())
return result
def _check_zmq_errno(result, func, arguments):
errno = get_errno()
if errno != 0:
raise ZMQError(errno)
return result
libzmq = CDLL(find_library("zmq"), use_errno=True)
libzmq.zmq_version.restype = None
libzmq.zmq_version.argtypes = [POINTER(c_int)]*3
major = c_int()
minor = c_int()
patch = c_int()
libzmq.zmq_version(byref(major), byref(minor), byref(patch))
__zmq_version__ = tuple((x.value for x in (major, minor, patch)))
def zmq_version():
return '.'.join(map(str, __zmq_version__))
memmove.restype = c_void_p
# Error number as known by the 0MQ library
libzmq.zmq_errno.argtypes = []
libzmq.zmq_strerror.restype = c_char_p
libzmq.zmq_strerror.argtypes = [c_int]
# 0MQ infrastructure
libzmq.zmq_init.restype = c_void_p
libzmq.zmq_init.argtypes = [c_int]
libzmq.zmq_term.restype = c_int # the default
libzmq.zmq_term.argtypes = [c_void_p]
# 0MQ message definition
class zmq_msg_t(Structure):
_fields_ = [
('content', c_void_p),
('flags', c_ubyte),
('vsm_size', c_ubyte),
('vsm_data', c_ubyte*MAX_VSM_SIZE)
]
libzmq.zmq_msg_init.argtypes = [POINTER(zmq_msg_t)]
libzmq.zmq_msg_init.restype = c_int
libzmq.zmq_msg_init_size.restype = c_int
libzmq.zmq_msg_init_size.argtypes = [POINTER(zmq_msg_t), size_t]
# requires a free function:
libzmq.zmq_msg_init_data.restype = c_int
libzmq.zmq_msg_init_data.argtypes = [POINTER(zmq_msg_t), c_void_p, size_t,
c_void_p, c_void_p]
libzmq.zmq_msg_close.restype = c_int
libzmq.zmq_msg_close.argtypes = [POINTER(zmq_msg_t)]
libzmq.zmq_msg_move.argtypes = [POINTER(zmq_msg_t), POINTER(zmq_msg_t)]
libzmq.zmq_msg_copy.argtypes = [POINTER(zmq_msg_t), POINTER(zmq_msg_t)]
libzmq.zmq_msg_data.restype = c_void_p
libzmq.zmq_msg_data.argtypes = [POINTER(zmq_msg_t)]
libzmq.zmq_msg_size.restype = size_t
libzmq.zmq_msg_size.argtypes = [POINTER(zmq_msg_t)]
# 0MQ socket definition
libzmq.zmq_socket.restype = c_void_p
libzmq.zmq_socket.argtypes = [c_void_p, c_int]
libzmq.zmq_socket.errcheck = _check_not_null
libzmq.zmq_close.restype = c_int
libzmq.zmq_close.argtypes = [c_void_p]
libzmq.zmq_setsockopt.restype = c_int
libzmq.zmq_setsockopt.argtypes = [c_void_p, c_int, c_void_p, size_t]
libzmq.zmq_getsockopt.restype = c_int
libzmq.zmq_getsockopt.argtypes = [c_void_p, c_int, c_void_p, POINTER(size_t)]
libzmq.zmq_bind.restype = c_int
libzmq.zmq_bind.argtypes = [c_void_p, c_char_p]
libzmq.zmq_connect.restype = c_int
libzmq.zmq_connect.argtypes = [c_void_p, c_char_p]
libzmq.zmq_send.restype = c_int
libzmq.zmq_send.argtypes = [c_void_p, POINTER(zmq_msg_t), c_int]
libzmq.zmq_recv.restype = c_int
libzmq.zmq_recv.argtypes = [c_void_p, POINTER(zmq_msg_t), c_int]
class zmq_pollitem_t(Structure):
_fields_ = [
('socket', c_void_p),
('fd', c_int),
('events', c_short),
('revents', c_short)
]
libzmq.zmq_poll.restype = c_int
libzmq.zmq_poll.argtypes = [POINTER(zmq_pollitem_t), c_int, c_long]
def _default_errcheck():
for symbol in dir(libzmq):
if symbol.startswith('zmq_'):
fn = getattr(libzmq, symbol)
if fn.errcheck != None:
continue
if fn.restype is c_int:
fn.errcheck = _check_nonzero
elif fn.restype is c_void_p:
fn.errcheck = _check_not_null
def _shortcuts():
for symbol in dir(libzmq):
if symbol.startswith('zmq_') and not symbol in globals():
fn = getattr(libzmq, symbol)
globals()[symbol] = fn
_default_errcheck()
_shortcuts()
# Higher-level interface. Partially copied from pyzmq.
class Context(object):
def __init__(self, io_threads=1):
"""The io_threads argument specifies the size of the ØMQ thread pool to
handle I/O operations. If your application is using only the inproc
transport for messaging you may set this to zero, otherwise set it to
at least one."""
if not io_threads > 0:
raise ZMQError(EINVAL)
self.handle = zmq_init(io_threads)
self.closed = False
def socket(self, kind):
if self.closed:
raise ZMQError(ENOTSUP)
return Socket(self, kind)
def term(self):
rc = zmq_term(self.handle)
self.handle = None
self.closed = True
return rc
class Socket(object):
def __init__(self, context, socket_type):
self.context = context
self.handle = zmq_socket(context.handle, socket_type)
self.socket_type = socket_type
self.closed = False
def _check_closed(self):
if self.closed:
raise ZMQError(ENOTSUP)
def close(self):
zmq_close(self.handle)
self.handle = None
self.closed = True
def bind(self, addr):
if isinstance(add
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共9个文件
py:8个
pkg-info:1个
资源分类:Python库 所属语言:Python 资源全名:pyzmq-ctypes-2.1.10.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
pyzmq-ctypes-2.1.10.tar.gz (9个子文件)
pyzmq-ctypes-2.1.10
PKG-INFO 865B
zmq
core
constants.py 29B
socket.py 29B
__init__.py 29B
context.py 29B
error.py 29B
_zmq.py 20KB
__init__.py 19B
setup.py 2KB
共 9 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功