Esempio n. 1
0
        /// <summary>
        /// 字节解析为命令对象
        /// </summary>
        public void ReadCommandFromBuffer()
        {
            string h1 = System.Text.Encoding.ASCII.GetString(m_bufer, 0, RpcEnvironment.GUID_LEN).TrimEnd('\0');
            string h2 = 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);
            if (Command.cmdId > RpcEnvironment.NET_COMMAND_BUSINESS_NOTIFY)
            {
                Command.userToken   = h1;
                Command.commandInfo = h2;
            }
            else
            {
                Command.userToken   = h2;
                Command.commandName = h1.Split(':')[1];
            }
            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);
        }
Esempio n. 2
0
        /// <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 (Command.cmdId <= RpcEnvironment.NET_COMMAND_BUSINESS_NOTIFY)
            {
                string token = $"{(Command.cmdId == RpcEnvironment.NET_COMMAND_BUSINESS_NOTIFY ? 'B' : 'S')}:{Command.commandName}";

                var bytes = Encoding.ASCII.GetBytes(token);
                for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++)
                {
                    m_bufer[m_postion++] = bytes[i];
                }
                m_postion = RpcEnvironment.GUID_LEN;
                bytes     = Encoding.ASCII.GetBytes(Command.userToken);
                for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++)
                {
                    m_bufer[m_postion++] = bytes[i];
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Command.userToken))
                {
                    var bytes = Encoding.ASCII.GetBytes(Command.userToken);
                    for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++)
                    {
                        m_bufer[m_postion++] = bytes[i];
                    }
                }
                m_postion = RpcEnvironment.GUID_LEN;
                {
                    var bytes = Encoding.ASCII.GetBytes(Command.commandInfo);
                    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;
        }