Exemple #1
0
        public unsafe void EndReceive(uint code, uint bytes, NativeOverlapped *native)
        {
            Overlapped.Free(native);
            Message.Props.Free();

            lock (Outstanding)
                Outstanding.Remove(this);

            if (code == 995) // operation aborted
            {
                Tcs.TrySetException(new QueueException(ErrorCode.OperationCanceled));
                return;
            }

            var result = Native.GetOverlappedResult(native);

            try
            {
                switch (result)
                {
                case 0:
                    Message.Props.ResizeBody();
                    Tcs.TrySetResult(Message);
                    break;

                case (int)ErrorCode.InsufficientResources:
                    Tcs.SetException(new OutOfMemoryException("async receive operation reported InsufficientResources"));
                    break;

                case (int)ErrorCode.IOTimeout:
                    Tcs.TrySetResult(null);
                    break;

                default:
                    // successfully completed but no enough memory
                    if (Native.NotEnoughMemory(result))
                    {
                        Message.Props.Free();
                        Message.Props.IncreaseBufferSize();
                        Props = Message.Props.Allocate();
                        var overlapped       = new Overlapped();
                        var nativeOverlapped = overlapped.Pack(EndReceive, null);
                        int res = Native.ReceiveMessage(handle, timeoutMS, action, Props, nativeOverlapped, null, cursor, IntPtr.Zero);

                        if (res == MQ_INFORMATION_OPERATION_PENDING)        // running asynchronously
                        {
                            return;
                        }

                        // call completed synchronously
                        Message.Props.Free();
                        Overlapped.Free(nativeOverlapped);

                        if (!Native.IsError(res))
                        {
                            Message.Props.ResizeBody();
                            Tcs.TrySetResult(Message);
                            return;
                        }
                    }

                    // some other error
                    Tcs.TrySetException(new QueueException(unchecked ((int)code)));    // or do we use the result?
                    break;
                }
            }
            catch (ObjectDisposedException ex)
            {
                Tcs.TrySetException(new QueueException(ErrorCode.OperationCanceled));
            }
        }