Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
python/qmp: allow sockets to be passed to connect()
Allow existing sockets to be passed to connect(). The changes are pretty
minimal, and this allows for far greater flexibility in setting up
communications with an endpoint.

Signed-off-by: John Snow <jsnow@redhat.com>
Message-id: 20230517163406.2593480-2-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
  • Loading branch information
jnsnow committed May 31, 2023
1 parent ab72522 commit 9341b2a
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions python/qemu/qmp/protocol.py
Expand Up @@ -370,7 +370,7 @@ async def accept(self) -> None:

@upper_half
@require(Runstate.IDLE)
async def connect(self, address: SocketAddrT,
async def connect(self, address: Union[SocketAddrT, socket.socket],
ssl: Optional[SSLContext] = None) -> None:
"""
Connect to the server and begin processing message queues.
Expand Down Expand Up @@ -615,7 +615,7 @@ async def _do_accept(self) -> None:
self.logger.debug("Connection accepted.")

@upper_half
async def _do_connect(self, address: SocketAddrT,
async def _do_connect(self, address: Union[SocketAddrT, socket.socket],
ssl: Optional[SSLContext] = None) -> None:
"""
Acting as the transport client, initiate a connection to a server.
Expand All @@ -634,23 +634,32 @@ async def _do_connect(self, address: SocketAddrT,
# otherwise yield.
await asyncio.sleep(0)

self.logger.debug("Connecting to %s ...", address)

if isinstance(address, tuple):
if isinstance(address, socket.socket):
self.logger.debug("Connecting with existing socket: "
"fd=%d, family=%r, type=%r",
address.fileno(), address.family, address.type)
connect = asyncio.open_connection(
limit=self._limit,
ssl=ssl,
sock=address,
)
elif isinstance(address, tuple):
self.logger.debug("Connecting to %s ...", address)
connect = asyncio.open_connection(
address[0],
address[1],
ssl=ssl,
limit=self._limit,
)
else:
self.logger.debug("Connecting to file://%s ...", address)
connect = asyncio.open_unix_connection(
path=address,
ssl=ssl,
limit=self._limit,
)
self._reader, self._writer = await connect

self._reader, self._writer = await connect
self.logger.debug("Connected.")

@upper_half
Expand Down

0 comments on commit 9341b2a

Please sign in to comment.