def test_big_size(self): logger.info("start test_big_size()") sock1 = SecureReliableSocket() sock2 = SecureReliableSocket() sock1.settimeout(5.0) sock2.settimeout(5.0) # connect fut1 = self.executor.submit(sock1.connect, ("127.0.0.1", self.port1), self.port2) fut2 = self.executor.submit(sock2.connect, ("127.0.0.1", self.port2), self.port1) fut1.result(10.0) fut2.result(10.0) # 1M bytes data data = os.urandom(1000000) self.executor.submit(sock2.sendall, data)\ .add_done_callback(lambda fut: fut.result()) received = b"" while True: try: received += sock1.recv(4096) if 1000000 <= len(received): break except s.timeout: break assert received == data, (len(received), len(data)) # close sock1.close() sock2.close()
def test_basic(self): logger.info("start test_basic()") sock1 = SecureReliableSocket() sock2 = SecureReliableSocket() sock1.settimeout(5.0) sock2.settimeout(5.0) # connect fut1 = self.executor.submit(sock1.connect, ("127.0.0.1", self.port1), self.port2) fut2 = self.executor.submit(sock2.connect, ("127.0.0.1", self.port2), self.port1) logger.info("wait fut") fut1.result(10.0) fut2.result(10.0) # connection info logger.info("getsockname") assert sock1.getpeername() == sock2.getsockname(), ( sock1.getpeername(), sock2.getsockname()) # normal sending logger.info("sending") sock1.sendall(b"hello world") logger.info("recv") assert sock2.recv(1024) == b"hello world" # broadcast sending sock2.broadcast(b"good man") assert sock1.recv(1024) == b"good man" # broadcast hook fnc def hook_fnc(packet: Packet, _sock: SecureReliableSocket): assert packet.data == b"broadcasting now" sock1.broadcast_hook_fnc = hook_fnc sock2.broadcast(b"broadcasting now") # close sock1.close() sock2.close()
from srudp import SecureReliableSocket from time import time import sys sock = SecureReliableSocket() sock.connect((sys.argv[1], int(sys.argv[2]))) while not sock.is_closed: data = sock.recv(1024) if not data: break print(time(), data) print("closed", sock)