[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
using System;
using System.Diagnostics;
using NewLife.Reflection;

namespace NewLife.Log
{
    /// <summary>性能计数器接口</summary>
    public interface ICounter
    {
        /// <summary>数值</summary>
        Int64 Value { get; }

        /// <summary>次数</summary>
        Int64 Times { get; }

        /// <summary>速度</summary>
        Int64 Speed { get; }

        /// <summary>平均耗时,单位us</summary>
        Int64 Cost { get; }

        /// <summary>增加</summary>
        /// <param name="value">增加的数量</param>
        /// <param name="usCost">耗时,单位us</param>
        void Increment(Int64 value, Int64 usCost);
    }

    /// <summary>计数器助手</summary>
    public static class CounterHelper
    {
        private static readonly Double tickFrequency;
        static CounterHelper()
        {
            var type = typeof(Stopwatch);
            var fi = type.GetFieldEx("tickFrequency") ?? type.GetFieldEx("s_tickFrequency");
            if (fi != null) tickFrequency = (Double)(fi.GetValue(null) ?? 0);
        }

        /// <summary>开始计时</summary>
        /// <param name="counter"></param>
        /// <returns></returns>
        public static Int64 StartCount(this ICounter? counter) => counter == null ? 0 : Stopwatch.GetTimestamp();

        /// <summary>结束计时</summary>
        /// <param name="counter"></param>
        /// <param name="startTicks"></param>
        public static Int64 StopCount(this ICounter? counter, Int64? startTicks)
        {
            if (counter == null || startTicks == null) return 0;

            var ticks = Stopwatch.GetTimestamp() - startTicks.Value;
            var usCost = (Int64)(ticks * tickFrequency / 10);
            counter.Increment(1, usCost);

            return usCost;
        }
    }
}