Пример #1
0
    def send_packet(self, name, data=b""):
        """ Sends a packet """

        if self.closed:
            return

        self.log_packet("# send", name)

        # Prepend ident
        key = (self.protocol_version, self.protocol_mode, self.send_direction,
               name)
        try:
            ident = packets.packet_idents[key]
        except KeyError:
            raise ProtocolError("No ID known for packet: %s" % (key, ))
        data = Buffer.pack_varint(ident) + data

        if self.compression_enabled:
            # Compress data and prepend uncompressed data length
            if len(data) >= self.compression_threshold:
                data = Buffer.pack_varint(len(data)) + zlib.compress(data)
            else:
                data = Buffer.pack_varint(0) + data

        # Prepend packet length
        data = self.buff_type.pack_varint(len(data)) + data

        # Encrypt
        data = self.cipher.encrypt(data)

        # Send
        self.transport.write(data)
Пример #2
0
    def send_packet(self, name, data=b""):
        """ Sends a packet """

        if self.closed:
            return

        self.log_packet("# send", name)

        # Prepend ident
        key = (self.protocol_version, self.protocol_mode, self.send_direction, name)
        try:
            ident = packets.packet_idents[key]
        except KeyError:
            raise ProtocolError("No ID known for packet: %s" % (key,))
        data = Buffer.pack_varint(ident) + data

        if self.compression_enabled:
            # Compress data and prepend uncompressed data length
            if len(data) >= self.compression_threshold:
                data = Buffer.pack_varint(len(data)) + zlib.compress(data)
            else:
                data = Buffer.pack_varint(0) + data

        # Prepend packet length
        data = self.buff_type.pack_varint(len(data)) + data

        # Encrypt
        data = self.cipher.encrypt(data)

        # Send
        self.transport.write(data)
Пример #3
0
def test_pack_varint():
    for value, data in varint_vectors:
        assert Buffer.pack_varint(value, signed=True) == data
Пример #4
0
 def testVarInt(self):
     value = 5533
     buff = Buffer()
     buff.buff = buff.pack_varint(value)
     self.assertTrue(buff.unpack_varint() == value)
Пример #5
0
 def testVarInt(self):
     value = 5533
     buff = Buffer()
     buff.buff = buff.pack_varint(value)
     self.assertTrue(buff.unpack_varint() == value)