diff options
author | Devaev Maxim <[email protected]> | 2020-05-25 21:32:01 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-05-25 21:32:01 +0300 |
commit | 9bfc09a265bb63ac8253bc2b3c03f23807dc0204 (patch) | |
tree | b8c30beb1c0fc378d8d3d6f98f1f23afc607a032 | |
parent | aed6bdaf8404317b6345412093e2dfd76916dd29 (diff) |
prepare to the new feature
-rw-r--r-- | kvmd/apps/vnc/server.py | 2 | ||||
-rw-r--r-- | kvmd/clients/streamer.py | 32 |
2 files changed, 24 insertions, 10 deletions
diff --git a/kvmd/apps/vnc/server.py b/kvmd/apps/vnc/server.py index 015b4581..a5517c11 100644 --- a/kvmd/apps/vnc/server.py +++ b/kvmd/apps/vnc/server.py @@ -171,7 +171,7 @@ class _Client(RfbClient): # pylint: disable=too-many-instance-attributes while True: try: streaming = False - async for (online, width, height, jpeg) in self.__streamer.read(): + async for (online, width, height, jpeg) in self.__streamer.read_stream(): if not streaming: logger.info("[streamer] Client %s: Streaming ...", self._remote) streaming = True diff --git a/kvmd/clients/streamer.py b/kvmd/clients/streamer.py index e38497e5..ea18e5f9 100644 --- a/kvmd/clients/streamer.py +++ b/kvmd/clients/streamer.py @@ -53,13 +53,12 @@ class StreamerClient: self.__timeout = timeout self.__user_agent = user_agent - async def read(self) -> AsyncGenerator[Tuple[bool, int, int, bytes], None]: + async def read_stream(self) -> AsyncGenerator[Tuple[bool, int, int, bytes], None]: try: - async with self.__make_session() as session: + async with self.__make_session(infinite=True) as session: async with session.get( - url=f"http://{self.__host}:{self.__port}/stream", + url=self.__make_url("stream"), params={"extra_headers": "1"}, - headers={"User-Agent": self.__user_agent}, ) as response: aiotools.raise_not_200(response) reader = aiohttp.MultipartReader.from_response(response) @@ -76,13 +75,28 @@ class StreamerClient: except Exception as err: # Тут бывают и ассерты, и KeyError, и прочая херня из-за корявых исключений в MultipartReader raise StreamerError(err) - def __make_session(self) -> aiohttp.ClientSession: - kwargs: Dict = { - "timeout": aiohttp.ClientTimeout( +# async def get_snapshot(self) -> Tuple[bool, bytes]: +# async with self.__make_session(infinite=False) as session: +# async with session.get(self.__make_url("snapshot")) as response: +# aiotools.raise_not_200(response) +# return ( +# (response.headers["X-UStreamer-Online"] == "true"), +# bytes(await response.read()), +# ) + + def __make_session(self, infinite: bool) -> aiohttp.ClientSession: + kwargs: Dict = {"headers": {"User-Agent": self.__user_agent}} + if infinite: + kwargs["timeout"] = aiohttp.ClientTimeout( connect=self.__timeout, sock_read=self.__timeout, - ), - } + ) + else: + kwargs["timeout"] = aiohttp.ClientTimeout(total=self.__timeout) if self.__unix_path: kwargs["connector"] = aiohttp.UnixConnector(path=self.__unix_path) return aiohttp.ClientSession(**kwargs) + + def __make_url(self, handle: str) -> str: + assert not handle.startswith("/"), handle + return f"http://{self.__host}:{self.__port}/{handle}" |