Send packets rather than send raw byte array data
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
		@@ -44,14 +44,18 @@ class Squinnondation:
 | 
			
		||||
 | 
			
		||||
        if not instance.args.bind_only:
 | 
			
		||||
            hazelnut = Hazelnut(address=instance.client_address, port=instance.client_port)
 | 
			
		||||
            squirrel.send_raw_data(hazelnut, b"Hello world!")
 | 
			
		||||
            pkt = Packet()
 | 
			
		||||
            pkt.magic = 95
 | 
			
		||||
            pkt.version = 0
 | 
			
		||||
            pkt.body = TLV()
 | 
			
		||||
            msg = f"Hello world, my name is {squirrel.nickname}!"
 | 
			
		||||
            pkt.body.raw_data = msg.encode("UTF-8")
 | 
			
		||||
            pkt.body_length = len(pkt.body.raw_data)
 | 
			
		||||
            squirrel.send_packet(hazelnut, pkt)
 | 
			
		||||
 | 
			
		||||
        while True:
 | 
			
		||||
            data, addr = squirrel.receive_raw_data()
 | 
			
		||||
            pkt = Packet.unmarshal(data)
 | 
			
		||||
            print("received message: %d %d %d %s" % (pkt.magic, pkt.version, pkt.body_length, pkt.body.raw_data))
 | 
			
		||||
            print(str(pkt.marshal()))
 | 
			
		||||
            # squirrel.send_raw_data(hazelnut, b"Hello squirrel!")
 | 
			
		||||
            pkt, addr = squirrel.receive_packet()
 | 
			
		||||
            print(f"received message: {pkt.body.raw_data.decode('UTF-8')}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TLV:
 | 
			
		||||
@@ -115,7 +119,8 @@ class Packet:
 | 
			
		||||
        """
 | 
			
		||||
        Compute the byte array data associated to the packet.
 | 
			
		||||
        """
 | 
			
		||||
        data = bytes([self.magic, self.version])
 | 
			
		||||
        data = self.magic.to_bytes(1, "big")
 | 
			
		||||
        data += self.version.to_bytes(1, "big")
 | 
			
		||||
        data += self.body_length.to_bytes(2, "big")
 | 
			
		||||
        data += self.body.raw_data
 | 
			
		||||
        return data
 | 
			
		||||
@@ -141,12 +146,26 @@ class Squirrel(Hazelnut):
 | 
			
		||||
        self.socket.bind((self.address, self.port))
 | 
			
		||||
        print(f"Listening on {self.address}:{self.port}")
 | 
			
		||||
 | 
			
		||||
    def send_packet(self, client: Hazelnut, pkt: Packet) -> int:
 | 
			
		||||
        """
 | 
			
		||||
        Send a formatted packet to a client.
 | 
			
		||||
        """
 | 
			
		||||
        return self.send_raw_data(client, pkt.marshal())
 | 
			
		||||
 | 
			
		||||
    def send_raw_data(self, client: Hazelnut, data: bytes) -> int:
 | 
			
		||||
        """
 | 
			
		||||
        Send a raw packet to a client.
 | 
			
		||||
        """
 | 
			
		||||
        return self.socket.sendto(data, (client.address, client.port))
 | 
			
		||||
 | 
			
		||||
    def receive_packet(self) -> Tuple[Packet, Any]:
 | 
			
		||||
        """
 | 
			
		||||
        Receive a packet from the socket and translate it into a Python object.
 | 
			
		||||
        TODO: Translate the address into the correct hazelnut.
 | 
			
		||||
        """
 | 
			
		||||
        data, addr = self.receive_raw_data()
 | 
			
		||||
        return Packet.unmarshal(data), addr
 | 
			
		||||
 | 
			
		||||
    def receive_raw_data(self) -> Tuple[bytes, Any]:
 | 
			
		||||
        """
 | 
			
		||||
        Receive a packet from the socket.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user