Socket.IOControl dotnet core Linux не работает

В настоящее время я работаю над сервером, который использует поддержку активности, используя Socket.IOControl Но он не работает в dotnet core Linux. Когда я пытаюсь запустить его, я получаю исключение PlatformNotSupportedException

Существует ли кроссплатформенная альтернатива реализации поддержки активности в ядре dotnet?

Пример тестового кода

private static void Main(string[] args)
{
    Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
    socket.Bind((EndPoint)new IPEndPoint(IPAddress.Loopback, 3178));
    socket.Listen(10);
    Console.WriteLine("Server: Begin Listening");
    socket.BeginAccept(new AsyncCallback(Program.AcceptCallback), (object)socket);
    Console.WriteLine("Client: Begin Connecting");
    TcpClient tcpClient = new TcpClient();
    tcpClient.Connect(new IPEndPoint(IPAddress.Loopback, 3178));
    Console.WriteLine("Client: Connected");
    Console.WriteLine("Client: Client keepAlive");
    Program.SetSocketKeepAliveValues(tcpClient.Client, 1000, 1);
    Thread.Sleep(50);
    Console.WriteLine("Done");
    Console.ReadLine();
}

private static void AcceptCallback(IAsyncResult ar)
{
    Socket asyncState = ar.AsyncState as Socket;
    try
    {
        Socket socket = asyncState.EndAccept(ar);
        Console.WriteLine("Server: Connection made");
        Console.WriteLine("Server: Set keepAlive");
        Program.SetSocketKeepAliveValues(socket, 1000, 1);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

public static void SetSocketKeepAliveValues(Socket socket, int KeepAliveTime, int KeepAliveInterval)
{
    uint structure = 0;
    byte[] optionInValue = new byte[Marshal.SizeOf<uint>(structure) * 3];
    BitConverter.GetBytes(true ? 1U : 0U).CopyTo((Array)optionInValue, 0);
    BitConverter.GetBytes((uint)KeepAliveTime).CopyTo((Array)optionInValue, Marshal.SizeOf<uint>(structure));
    BitConverter.GetBytes((uint)KeepAliveInterval).CopyTo((Array)optionInValue, Marshal.SizeOf<uint>(structure) * 2);
    socket.IOControl(IOControlCode.KeepAliveValues, optionInValue, (byte[])null);
}

заранее спасибо


person Jan-Fokke    schedule 11.04.2018    source источник
comment
Опубликуйте SSCCE: sscce.org. Поддержка KeepAlive доступна здесь: docs.microsoft.com/en-us/dotnet/api/   -  person omajid    schedule 11.04.2018
comment
SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); включает поддержку активности, но интервал управляется ОС и слишком велик   -  person Jan-Fokke    schedule 11.04.2018


Ответы (2)


[Изменить] KeepAlive теперь поддерживается в ядре Dotnet

код C

#include <netinet/in.h>
#include <netinet/tcp.h>
#define check(expr) if (!(expr)) { return 0; }

int enable_keepalive(int sock, int enable_keepalive,int time, int interval,int maxpkt) {
    check(setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable_keepalive, sizeof(int)) != -1);

    check(setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &time, sizeof(int)) != -1);

    check(setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(int)) != -1);

    check(setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &maxpkt, sizeof(int)) != -1);
    return 1;
}

использование

[DllImport("libname.so")]
public static extern int enable_keepalive(int sock, int enable_keepalive,int time, int interval,int maxpkt);

private static void Main(string[] args)
{
    TcpClient tcpClient = new TcpClient();
    tcpClient.Connect(new IPEndPoint(IPAddress.Loopback, 3178));
    Console.WriteLine("Client: Connected");
    Console.WriteLine("Client: Client keepAlive");
    Console.WriteLine(enable_keepalive((int)tcpClient.Client.Handle,1,10,5, 2));
}

проверено в убунту 16.04

person Jan-Fokke    schedule 12.04.2018