mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 07:00:54 +00:00
Stream rearchitecture (#126)
* Add generic protocol handler * Add generic protocol handler to stream muxing pipeline * Modify conn_handler to only deal with connections * mplex accept stream architecture changes * Add create generic protocol handler * Fix minor bugs * who would win 4 devs or one not * Debugging * rearch with handle_incoming infinite loop, seems to work, needs cleanup" * passing linting, still needs cleanup * fixing linting again; code still needs cleanup * fixing tests; code still needs cleanup * adding test cleanup and task cleanup, removing prints * linting, and cleanup complete * storing connections based on peer id * remove dead code * remove unnecessary peer_id
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
|
||||
from .constants import HEADER_TAGS
|
||||
from .utils import get_flag
|
||||
from ..muxed_stream_interface import IMuxedStream
|
||||
|
||||
|
||||
@ -26,17 +26,6 @@ class MplexStream(IMuxedStream):
|
||||
self.remote_closed = False
|
||||
self.stream_lock = asyncio.Lock()
|
||||
|
||||
def get_flag(self, action):
|
||||
"""
|
||||
get header flag based on action for mplex
|
||||
:param action: action type in str
|
||||
:return: int flag
|
||||
"""
|
||||
if self.initiator:
|
||||
return HEADER_TAGS[action]
|
||||
|
||||
return HEADER_TAGS[action] - 1
|
||||
|
||||
async def read(self):
|
||||
"""
|
||||
read messages associated with stream from buffer til end of file
|
||||
@ -49,7 +38,8 @@ class MplexStream(IMuxedStream):
|
||||
write to stream
|
||||
:return: number of bytes written
|
||||
"""
|
||||
return await self.mplex_conn.send_message(self.get_flag("MESSAGE"), data, self.stream_id)
|
||||
return await self.mplex_conn.send_message(
|
||||
get_flag(self.initiator, "MESSAGE"), data, self.stream_id)
|
||||
|
||||
async def close(self):
|
||||
"""
|
||||
@ -59,7 +49,7 @@ class MplexStream(IMuxedStream):
|
||||
"""
|
||||
# TODO error handling with timeout
|
||||
# TODO understand better how mutexes are used from go repo
|
||||
await self.mplex_conn.send_message(self.get_flag("CLOSE"), None, self.stream_id)
|
||||
await self.mplex_conn.send_message(get_flag(self.initiator, "CLOSE"), None, self.stream_id)
|
||||
|
||||
remote_lock = ""
|
||||
async with self.stream_lock:
|
||||
@ -87,7 +77,8 @@ class MplexStream(IMuxedStream):
|
||||
return True
|
||||
|
||||
if not self.remote_closed:
|
||||
await self.mplex_conn.send_message(self.get_flag("RESET"), None, self.stream_id)
|
||||
await self.mplex_conn.send_message(
|
||||
get_flag(self.initiator, "RESET"), None, self.stream_id)
|
||||
|
||||
self.local_closed = True
|
||||
self.remote_closed = True
|
||||
|
||||
Reference in New Issue
Block a user