mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 21:10:29 +00:00
Added and updated documentation
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
# This file is based on TLSharp
|
||||
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/Network/TcpTransport.cs
|
||||
from zlib import crc32
|
||||
|
||||
from network.tcp_message import TcpMessage
|
||||
from network.tcp_client import TcpClient
|
||||
|
||||
|
||||
class TcpTransport:
|
||||
|
||||
def __init__(self, ip_address, port):
|
||||
self._tcp_client = TcpClient()
|
||||
self._send_counter = 0
|
||||
@@ -13,20 +13,20 @@ class TcpTransport:
|
||||
self._tcp_client.connect(ip_address, port)
|
||||
|
||||
def send(self, packet):
|
||||
"""
|
||||
:param packet: Bytes array representing the packet to be sent
|
||||
"""
|
||||
"""Sends the given packet (bytes array) to the connected peer"""
|
||||
if not self._tcp_client.connected:
|
||||
raise ConnectionError('Client not connected to server.')
|
||||
|
||||
# Get a TcpMessage which contains the given packet
|
||||
tcp_message = TcpMessage(self._send_counter, packet)
|
||||
|
||||
# TODO async? and receive too, of course
|
||||
# TODO In TLSharp, this is async; Should both send and receive be here too?
|
||||
self._tcp_client.write(tcp_message.encode())
|
||||
|
||||
self._send_counter += 1
|
||||
|
||||
def receive(self):
|
||||
"""Receives a TcpMessage from the connected peer"""
|
||||
|
||||
# First read everything
|
||||
packet_length_bytes = self._tcp_client.read(4)
|
||||
packet_length = int.from_bytes(packet_length_bytes, byteorder='big')
|
||||
@@ -45,6 +45,7 @@ class TcpTransport:
|
||||
if checksum != valid_checksum:
|
||||
raise ValueError('Invalid checksum, skip')
|
||||
|
||||
# If we passed the tests, we can then return a valid TcpMessage
|
||||
return TcpMessage(seq, body)
|
||||
|
||||
def dispose(self):
|
||||
|
Reference in New Issue
Block a user