using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Net.Sockets;

namespace CSharpUtil.Extensions.Net
{
    /// <summary>
    /// 辅助类
    /// </summary>
    public static  class SocketUtils
    {
        public static void SetKeepAlive(this Socket sock, int interval_ms)
        {
            uint dummy = 0;
            byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
            BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
            BitConverter.GetBytes((uint)interval_ms).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
            BitConverter.GetBytes((uint)interval_ms).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
            sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
        }
    }
}
