Пример #1
0
    def _send(self,
              data: TLObject,
              wait_response: bool = True,
              timeout: float = WAIT_TIMEOUT):
        message = self.msg_factory(data)
        msg_id = message.msg_id

        if wait_response:
            self.results[msg_id] = Result()

        payload = self.pack(message)

        try:
            self.connection.send(payload)
        except OSError as e:
            self.results.pop(msg_id, None)
            raise e

        if wait_response:
            self.results[msg_id].event.wait(timeout)
            result = self.results.pop(msg_id).value

            if result is None:
                raise TimeoutError
            elif isinstance(result, types.RpcError):
                RPCError.raise_it(result, type(data))
            elif isinstance(result, types.BadMsgNotification):
                raise Exception(
                    self.BAD_MSG_DESCRIPTION.get(
                        result.error_code,
                        "Error code {}".format(result.error_code)))
            else:
                return result
    async def _send(self,
                    data: TLObject,
                    wait_response: bool = True,
                    timeout: float = WAIT_TIMEOUT):
        message = self.msg_factory(data)
        msg_id = message.msg_id

        if wait_response:
            self.results[msg_id] = Result()

        # Call log.debug twice because calling it once by appending "data" to the previous string (i.e. f"Kind: {data}")
        # will cause "data" to be evaluated as string every time instead of only when debug is actually enabled.
        log.debug(f"Sent:")
        log.debug(message)

        payload = await self.loop.run_in_executor(pyrogram.crypto_executor,
                                                  mtproto.pack, message,
                                                  self.current_salt.salt,
                                                  self.session_id,
                                                  self.auth_key,
                                                  self.auth_key_id)

        try:
            await self.connection.send(payload)
        except OSError as e:
            self.results.pop(msg_id, None)
            raise e

        if wait_response:
            try:
                await asyncio.wait_for(self.results[msg_id].event.wait(),
                                       timeout)
            except asyncio.TimeoutError:
                pass
            finally:
                result = self.results.pop(msg_id).value

            if result is None:
                raise TimeoutError
            elif isinstance(result, raw.types.RpcError):
                if isinstance(data, (raw.functions.InvokeWithoutUpdates,
                                     raw.functions.InvokeWithTakeout)):
                    data = data.query

                RPCError.raise_it(result, type(data))
            elif isinstance(result, raw.types.BadMsgNotification):
                raise Exception(
                    self.BAD_MSG_DESCRIPTION.get(
                        result.error_code, f"Error code {result.error_code}"))
            else:
                return result
Пример #3
0
    async def _send(self,
                    data: TLObject,
                    wait_response: bool = True,
                    timeout: float = WAIT_TIMEOUT):
        message = self.msg_factory(data)
        msg_id = message.msg_id

        if wait_response:
            self.results[msg_id] = Result()

        log.debug(f"Sent:\n{message}")

        if len(message) <= self.EXECUTOR_SIZE_THRESHOLD:
            payload = mtproto.pack(message, self.current_salt.salt,
                                   self.session_id, self.auth_key,
                                   self.auth_key_id)
        else:
            payload = await self.loop.run_in_executor(
                self.executor, mtproto.pack, message, self.current_salt.salt,
                self.session_id, self.auth_key, self.auth_key_id)

        try:
            await self.connection.send(payload)
        except OSError as e:
            self.results.pop(msg_id, None)
            raise e

        if wait_response:
            try:
                await asyncio.wait_for(self.results[msg_id].event.wait(),
                                       timeout)
            except asyncio.TimeoutError:
                pass
            finally:
                result = self.results.pop(msg_id).value

            if result is None:
                raise TimeoutError
            elif isinstance(result, raw.types.RpcError):
                if isinstance(data, (raw.functions.InvokeWithoutUpdates,
                                     raw.functions.InvokeWithTakeout)):
                    data = data.query

                RPCError.raise_it(result, type(data))
            elif isinstance(result, raw.types.BadMsgNotification):
                raise Exception(
                    self.BAD_MSG_DESCRIPTION.get(
                        result.error_code, f"Error code {result.error_code}"))
            else:
                return result
Пример #4
0
    async def _send(self,
                    data: TLObject,
                    wait_response: bool = True,
                    timeout: float = WAIT_TIMEOUT):
        message = self.msg_factory(data)
        msg_id = message.msg_id

        if wait_response:
            self.results[msg_id] = Result()

        payload = MTProto.pack(message, self.current_salt.salt,
                               self.session_id, self.auth_key,
                               self.auth_key_id)

        try:
            await self.connection.send(payload)
        except OSError as e:
            self.results.pop(msg_id, None)
            raise e

        if wait_response:
            try:
                await asyncio.wait_for(self.results[msg_id].event.wait(),
                                       timeout)
            except asyncio.TimeoutError:
                pass
            finally:
                result = self.results.pop(msg_id).value

            if result is None:
                raise TimeoutError
            elif isinstance(result, types.RpcError):
                if isinstance(data, (functions.InvokeWithoutUpdates,
                                     functions.InvokeWithTakeout)):
                    data = data.query

                RPCError.raise_it(result, type(data))
            elif isinstance(result, types.BadMsgNotification):
                raise Exception(
                    self.BAD_MSG_DESCRIPTION.get(
                        result.error_code,
                        "Error code {}".format(result.error_code)))
            else:
                return result