private OperationResult <ChannelState> ReadWithTimeout(ReadingOperation readingOperation, TimeSpan timeout) { ThrowIfDisposed(); bool gainedExclusiveAccess = false; try { gainedExclusiveAccess = _exclusiveAccessSemaphore.WaitOne(timeout); if (!gainedExclusiveAccess) { return(new OperationResult <ChannelState>(OperationStatus.Timeout, null)); } return(ReadAndSetEvents(readingOperation)); } finally { if (gainedExclusiveAccess) { _exclusiveAccessSemaphore.Release(); } } }
public OperationResult <ChannelState> Read(ReadingOperation readingOperation) { var header = Header.Read(_headerView); if (header.HeadNode < 0) { return(new OperationResult <ChannelState>(OperationStatus.QueueIsEmpty, new ChannelState(header))); } var offset = header.HeadNode; var node = Node.Read(_file, header.HeadNode); var status = readingOperation.Read(_file, header.HeadNode + _sizeOfNode, node.Length); if (status == OperationStatus.Cancelled || status == OperationStatus.DelegateFailed) { return(new OperationResult <ChannelState>(status, new ChannelState(header))); } header.HeadNode = node.Next; header.ActiveNodes -= 1; ReturnSpaceToFreeList(offset, _sizeOfNode + node.Length, ref header); Header.Write(_headerView, ref header); return(new OperationResult <ChannelState>(status, new ChannelState(header))); }
private OperationResult <ChannelState> ReadAndSetEvents(ReadingOperation readingOperation) { var result = ((ReaderMemoryManager)_memoryManager).Read(readingOperation); var channelState = result.Data; if (channelState != null && channelState.MessagesCount == 0) { _hasMessagesEvent.Reset(); _noMessagesEvent.Set(); } return(result); }
private OperationResult <ChannelState> ReadWithCancellation(ReadingOperation readingOperation, CancellationToken cancellationToken, TimeSpan timeout) { if (!cancellationToken.CanBeCanceled) { return(ReadWithTimeout(readingOperation, timeout)); } ThrowIfDisposed(); bool gainedExclusiveAccess = false; try { var index = WaitHandle.WaitAny(new[] { cancellationToken.WaitHandle, _exclusiveAccessSemaphore }, timeout); if (index == 0) { return(new OperationResult <ChannelState>(OperationStatus.Cancelled, null)); } if (index == WaitHandle.WaitTimeout) { return(new OperationResult <ChannelState>(OperationStatus.Timeout, null)); } gainedExclusiveAccess = true; return(ReadAndSetEvents(readingOperation)); } finally { if (gainedExclusiveAccess) { _exclusiveAccessSemaphore.Release(); } } }