Let all connection modes implement the new Connection

This commit is contained in:
Lonami Exo
2018-09-27 19:22:35 +02:00
parent 096424ea78
commit 2fd51b8582
7 changed files with 95 additions and 202 deletions

View File

@@ -1,20 +1,20 @@
import struct
from .tcpfull import ConnectionTcpFull
from .connection import Connection
class ConnectionTcpIntermediate(ConnectionTcpFull):
class ConnectionTcpIntermediate(Connection):
"""
Intermediate mode between `ConnectionTcpFull` and `ConnectionTcpAbridged`.
Always sends 4 extra bytes for the packet length.
"""
async def connect(self, ip, port):
result = await super().connect(ip, port)
await self.conn.write(b'\xee\xee\xee\xee')
return result
async def connect(self):
await super().connect()
await self.send(b'\xee\xee\xee\xee')
async def recv(self):
return await self.read(struct.unpack('<i', await self.read(4))[0])
def _send(self, data):
self._writer.write(struct.pack('<i', len(data)) + data)
async def send(self, message):
await self.write(struct.pack('<i', len(message)) + message)
async def _recv(self):
return await self._reader.readexactly(
struct.unpack('<i', await self._reader.readexactly(4))[0])