예제 #1
0
 def process(self, event):
     type_id = event.type_id()
     if type_id == LinkEventType.HANDSHAKE_REQ:
         response = None
         try:
             response = self.buffer_transform.handshake(event.data)
         except Exception as ex:
             Trace.error("{} error handshaking {}", self.link.name, ex)
         self.session.send(HandshakeResp().setattrs(_transform=False,
                                                    data=response))
     elif type_id == LinkEventType.HANDSHAKE_RESP:
         result = False
         try:
             result = self.buffer_transform.fini_handshake(event.data)
         except Exception as ex:
             Trace.error("{} error finishing handshake {}", self.link.name,
                         ex)
         if result:
             self.rx_transform_ready = True
         self.session.send(HandshakeAck().setattrs(_transform=False,
                                                   result=result))
     elif type_id == LinkEventType.HANDSHAKE_ACK:
         result = event.result
         if result:
             self.tx_transform_ready = True
         self.session.link.on_connect(result, self.session)
     else:
         return False
     return True
예제 #2
0
 def _on_connect(self, result, context):
     super(TcpClient, self)._on_connect(result, context)
     if result:
         peername = context.transport.get_extra_info('peername')
         Trace.info("connected to {}:{}", peername[0], peername[1])
     else:
         Trace.error("error connecting to {}:{}", self.remote_host,
                     self.remote_port)
예제 #3
0
 def handle_error(self):
     err = self.dispatcher.socket.getsockopt(socket.SOL_SOCKET,
                                             socket.SO_ERROR)
     if self.connecting:
         Trace.error("connect error {}", errno.errorcode[err])
         self.connecting = False
         return
     Trace.error("error {}", errno.errorcode[err])
예제 #4
0
 def _on_connect(self, result, context):
     super(TcpClient, self)._on_connect(result, context)
     if result:
         peername = context.socket.getpeername()
         Trace.info("connected to {}:{}", peername[0], peername[1])
         context.peername = peername
     else:
         Trace.error("error connecting to {}:{}", self.remote_host,
                     self.remote_port)
예제 #5
0
    def on_receive(self, data):
        if self.has_heartbeat_strategy:
            self.heartbeat_strategy.on_receive()

        self.rx_buffer += data

        deserializer = Deserializer()
        while True:
            deserializer.buffer = self.rx_buffer
            deserializer.pos = 0

            num_bytes, length, transformed = self.parse_header(deserializer)
            if num_bytes == 0:
                return

            if len(self.rx_buffer) < (length + num_bytes):
                return
            buffer = self.rx_buffer[num_bytes:num_bytes + length]
            self.rx_buffer = self.rx_buffer[num_bytes + length:]

            if self.has_channel_strategy and transformed:
                try:
                    buffer = self.channel_strategy.after_receive(buffer)
                except Exception as ex:
                    Trace.error("{} inverse transform error {}",
                                self.link.name, ex)
                    continue

            deserializer.buffer = buffer
            deserializer.pos = 0

            type_id = deserializer.read_int32(None)

            event = self.link.create_event(type_id)
            if event is None:
                Trace.Error("unknown event type id {}", type_id)
                continue
            event.deserialize(deserializer)

            Trace.debug("{} received {}", self.link.name, event)

            processed = False

            if self.has_channel_strategy:
                processed = self.channel_strategy.process(event)
            elif not processed and self.has_heartbeat_strategy:
                processed = self.heartbeat_strategy.process(event)
            elif not processed:
                processed = self._process(event)

            if not processed:
                event._handle = self.handle
                Hub.post(event)
예제 #6
0
    def on_timeout(self, e):
        Flow.unbind_with(self.handler_token)
        Flow.unbind_with(self.timeout_token)

        wait_handle = self.handler_token[0]._wait_handle
        if wait_handle:
            WaitHandlePool.release(wait_handle)

        Trace.error("WaitForEvent timeout for {}".format(
            self.handler_token[0]))

        self.coroutine.result = None
        self.coroutine.next()
예제 #7
0
파일: flow.py 프로젝트: jaykang920/x2py
    def dispatch(self, event):
        event_proxy = Flow.thread_local.event_proxy
        handler_chain = Flow.thread_local.handler_chain

        if len(handler_chain) != 0:
            del handler_chain[:]

        self.binding.build_handler_chain(event, event_proxy, handler_chain)

        for handler in handler_chain:
            try:
                handler(event)
            except Exception as ex:
                Trace.error("flow: dispatch {}".format(ex))
                traceback.print_exc(file=sys.stderr)

        del handler_chain[:]