Asynchronously handling the accepted stream.

This commit is contained in:
mhchia 2019-09-12 17:06:10 +08:00
parent 0c7afeebaf
commit a7bc9fc358
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A

View File

@ -46,12 +46,15 @@ class SwarmConn(INetConn):
while True: while True:
print("!@# SwarmConn._handle_new_streams") print("!@# SwarmConn._handle_new_streams")
stream = await self.conn.accept_stream() stream = await self.conn.accept_stream()
print("!@# SwarmConn._handle_new_streams: accept_stream:", stream) # Asynchronously handle the accepted stream, to avoid blocking the next stream.
net_stream = await self._add_stream(stream) await self.run_task(self._handle_muxed_stream(stream))
print("!@# SwarmConn.calling common_stream_handler")
await self.close()
async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None:
net_stream = await self._add_stream(muxed_stream)
if self.swarm.common_stream_handler is not None: if self.swarm.common_stream_handler is not None:
await self.run_task(self.swarm.common_stream_handler(net_stream)) await self.run_task(self.swarm.common_stream_handler(net_stream))
await self.close()
async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream: async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream:
print("!@# SwarmConn._add_stream:", muxed_stream) print("!@# SwarmConn._add_stream:", muxed_stream)