Revert "use weakrefs to solve circular references"

This reverts commit dfe8bcb01e.
This commit is contained in:
Stefan Allius
2023-10-03 11:43:08 +02:00
parent 700b946acf
commit 38dacf2b97

View File

@@ -1,33 +1,34 @@
import asyncio, logging, traceback, weakref import asyncio, logging, traceback
from async_stream import AsyncStream from async_stream import AsyncStream
class Proxy: class Proxy:
def __init__ (proxy, reader, writer, addr): def __init__ (proxy, reader, writer, addr):
proxy.__ServerStream = AsyncStream(proxy, reader, writer, addr) proxy.ServerStream = AsyncStream(proxy, reader, writer, addr)
proxy.__ClientStream = None proxy.ClientStream = None
async def server_loop(proxy, addr): async def server_loop(proxy, addr):
logging.info(f'Accept connection from {addr}') logging.info(f'Accept connection from {addr}')
await proxy.__ServerStream.loop() await proxy.ServerStream.loop()
logging.info(f'Stopped server connection loop {addr}') logging.info(f'Close server connection {addr}')
if proxy.__ClientStream: if proxy.ClientStream:
logging.debug ("disconnect client connection") logging.debug ("close client connection")
proxy.__ClientStream.disc() proxy.ClientStream.close()
async def client_loop(proxy, addr): async def client_loop(proxy, addr):
await proxy.__ClientStream.loop() await proxy.ClientStream.loop()
logging.info(f'Stopped client connection loop {addr}') logging.info(f'Close client connection {addr}')
proxy.__ClientStream = None proxy.ServerStream.remoteStream = None
proxy.ClientStream = None
async def CreateClientStream (proxy, host, port): async def CreateClientStream (proxy, stream, host, port):
addr = (host, port) addr = (host, port)
try: try:
logging.info(f'Connected to {addr}') logging.info(f'Connected to {addr}')
connect = asyncio.open_connection(host, port) connect = asyncio.open_connection(host, port)
reader, writer = await connect reader, writer = await connect
proxy.__ClientStream = AsyncStream(proxy, reader, writer, addr, weakref.ref(proxy.__ServerStream), server_side=False) proxy.ClientStream = AsyncStream(proxy, reader, writer, addr, stream, server_side=False)
asyncio.create_task(proxy.client_loop(addr)) asyncio.create_task(proxy.client_loop(addr))
except ConnectionRefusedError as error: except ConnectionRefusedError as error:
@@ -36,7 +37,7 @@ class Proxy:
logging.error( logging.error(
f"Proxy: Exception for {addr}:\n" f"Proxy: Exception for {addr}:\n"
f"{traceback.format_exc()}") f"{traceback.format_exc()}")
return weakref.ref(proxy.__ClientStream) return proxy.ClientStream
def __del__ (proxy): def __del__ (proxy):
logging.info ("Proxy __del__") logging.debug ("Proxy __del__")