コード例 #1
0
 /// <summary>
 /// 字节解析为命令对象
 /// </summary>
 public void ReadCommandFromBuffer()
 {
     Command.command = System.Text.Encoding.ASCII.GetString(m_bufer, 0, RpcEnvironment.GUID_LEN).TrimEnd('\0');
     Command.token   = System.Text.Encoding.ASCII.GetString(m_bufer, RpcEnvironment.GUID_LEN, RpcEnvironment.GUID_LEN).TrimEnd('\0');
     m_postion       = RpcEnvironment.GUID_LEN << 1;
     Read(ref Command.cmdId);
     Read(ref Command.tryNum);
     Read(ref Command.cmdState);
     Read(ref Command.crcCode);
     if (Command.crcCode != CrcHelper.Crc(m_bufer, RpcEnvironment.NETCOMMAND_BODY_LEN))
     {
         m_error          = true;
         Command.cmdState = RpcEnvironment.NET_COMMAND_STATE_CRC_ERROR;
         return;
     }
     Read(ref Command.dataLen);
     if (Command.dataLen == 0)
     {
         return;
     }
     m_start_postion = RpcEnvironment.NETCOMMAND_HEAD_LEN;
     Begin();
     Command.dataTypeId = this.m_type_id;
     Command.Data       = TsonTypeRegister.CreateType(Command.dataTypeId);
     if (Command.Data == null)
     {
         m_error          = true;
         Command.cmdState = RpcEnvironment.NET_COMMAND_STATE_UNKNOW_DATA;
         return;
     }
     Command.Data.Deserialize(this);
 }
コード例 #2
0
ファイル: CommandWriter.cs プロジェクト: pingbc/EntityModel
        /// <summary>
        /// 命令对象写入字节
        /// </summary>
        public void WriteCommandToBuffer()
        {
            m_buffer_len = RpcEnvironment.NETCOMMAND_LEN;
            if (Command.Data != null)
            {
                m_buffer_len += Command.Data.SafeBufferLength;
            }
            m_bufer = new byte[m_buffer_len];

            m_postion = 0;
            if (!string.IsNullOrEmpty(Command.command))
            {
                var bytes = Encoding.ASCII.GetBytes(Command.command);
                for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++)
                {
                    m_bufer[m_postion++] = bytes[i];
                }
            }
            if (!string.IsNullOrEmpty(Command.token))
            {
                m_postion = RpcEnvironment.GUID_LEN;
                var bytes = Encoding.ASCII.GetBytes(Command.token);
                for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++)
                {
                    m_bufer[m_postion++] = bytes[i];
                }
            }

            m_postion = RpcEnvironment.GUID_LEN << 1;
            Write(Command.cmdId);
            Write(Command.tryNum);
            Write(Command.cmdState);
            uint crc = CrcHelper.Crc(m_bufer, RpcEnvironment.NETCOMMAND_BODY_LEN);

            Write(crc);
            if (Command.Data == null)
            {
                Command.dataLen = 0;
                return;
            }
            TsonSerializer serializer = new TsonSerializer(m_bufer, m_buffer_len, RpcEnvironment.NETCOMMAND_HEAD_LEN);

            Command.Data.Serialize(serializer);
            Command.dataLen = serializer.DataLen;
            m_end_postion   = serializer.EndPostion;
            m_data_len      = m_end_postion;
        }