Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def connect(self, host, port=1883, ssl=False, keepalive=60, version=MQTTv50, raise_exc=True):
# Init connection
self._host = host
self._port = port
self._ssl = ssl
self._keepalive = keepalive
self._is_active = True
MQTTProtocol.proto_ver = version
self._connection = await self._create_connection(
host, port=self._port, ssl=self._ssl, clean_session=self._clean_session, keepalive=keepalive)
await self._connection.auth(self._client_id, self._username, self._password, will_message=self._will_message,
**self._connect_properties)
await self._connected.wait()
loop = asyncio.get_event_loop()
while not await self._persistent_storage.is_empty:
await loop.create_future()
if raise_exc and self._error:
raise self._error
remaining_length += len(prop_bytes)
packet.extend(pack_variable_byte_integer(remaining_length))
packet.extend(struct.pack("!H" + str(len(protocol.proto_name)) + "sBBH",
len(protocol.proto_name),
protocol.proto_name,
protocol.proto_ver,
connect_flags,
keepalive))
packet.extend(prop_bytes)
return packet
class FBNSMQTTProtocol(MQTTProtocol):
proto_name = b'MQTToT'
proto_ver = 3
async def send_auth_package(self, fbns_auth, clean_session, keepalive, will_message=None, **kwargs):
pkg = FBNSConnectPackageFactor.build_package(fbns_auth, clean_session, keepalive, self, will_message=will_message, **kwargs)
self.write_data(pkg)
class FBNSMQTTConnection(MQTTConnection):
@classmethod
async def create_connection(cls, host, port, ssl, clean_session, keepalive, loop=None):
loop = loop or asyncio.get_event_loop()
transport, protocol = await loop.create_connection(FBNSMQTTProtocol, host, port, ssl=ssl)
return FBNSMQTTConnection(transport, protocol, clean_session, keepalive)
async def auth(self, fbns_auth, will_message=None, **kwargs):
def __init__(self, *args, **kwargs):
super(MQTTProtocol, self).__init__(*args, **kwargs)
self._queue = asyncio.Queue()
self._disconnect = asyncio.Event()
self._read_loop_future = None
def _handle_connack_packet(self, cmd, packet):
self._connected.set()
(flags, result) = struct.unpack("!BB", packet[:2])
if result != 0:
logger.warning('[CONNACK] %s', hex(result))
self.failed_connections += 1
if result == 1 and self.protocol_version == MQTTv50:
logger.info('[CONNACK] Downgrading to MQTT 3.1 protocol version')
MQTTProtocol.proto_ver = MQTTv311
future = asyncio.ensure_future(self.reconnect(delay=True))
future.add_done_callback(self._handle_exception_in_future)
return
else:
self._error = MQTTConnectError(result)
asyncio.ensure_future(self.reconnect(delay=True))
return
else:
self.failed_connections = 0
if len(packet) > 2:
properties, _ = self._parse_properties(packet[2:])
if properties is None:
self._error = MQTTConnectError(10)
asyncio.ensure_future(self.disconnect())
self._connack_properties = properties
async def create_connection(cls, host, port, ssl, clean_session, keepalive, loop=None):
loop = loop or asyncio.get_event_loop()
transport, protocol = await loop.create_connection(MQTTProtocol, host, port, ssl=ssl)
return MQTTConnection(transport, protocol, clean_session, keepalive)