[fix]Config创建默认配置文件的开关Runtime.CreateConfigOnMissing,仅需对自动创建生效,而不应该阻止用户主动Save
智能大石头 编写于 2024-08-09 00:30:41 石头 提交于 2024-08-10 14:22:24
X
using System;
using NewLife.Serialization;

namespace NewLife.Net.DNS
{
    /// <summary>DNS的二进制序列化</summary>
    class BinaryDNS : BinaryHandlerBase
    {
        BinaryComposite _binary;
        public DNSNameAccessor Accessor { get; set; }

        public BinaryDNS()
        {
            //Priority = 90;
            // 提高优先级,必须在基础处理器之前
            Priority = 0;

            _binary = new BinaryComposite();
            Accessor = new DNSNameAccessor();
        }

        public override bool Write(object value, Type type)
        {
            _binary.Host = this.Host;

            // TXT记录的Text字段不采用DNS字符串
            if (type == typeof(String) && Host.Member.Name != "_Text")
            {
                Int64 p = 0;
                p += Host.Stream.Position;
                Accessor.Write(Host.Stream, (String)value, p);

                return true;
            }
            if (type == typeof(TimeSpan))
            {
                var ts = (TimeSpan)value;
                Host.Write((Int32)ts.TotalSeconds);

                return true;
            }

            return false;
        }

        public override bool TryRead(Type type, ref object value)
        {
            _binary.Host = this.Host;

            // TXT记录的Text字段不采用DNS字符串
            if (type == typeof(String) && Host.Member.Name != "_Text")
            {
                value = Accessor.Read(Host.Stream, 0);

                return true;
            }
            if (type == typeof(TimeSpan))
            {
                value = new TimeSpan(0, 0, Host.Read<Int32>());

                return true;
            }
  
            return false;
        }
    }
}