UdpClient.ReceiveAsync correct early termination

后端 未结 2 652
情歌与酒
情歌与酒 2020-12-17 05:58

Good day. I work with UdpClient and have wrapper upon it.

For reading I have asynchronous method:

private async Task Recei         


        
2条回答
  •  醉酒成梦
    2020-12-17 06:50

    And so, after nearly a week of suffering, I have found the reason and solution.

    At first, I looked at the UdpClient source code. The ReceiveAsync method:

    [HostProtection(ExternalThreading = true)]
    public Task ReceiveAsync()
    {
        return Task.Factory.FromAsync((callback, state) => BeginReceive(callback, state), (ar)=>
            {
                IPEndPoint remoteEP = null;
                Byte[] buffer = EndReceive(ar, ref remoteEP);
                return new UdpReceiveResult(buffer, remoteEP);
    
            }, null);
    }
    

    At second, I found this post with perfect answer:How to abort socket's BeginReceive()?, in which said:

    To cancel a pending call to the BeginConnect() method, close the Socket. When the Close() method is called while an asynchronous operation is in progress, the callback provided to the BeginConnect() method is called. A subsequent call to the EndConnect(IAsyncResult) method will throw an ObjectDisposedException to indicate that the operation has been cancelled.

    And, as we can see, the original ReceiveAsync method return us the ObjectDisposedException, because IOOperation has not been completed after Close invoking.

    To overcome this problem I have done like this:

    New ReceiveAsync realization:

    /// 
    /// Асинхронный запрос на ожидание приёма данных с возможностью досрочного выхода
    /// (для выхода из ожидания вызовите метод Disconnect())
    /// 
    /// Рабочий экземпляр класса UdpClient
    /// Признак досрочного завершения
    /// Если breakToken произошёл до вызова данного метода или в режиме ожидания
    /// ответа, вернёт пустой UdpReceiveResult; при удачном получении ответа-результат
    /// асинхронной операции чтения
    public Task ReceiveAsync(UdpClient client, CancellationToken breakToken)
        => breakToken.IsCancellationRequested
            ? Task.Run(() => new UdpReceiveResult())
            : Task.Factory.FromAsync(
                (callback, state) => client.BeginReceive(callback, state),
                (ar) =>
                    {
                        /// Предотвращение 
                        if (breakToken.IsCancellationRequested)
                            return new UdpReceiveResult();
    
                        IPEndPoint remoteEP = null;
                        var buffer = client.EndReceive(ar, ref remoteEP);
                        return new UdpReceiveResult(buffer, remoteEP);
                    },
                null);
    

    New Dispose realization:

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.cancelReceive?.Cancel();
            this.client?.Close();
            this.cancelReceive?.Dispose();
        }
    }
    

    I very much hope, that my decision will deprive someone else of the pain that I experienced.

提交回复
热议问题