应为具有IAsyncResult BeginSend的方法
本文关键字:BeginSend 方法 IAsyncResult | 更新日期: 2023-09-27 18:21:15
嗨,我正在尝试创建一个TAP包装器。ConnectTaskAsync很好,但是我在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);
}
我得到的错误是BeginSend
下面的红色下划线,其中包含消息Expected a method with IAsyncResult BeginSend
我哪里错了?
您的FromAsync
定义不正确。不幸的是,FromAsync没有重载,它接受4个单独的参数(BeginSend
需要这些参数),因此您需要使用不同的重载:
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);
}