Пример #1
0
    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()
Пример #2
0
from srudp import SecureReliableSocket
from time import sleep, time
import sys

sock = SecureReliableSocket()
sock.connect((sys.argv[1], int(sys.argv[2])))

while not sock.is_closed:
    sock.sendall(b'hello ' + str(time()).encode())
    sleep(3)
print("closed", sock)