コード例 #1
0
    public static Packet BuildPacket(ICommand cmd, UInt32 sequence, UInt32 requestId, UInt32 rpcId, bool checksum, bool crypto)
    {
        Packet packet = new Packet();

        if (null != cmd)
        {
            MemoryStream cmd_stream = new MemoryStream();
            ProtoBuf.Serializer.Serialize(cmd_stream, cmd);

            packet.message = cmd_stream.ToArray();

            UInt32 MsgType = NetHelper.BKDRHash(cmd.GetType().Name);
            UInt32 MsgLen  = (UInt32)packet.message.Length;

            if (MsgLen >= MAX_MSG_LEN)
            {
                throw new Exception("Message too Large!! " + MsgLen);
            }

            packet.head  |= MsgLen;
            packet.opcode = MsgType;
            packet.head  |= NetHelper.HEAD_MSG_MASK;

            if (0 != rpcId)
            {
                packet.rpcId = rpcId;
                packet.head |= NetHelper.HEAD_RPC_MASK;
            }

            if (checksum)
            {
                packet.head |= NetHelper.HEAD_CHK_MASK;
            }

            if (crypto)
            {
                packet.head |= NetHelper.HEAD_CRY_MASK;
            }
        }

        if (0 != requestId)
        {
            packet.requestId = requestId;
            packet.head     |= NetHelper.HEAD_REQ_MASK;
        }

        if (0 != sequence)
        {
            packet.sequence = sequence;
            packet.head    |= NetHelper.HEAD_SEQ_MASK;
        }

        return(packet);
    }