Exemplo n.º 1
0
        protected override void UnPack(Head responce)
        {
            // TODO:如果是关闭类型,则关闭该流
            if (responce.Type == ContentType.CloseStreamType)
            {
                StreamClose();
                return;
            }

            // 正常读取流信息,分发处理
            uint   OpCode = OpcodeTypeComponent.InnerOpCode((ushort)responce.ServiceID, (ushort)responce.FunctionID);
            object message_data;

            try
            {
                OpcodeTypeComponent opcodeTypeComponent = connection_.netWork.opcodeTypeComponent;
                object instance = opcodeTypeComponent.GetInstance(OpCode);

                message_data = connection_.netWork.MessagePacker
                               .DeserializeFrom(instance, responce.Body.ToByteArray(), 0, responce.Body.Length);
            }
            catch (Exception e)
            {
                // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                Debug.Fail($"package error; opcode: {(ushort)responce.FunctionID} {connection_.netWork.Count} {e} ");
                connection_.netWork.Remove(connection_);
                return;
            }

            connection_.netWork.MessageDispatcher
            .Dispatch(connection_, (uint)OpCode, message_data);
        }
Exemplo n.º 2
0
        protected override void UnPack(Head responce)
        {
            try
            {
                uint   OpCode = OpcodeTypeComponent.InnerOpCode((ushort)responce.ServiceID, (ushort)responce.FunctionID);
                object message_data;
                try
                {
                    OpcodeTypeComponent opcodeTypeComponent = connection_.netWork.opcodeTypeComponent;
                    object instance = opcodeTypeComponent.GetInstance(OpCode);

                    message_data = connection_.netWork.MessagePacker
                                   .DeserializeFrom(instance, responce.Body.ToByteArray(), 0, responce.Body.Length);
                }
                catch (Exception e)
                {
                    // 出现任何消息解析异常都要断开Session,防止客户端伪造消息
                    Debug.Fail($"package error; opcode: {(ushort)responce.FunctionID} {connection_.netWork.Count} {e} ");
                    connection_.netWork.Remove(connection_);
                    return;
                }

                if (responce.Type == ContentType.ResponseType)
                {
                    // PRC CALL信息返回,任务回调-正确消息或错误消息
                    IMessage imessage = message_data as IMessage;
                    response_action_(imessage);
                }
                else if (responce.Type == ContentType.ErrorType)
                {
                    StreamClose();
                    Debug.Fail($"Error; ServiceID: {responce.ServiceID}, FunctionID: {responce.FunctionID}");
                    throw new Exception($"Error; ServiceID: {responce.ServiceID}, FunctionID: {responce.FunctionID}");
                }
                else
                {
                    StreamClose();
                    Debug.Fail($"Error responce message, Type:{responce.Type}; OpCode: {(ushort)responce.FunctionID}");
                    throw new Exception($"Error responce message, Type:{responce.Type}; OpCode: {(ushort)responce.FunctionID}");
                }
            }
            finally
            {
                StreamClose();
            }
        }