Пример #1
0
    def SerializeBody(self):
        # Create a stream to work with
        data = cStringIO.StringIO()

        # Lots of version info.  But first, the protocol signature.
        data.write(ProtocolSignature)
        BinaryStructs.SerializeUint16(data, ProtocolRevision)
        BinaryStructs.SerializeUint16(data, Version.MajorVersion)
        BinaryStructs.SerializeUint16(data, Version.MinorVersion)

        # Return the body
        return data.getvalue()
Пример #2
0
 def SerializeBody(self):
     # Write data.
     data = cStringIO.StringIO()
     BinaryStructs.SerializeUint16(data, self.Reason)
     retval = data.getvalue()
     data.close()
     return retval
Пример #3
0
 def SerializeBody(self):
     # Implemented from protocol documentation.
     data = cStringIO.StringIO()
     BinaryStructs.SerializeUint32(data, self.RequestSerial)
     BinaryStructs.SerializeUint16(data, self.ReasonCode)
     retval = data.getvalue()
     data.close()
     return retval
Пример #4
0
    def GetBinaryForm(self):
        '''
        Encodes the packet to a binary string for network transmission.
        
        Do not subclass this method for custom packets; instead, subclass the
        SerializeBody() interface method.
        
        @return: Network-safe binary string containing the packet.
        '''
        # let's figure out what we're sending
        flags = 0
        body = self.SerializeBody()
        if body:
            # there's a body that needs to be compressed
            compressed = self.CompressIfNeeded(body)
            if compressed != None:
                bodywriter = cStringIO.StringIO()
                BinaryStructs.SerializeBinary(bodywriter,
                                              compressed,
                                              maxlen=MaxCompressedSize)
                body = bodywriter.getvalue()
                bodywriter.close()
                flags |= HeaderFlag_zlib

        # okay, build the packet
        packetwriter = cStringIO.StringIO()

        # first, the header
        BinaryStructs.SerializeUint16(packetwriter, self.PacketType)
        BinaryStructs.SerializeUint16(packetwriter, flags)

        # then the body
        if body:
            packetwriter.write(body)

        # return the packet data
        retval = packetwriter.getvalue()
        packetwriter.close()
        return retval