def __init__(self, address, connect_timeout, socket_options,
                 connection_closed_callback, message_callback):
        Connection.__init__(self, address, connection_closed_callback,
                            message_callback)

        self._write_lock = threading.Lock()
        self._socket = gevent.socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.settimeout(connect_timeout)

        # set tcp no delay
        self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        # set socket buffer
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                                BUFFER_SIZE)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF,
                                BUFFER_SIZE)

        for socket_option in socket_options:
            self._socket.setsockopt(socket_option.level, socket_option.option,
                                    socket_option.value)

        self._socket.connect(self._address)
        self.logger.debug("Connected to %s", self._address)

        # Set no timeout as we use seperate greenlets to handle heartbeat timeouts, etc
        self._socket.settimeout(None)

        self.write("CB2")
        self.sent_protocol_bytes = True

        self._read_thread = gevent.spawn(self._read_loop)
示例#2
0
    def __init__(self, reactor, connection_manager, connection_id, address,
                 config, message_callback):
        asyncore.dispatcher.__init__(self, map=reactor.map)
        Connection.__init__(self, connection_manager, connection_id,
                            message_callback)

        self._reactor = reactor
        self.connected_address = address
        self._write_queue = deque()
        self._write_buf = io.BytesIO()

        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        # set the socket timeout to 0 explicitly
        self.socket.settimeout(0)
        self._set_socket_options(config)
        if config.ssl_enabled:
            self._wrap_as_ssl_socket(config)

        try:
            self.connect((address.host, address.port))
        except socket.error as e:
            # If the connection attempt failed
            # immediately, remove the connection from
            # the dispatchers map and clean resources.
            self._inner_close()
            raise e

        timeout = config.connection_timeout
        if timeout > 0:
            self._close_timer = reactor.add_timer(timeout,
                                                  self._close_timer_cb)

        self.local_address = Address(*self.socket.getsockname())
        self._write_queue.append(b"CP2")
示例#3
0
    def __init__(self, map, address, connect_timeout, socket_options,
                 connection_closed_callback, message_callback):
        asyncore.dispatcher.__init__(self, map=map)
        Connection.__init__(self, address, connection_closed_callback,
                            message_callback)

        self._write_lock = threading.Lock()
        self._write_queue = deque()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(connect_timeout)

        # set tcp no delay
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        # set socket buffer
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                               BUFFER_SIZE)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF,
                               BUFFER_SIZE)

        for socket_option in socket_options:
            self.socket.setsockopt(socket_option.level, socket_option.option,
                                   socket_option.value)

        self.connect(self._address)

        # the socket should be non-blocking from now on
        self.socket.settimeout(0)

        self._write_queue.append("CB2")
    def __init__(self, reactor, connection_manager, connection_id, address,
                 config, message_callback):
        asyncore.dispatcher.__init__(self, map=reactor.map)
        Connection.__init__(self, connection_manager, connection_id,
                            message_callback)

        self._reactor = reactor
        self.connected_address = address
        self._write_queue = deque()
        self._write_buf = io.BytesIO()

        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        # set the socket timeout to 0 explicitly
        self.socket.settimeout(0)
        self._set_socket_options(config)
        if config.ssl_enabled:
            self._wrap_as_ssl_socket(config)

        self.connect((address.host, address.port))

        timeout = config.connection_timeout
        if timeout > 0:
            self._close_timer = reactor.add_timer(timeout,
                                                  self._close_timer_cb)

        self.local_address = Address(*self.socket.getsockname())
        self._write_queue.append(b"CP2")
    def __init__(self, map, address, connection_closed_callback, message_callback):
        asyncore.dispatcher.__init__(self, map=map)
        Connection.__init__(self, address, connection_closed_callback, message_callback)

        self._write_queue = deque()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect(self._address)
        self.write("CB2")
    def __init__(self, map, address, connection_closed_callback, message_callback):
        asyncore.dispatcher.__init__(self, map=map)
        Connection.__init__(self, address, connection_closed_callback, message_callback)

        self._write_lock = threading.Lock()
        self._write_queue = deque()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect(self._address)
        self._write_queue.append("CB2")
    def __init__(self, map, address, connect_timeout, socket_options, connection_closed_callback, message_callback):
        asyncore.dispatcher.__init__(self, map=map)
        Connection.__init__(self, address, connection_closed_callback, message_callback)

        self._write_lock = threading.Lock()
        self._write_queue = deque()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(connect_timeout)

        # set tcp no delay
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        # set socket buffer
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, BUFFER_SIZE)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFFER_SIZE)

        for socket_option in socket_options:
            self.socket.setsockopt(socket_option.level, socket_option.option, socket_option.value)

        self.connect(self._address)

        # the socket should be non-blocking from now on
        self.socket.settimeout(0)

        self._write_queue.append("CB2")
示例#8
0
    def __init__(self, map, address, connect_timeout, socket_options,
                 connection_closed_callback, message_callback, network_config):
        asyncore.dispatcher.__init__(self, map=map)
        Connection.__init__(self, address, connection_closed_callback,
                            message_callback)

        self._write_lock = threading.Lock()
        self._write_queue = deque()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(connect_timeout)

        # set tcp no delay
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        # set socket buffer
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                               BUFFER_SIZE)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF,
                               BUFFER_SIZE)

        for socket_option in socket_options:
            self.socket.setsockopt(socket_option.level, socket_option.option,
                                   socket_option.value)

        self.connect(self._address)

        ssl_config = network_config.ssl_config
        if ssl and ssl_config.enabled:
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

            protocol = ssl_config.protocol

            # Use only the configured protocol
            try:
                if protocol != PROTOCOL.SSLv2:
                    ssl_context.options |= ssl.OP_NO_SSLv2
                if protocol != PROTOCOL.SSLv3 and protocol != PROTOCOL.SSL:
                    ssl_context.options |= ssl.OP_NO_SSLv3
                if protocol != PROTOCOL.TLSv1:
                    ssl_context.options |= ssl.OP_NO_TLSv1
                if protocol != PROTOCOL.TLSv1_1:
                    ssl_context.options |= ssl.OP_NO_TLSv1_1
                if protocol != PROTOCOL.TLSv1_2 and protocol != PROTOCOL.TLS:
                    ssl_context.options |= ssl.OP_NO_TLSv1_2
                if protocol != PROTOCOL.TLSv1_3:
                    ssl_context.options |= ssl.OP_NO_TLSv1_3
            except AttributeError:
                pass

            ssl_context.verify_mode = ssl.CERT_REQUIRED

            if ssl_config.cafile:
                ssl_context.load_verify_locations(ssl_config.cafile)
            else:
                ssl_context.load_default_certs()

            if ssl_config.certfile:
                ssl_context.load_cert_chain(ssl_config.certfile,
                                            ssl_config.keyfile,
                                            ssl_config.password)

            if ssl_config.ciphers:
                ssl_context.set_ciphers(ssl_config.ciphers)

            self.socket = ssl_context.wrap_socket(self.socket)

        # the socket should be non-blocking from now on
        self.socket.settimeout(0)

        self._write_queue.append(b"CB2")
    def __init__(self, dispatcher_map, connection_manager, connection_id,
                 address, network_config, message_callback, logger_extras):
        asyncore.dispatcher.__init__(self, map=dispatcher_map)
        Connection.__init__(self, connection_manager, connection_id,
                            message_callback, logger_extras)
        self.connected_address = address

        self._write_lock = threading.Lock()
        self._write_queue = deque()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

        timeout = network_config.connection_timeout
        if not timeout:
            timeout = six.MAXSIZE

        self.socket.settimeout(timeout)

        # set tcp no delay
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        # set socket buffer
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                               _BUFFER_SIZE)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF,
                               _BUFFER_SIZE)

        for socket_option in network_config.socket_options:
            if socket_option.option is socket.SO_RCVBUF:
                self.read_buffer_size = socket_option.value

            self.socket.setsockopt(socket_option.level, socket_option.option,
                                   socket_option.value)

        self.connect((address.host, address.port))

        ssl_config = network_config.ssl
        if ssl and ssl_config.enabled:
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

            protocol = ssl_config.protocol

            # Use only the configured protocol
            try:
                if protocol != PROTOCOL.SSLv2:
                    ssl_context.options |= ssl.OP_NO_SSLv2
                if protocol != PROTOCOL.SSLv3 and protocol != PROTOCOL.SSL:
                    ssl_context.options |= ssl.OP_NO_SSLv3
                if protocol != PROTOCOL.TLSv1:
                    ssl_context.options |= ssl.OP_NO_TLSv1
                if protocol != PROTOCOL.TLSv1_1:
                    ssl_context.options |= ssl.OP_NO_TLSv1_1
                if protocol != PROTOCOL.TLSv1_2 and protocol != PROTOCOL.TLS:
                    ssl_context.options |= ssl.OP_NO_TLSv1_2
                if protocol != PROTOCOL.TLSv1_3:
                    ssl_context.options |= ssl.OP_NO_TLSv1_3
            except AttributeError:
                pass

            ssl_context.verify_mode = ssl.CERT_REQUIRED

            if ssl_config.cafile:
                ssl_context.load_verify_locations(ssl_config.cafile)
            else:
                ssl_context.load_default_certs()

            if ssl_config.certfile:
                ssl_context.load_cert_chain(ssl_config.certfile,
                                            ssl_config.keyfile,
                                            ssl_config.password)

            if ssl_config.ciphers:
                ssl_context.set_ciphers(ssl_config.ciphers)

            self.socket = ssl_context.wrap_socket(self.socket)

        # the socket should be non-blocking from now on
        self.socket.settimeout(0)

        self.local_address = Address(*self.socket.getsockname())

        self._write_queue.append(b"CP2")
示例#10
0
    def __init__(self, reactor, connection_manager, connection_id, address,
                 config, message_callback):
        asyncore.dispatcher.__init__(self, map=reactor.map)
        Connection.__init__(self, connection_manager, connection_id,
                            message_callback)

        self._reactor = reactor
        self.connected_address = address
        self._write_queue = deque()
        self._write_buf = io.BytesIO()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

        timeout = config.connection_timeout
        if not timeout:
            timeout = six.MAXSIZE

        self.socket.settimeout(timeout)

        # set tcp no delay
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        # set socket buffer
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                               _BUFFER_SIZE)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF,
                               _BUFFER_SIZE)

        for level, option_name, value in config.socket_options:
            if option_name is socket.SO_RCVBUF:
                self.receive_buffer_size = value
            elif option_name is socket.SO_SNDBUF:
                self.send_buffer_size = value

            self.socket.setsockopt(level, option_name, value)

        self.connect((address.host, address.port))

        if ssl and config.ssl_enabled:
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

            protocol = config.ssl_protocol

            # Use only the configured protocol
            try:
                if protocol != SSLProtocol.SSLv2:
                    ssl_context.options |= ssl.OP_NO_SSLv2
                if protocol != SSLProtocol.SSLv3:
                    ssl_context.options |= ssl.OP_NO_SSLv3
                if protocol != SSLProtocol.TLSv1:
                    ssl_context.options |= ssl.OP_NO_TLSv1
                if protocol != SSLProtocol.TLSv1_1:
                    ssl_context.options |= ssl.OP_NO_TLSv1_1
                if protocol != SSLProtocol.TLSv1_2:
                    ssl_context.options |= ssl.OP_NO_TLSv1_2
                if protocol != SSLProtocol.TLSv1_3:
                    ssl_context.options |= ssl.OP_NO_TLSv1_3
            except AttributeError:
                pass

            ssl_context.verify_mode = ssl.CERT_REQUIRED

            if config.ssl_cafile:
                ssl_context.load_verify_locations(config.ssl_cafile)
            else:
                ssl_context.load_default_certs()

            if config.ssl_certfile:
                ssl_context.load_cert_chain(config.ssl_certfile,
                                            config.ssl_keyfile,
                                            config.ssl_password)

            if config.ssl_ciphers:
                ssl_context.set_ciphers(config.ssl_ciphers)

            self.socket = ssl_context.wrap_socket(self.socket)

        # the socket should be non-blocking from now on
        self.socket.settimeout(0)

        self.local_address = Address(*self.socket.getsockname())

        self._write_queue.append(b"CP2")