コード例 #1
0
ファイル: puback.py プロジェクト: baiyongrui/FullyConnect
 def __init__(self, fixed: MQTTFixedHeader=None, variable_header: PacketIdVariableHeader=None):
     if fixed is None:
         header = MQTTFixedHeader(PUBACK, 0x00)
     else:
         if fixed.packet_type is not PUBACK:
             raise fullyconnectException("Invalid fixed packet type %s for PubackPacket init" % fixed.packet_type)
         header = fixed
     super().__init__(header)
     self.variable_header = variable_header
     self.payload = None
コード例 #2
0
ファイル: disconnect.py プロジェクト: baiyongrui/FullyConnect
 def __init__(self, fixed: MQTTFixedHeader = None):
     if fixed is None:
         header = MQTTFixedHeader(DISCONNECT, 0x00)
     else:
         if fixed.packet_type is not DISCONNECT:
             raise fullyconnectException(
                 "Invalid fixed packet type %s for DisconnectPacket init" %
                 fixed.packet_type)
         header = fixed
     super().__init__(header)
     self.variable_header = None
     self.payload = None
コード例 #3
0
ファイル: pingresp.py プロジェクト: baiyongrui/FullyConnect
 def __init__(self, fixed: MQTTFixedHeader = None):
     if fixed is None:
         header = MQTTFixedHeader(PINGRESP, 0x00)
     else:
         if fixed.packet_type is not PINGRESP:
             raise fullyconnectException(
                 "Invalid fixed packet type %s for PingRespPacket init" %
                 fixed.packet_type)
         header = fixed
     super().__init__(header)
     self.variable_header = None
     self.payload = None
コード例 #4
0
    def __init__(self,
                 fixed: MQTTFixedHeader = None,
                 variable_header: PacketIdVariableHeader = None,
                 payload=None):
        if fixed is None:
            header = MQTTFixedHeader(SUBSCRIBE, 0x02)  # [MQTT-3.8.1-1]
        else:
            if fixed.packet_type is not SUBSCRIBE:
                raise fullyconnectException(
                    "Invalid fixed packet type %s for SubscribePacket init" %
                    fixed.packet_type)
            header = fixed

        super().__init__(header)
        self.variable_header = variable_header
        self.payload = payload
コード例 #5
0
    def _reader_loop(self):
        running_tasks = collections.deque()
        while True:
            try:
                self._reader_ready.set()
                while running_tasks and running_tasks[0].done():
                    running_tasks.popleft()
                if len(running_tasks) > 1:
                    logging.debug("{} Handler running tasks: {}".format(self._peername, len(running_tasks)))

                fixed_header = yield from asyncio.wait_for(
                    MQTTFixedHeader.from_stream(self._reader),
                    self._keepalive_timeout + 10, loop=self._loop)
                if fixed_header:
                    if fixed_header.packet_type == RESERVED_0 or fixed_header.packet_type == RESERVED_15:
                        logging.warning("{} Received reserved packet, which is forbidden: closing connection".format(self._peername))
                        break
                    else:
                        cls = packet_class(fixed_header)
                        packet = yield from cls.from_stream(self._reader, fixed_header=fixed_header)
                        task = None
                        if packet.fixed_header.packet_type == CONNACK:
                            task = ensure_future(self.handle_connack(packet), loop=self._loop)
                        elif packet.fixed_header.packet_type == PINGREQ:
                            task = ensure_future(self.handle_pingreq(packet), loop=self._loop)
                        elif packet.fixed_header.packet_type == PINGRESP:
                            task = ensure_future(self.handle_pingresp(packet), loop=self._loop)
                        elif packet.fixed_header.packet_type == PUBLISH:
                            # task = ensure_future(self.handle_publish(packet), loop=self._loop)
                            self.handle_publish(packet)
                        # elif packet.fixed_header.packet_type == SUBSCRIBE:
                        #     task = ensure_future(self.handle_subscribe(packet), loop=self._loop)
                        # elif packet.fixed_header.packet_type == UNSUBSCRIBE:
                        #     task = ensure_future(self.handle_unsubscribe(packet), loop=self._loop)
                        # elif packet.fixed_header.packet_type == SUBACK:
                        #     task = ensure_future(self.handle_suback(packet), loop=self._loop)
                        # elif packet.fixed_header.packet_type == UNSUBACK:
                        #     task = ensure_future(self.handle_unsuback(packet), loop=self._loop)
                        elif packet.fixed_header.packet_type == DISCONNECT:
                            task = ensure_future(self.handle_disconnect(packet), loop=self._loop)
                        else:
                            logging.warning("{} Unhandled packet type: {}".format(self._peername, packet.fixed_header.packet_type))
                        if task:
                            running_tasks.append(task)
                else:
                    logging.debug("{} No more data (EOF received), stopping reader coro".format(self._peername))
                    break
            except MQTTException:
                logging.debug("{} Message discarded".format(self._peername))
            except asyncio.CancelledError:
                # logger.debug("Task cancelled, reader loop ending")
                break
            except asyncio.TimeoutError:
                logging.debug("{} Input stream read timeout".format(self._peername))
                break
            except NoDataException:
                logging.debug("{} No data available".format(self._peername))
            except BaseException as e:
                logging.warning(
                    "{}:{} Unhandled exception in reader coro: {}".format(type(self).__name__, self._peername, e))
                break
        while running_tasks:
            running_tasks.popleft().cancel()
        self._reader_stopped.set()
        logging.debug("{} Reader coro stopped".format(self._peername))
        yield from self.stop()