使用AsyncEnumerator实现APM模式时的异常处理

时间:2011-08-19 13:18:47

标签: c# asynchronous

我正在尝试使用Richter的AsyncEnumerator类来实现APM模式。目标是实现ExtendedSocket类,该类派生自Socket并提供Begin/EndReceiveFixedBegin/EndSendFixed方法,以异步方式发送或接收固定数量的字节。

代码看起来像这样(我省略了发送部分,因为它与接收基本相同):

class ExtendedSocket : Socket
{

    public ExtendedSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        : base(addressFamily, socketType, protocolType)
    {

    }

    public IAsyncResult BeginReceiveFixed(byte[] buffer, SocketFlags socketFlags, AsyncCallback callback, Object state)
    {
        AsyncEnumerator ae = new AsyncEnumerator();
        return ae.BeginExecute(DoReceiveFixed(ae, buffer, socketFlags), callback, state);
    }

    public void EndReceiveFixed(IAsyncResult asyncResult)
    {
        AsyncResult ar = asyncResult as AsyncResult;
        (ar.InitiatingObject as AsyncEnumerator).EndExecute(ar);
    }

    private IEnumerator<Int32> DoReceiveFixed(AsyncEnumerator ae, byte[] buffer, SocketFlags socketFlags)
    {
        int totalReceivedBytes = 0;
        while (totalReceivedBytes < buffer.Length)
        {
            BeginReceive(buffer, totalReceivedBytes, buffer.Length - totalReceivedBytes, socketFlags, ae.End(), null);
            yield return 1;
            totalReceivedBytes += EndReceive(ae.DequeueAsyncResult());
        }
    }
}

这在我的应用程序中完全正常,但我不知道如何处理DoReceiveFixed中的异常。我想实现默认的APM行为,其中在调用EndReceiveFixed时(重新)抛出异常。

不幸的是,我无权访问AsyncResult内的DoReceiveFixed对象,因此我无法在SetAsCompleted对象上调用AsyncResult,但我不能。{/ p>

我目前的解决方法是使用AsyncEnumerator<Exception>代替AsyncEnumerator,如下所示:

class ExtendedSocket : Socket
{

    public ExtendedSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        : base(addressFamily, socketType, protocolType)
    {

    }

    public IAsyncResult BeginReceiveFixed(byte[] buffer, SocketFlags socketFlags, AsyncCallback callback, Object state)
    {
        AsyncEnumerator<Exception> ae = new AsyncEnumerator<Exception>();
        return ae.BeginExecute(DoReceiveFixed(ae, buffer, socketFlags), callback, state);
    }

    public void EndReceiveFixed(IAsyncResult asyncResult)
    {
        AsyncResult ar = asyncResult as AsyncResult;
        AsyncEnumerator<Exception> ae = ar.InitiatingObject as AsyncEnumerator<Exception>;
        ae.EndExecute(ar);
        if (ae.Result != null)
        {
            throw ae.Result;
        }
    }

    private IEnumerator<Int32> DoReceiveFixed(AsyncEnumerator<Exception> ae, byte[] buffer, SocketFlags socketFlags)
    {
        int totalReceivedBytes = 0;
        Exception catchedException = null;
        while (totalReceivedBytes < buffer.Length)
        {
            try
            {
                BeginReceive(buffer, totalReceivedBytes, buffer.Length - totalReceivedBytes, socketFlags, ae.End(), null);
            }
            catch (Exception ex)
            {
                catchedException = ex;
                break;
            }
            yield return 1;
            try
            {
                totalReceivedBytes += EndReceive(ae.DequeueAsyncResult());
            }
            catch (Exception ex)
            {
                catchedException = ex;
                break;
            }
        }
        ae.Result = catchedException;
    }
}

这似乎有效,但我真的不喜欢这个解决方案。有一个更好的方法吗?也许有办法从AsyncResult内部访问DoFixedReceive对象?

1 个答案:

答案 0 :(得分:0)

在Jeffrey Richter的帮助下,我解决了我的问题(see here):

无需捕获迭代器中的所有异常并手动重新抛出它们。 AsyncEnumerator为我们做了这件事。

但请注意调试器设置。我需要在常规调试页面上取消选中“启用我的代码”设置。否则,如果迭代器内部发生异常,则在AsyncEnumerator有机会捕获异常之前,调试器会以未处理的异常消息中断。