Frage

Hi I'm trying to create a TAP wrapper. The ConnectTaskAsync is fine, however I'm having some difficulties with SendTaskAsync.

public static Task ConnectTaskAsync(this Socket socket, EndPoint endpoint)
        {
            return Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, endpoint, null);
        }

    public static Task<int> SendTaskAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags)
    {
        return Task<int>.Factory.FromAsync(socket.BeginSend, buffer, offset, size, flags, socket.EndSend, null);
    }

The error I'm getting is a red underline beneath BeginSend with the message Expected a method with IAsyncResult BeginSend

Where did I go wrong?

War es hilfreich?

Lösung

You have your FromAsync defined incorrectly. Unfortunately, there isn't an overload of FromAsync which accepts 4 separate arguments (which would be required for BeginSend), so you'll need to use a different overload:

public static Task<int> SendTaskAsync(this Socket socket, byte[] buffer, int offset, int size, SocketFlags flags)
{
    AsyncCallback nullOp = (i) => {};
    IAsyncResult result = socket.BeginSend(buffer, offset, size, flags, nullOp, socket);
    // Use overload that takes an IAsyncResult directly
    return Task.Factory.FromAsync(result, socket.EndSend);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top