示例#1
0
文件: protocol.py 项目: ctian1/quarry
    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
文件: tests.py 项目: aaps/quarry
 def testVarInt(self):
     value = 5533
     buff = Buffer()
     buff.buff = buff.pack_varint(value)
     self.assertTrue(buff.unpack_varint() == value)