Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / SynchronizedMessageSource.cs / 1 / SynchronizedMessageSource.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.ServiceModel; using System.Threading; class SynchronizedMessageSource { IMessageSource source; ThreadNeutralSemaphore sourceLock; public SynchronizedMessageSource(IMessageSource source) { this.source = source; sourceLock = new ThreadNeutralSemaphore(1); } public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state) { return new WaitForMessageAsyncResult(this, timeout, callback, state); } public bool EndWaitForMessage(IAsyncResult result) { return WaitForMessageAsyncResult.End(result); } public bool WaitForMessage(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); if (!sourceLock.TryEnter(timeoutHelper.RemainingTime())) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new TimeoutException(SR.GetString(SR.WaitForMessageTimedOut, timeout), ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout))); } try { return source.WaitForMessage(timeoutHelper.RemainingTime()); } finally { sourceLock.Exit(); } } public IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback, object state) { return new ReceiveAsyncResult(this, timeout, callback, state); } public Message EndReceive(IAsyncResult result) { return ReceiveAsyncResult.End(result); } public Message Receive(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); if (!sourceLock.TryEnter(timeoutHelper.RemainingTime())) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new TimeoutException(SR.GetString(SR.ReceiveTimedOut2, timeout), ThreadNeutralSemaphore.CreateEnterTimedOutException(timeout))); } try { return source.Receive(timeoutHelper.RemainingTime()); } finally { sourceLock.Exit(); } } abstract class SynchronizedAsyncResult: AsyncResult { T returnValue; bool exitLock; SynchronizedMessageSource syncSource; static WaitCallback onEnterComplete = new WaitCallback(OnEnterComplete); TimeoutHelper timeoutHelper; public SynchronizedAsyncResult(SynchronizedMessageSource syncSource, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state) { this.syncSource = syncSource; this.timeoutHelper = new TimeoutHelper(timeout); if (!syncSource.sourceLock.Enter(onEnterComplete, this)) return; exitLock = true; bool success = false; bool completeSelf; try { completeSelf = PerformOperation(timeoutHelper.RemainingTime()); success = true; } finally { if (!success) { ExitLock(); } } if (completeSelf) { CompleteWithUnlock(true); } } protected IMessageSource Source { get { return syncSource.source; } } protected void SetReturnValue(T returnValue) { this.returnValue = returnValue; } protected abstract bool PerformOperation(TimeSpan timeout); void ExitLock() { if (exitLock) { syncSource.sourceLock.Exit(); exitLock = false; } } protected void CompleteWithUnlock(bool synchronous) { CompleteWithUnlock(synchronous, null); } protected void CompleteWithUnlock(bool synchronous, Exception exception) { ExitLock(); base.Complete(synchronous, exception); } public static T End(IAsyncResult result) { SynchronizedAsyncResult thisPtr = AsyncResult.End >(result); return thisPtr.returnValue; } static void OnEnterComplete(object state) { SynchronizedAsyncResult thisPtr = (SynchronizedAsyncResult )state; Exception completionException = null; bool completeSelf; try { thisPtr.exitLock = true; completeSelf = thisPtr.PerformOperation(thisPtr.timeoutHelper.RemainingTime()); } #pragma warning suppress 56500 // [....], transferring exception to another thread catch (Exception e) { if (DiagnosticUtility.IsFatal(e)) { throw; } completeSelf = true; completionException = e; } if (completeSelf) { thisPtr.CompleteWithUnlock(false, completionException); } } } class ReceiveAsyncResult : SynchronizedAsyncResult { static WaitCallback onReceiveComplete = new WaitCallback(OnReceiveComplete); public ReceiveAsyncResult(SynchronizedMessageSource syncSource, TimeSpan timeout, AsyncCallback callback, object state) : base(syncSource, timeout, callback, state) { } protected override bool PerformOperation(TimeSpan timeout) { if (Source.BeginReceive(timeout, onReceiveComplete, this) == AsyncReceiveResult.Completed) { SetReturnValue(Source.EndReceive()); return true; } return false; } static void OnReceiveComplete(object state) { ReceiveAsyncResult thisPtr = ((ReceiveAsyncResult)state); Exception completionException = null; try { thisPtr.SetReturnValue(thisPtr.Source.EndReceive()); } #pragma warning suppress 56500 // [....], transferring exception to another thread catch (Exception e) { if (DiagnosticUtility.IsFatal(e)) { throw; } completionException = e; } thisPtr.CompleteWithUnlock(false, completionException); } } class WaitForMessageAsyncResult : SynchronizedAsyncResult { static WaitCallback onWaitForMessageComplete = new WaitCallback(OnWaitForMessageComplete); public WaitForMessageAsyncResult(SynchronizedMessageSource syncSource, TimeSpan timeout, AsyncCallback callback, object state) : base(syncSource, timeout, callback, state) { } protected override bool PerformOperation(TimeSpan timeout) { if (Source.BeginWaitForMessage(timeout, onWaitForMessageComplete, this) == AsyncReceiveResult.Completed) { SetReturnValue(Source.EndWaitForMessage()); return true; } return false; } static void OnWaitForMessageComplete(object state) { WaitForMessageAsyncResult thisPtr = (WaitForMessageAsyncResult)state; Exception completionException = null; try { thisPtr.SetReturnValue(thisPtr.Source.EndWaitForMessage()); } #pragma warning suppress 56500 // [....], transferring exception to another thread catch (Exception e) { if (DiagnosticUtility.IsFatal(e)) { throw; } completionException = e; } thisPtr.CompleteWithUnlock(false, completionException); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- NullableDoubleSumAggregationOperator.cs
- DesignerOptionService.cs
- Geometry.cs
- XhtmlBasicCommandAdapter.cs
- NetSectionGroup.cs
- OleDbTransaction.cs
- TemplatedMailWebEventProvider.cs
- ComNativeDescriptor.cs
- ObjectDataSourceEventArgs.cs
- ResourceReferenceKeyNotFoundException.cs
- RoutedEventConverter.cs
- NGCPageContentSerializerAsync.cs
- NeutralResourcesLanguageAttribute.cs
- ContextStaticAttribute.cs
- BaseCAMarshaler.cs
- MenuEventArgs.cs
- SamlAudienceRestrictionCondition.cs
- EnlistmentState.cs
- MgmtConfigurationRecord.cs
- HtmlInputControl.cs
- EncoderReplacementFallback.cs
- ExpandoClass.cs
- WebPartZone.cs
- HyperLinkField.cs
- TemplatedWizardStep.cs
- indexingfiltermarshaler.cs
- XmlSchemaAttributeGroupRef.cs
- InvokePattern.cs
- PixelFormatConverter.cs
- HtmlHistory.cs
- ResourceExpressionEditorSheet.cs
- SingleResultAttribute.cs
- SiteMapPath.cs
- RegexCapture.cs
- FormattedTextSymbols.cs
- IsolatedStorageException.cs
- FixedDocument.cs
- Variable.cs
- Material.cs
- WebPartConnection.cs
- XmlExceptionHelper.cs
- TextTrailingCharacterEllipsis.cs
- MarginsConverter.cs
- ExceptionAggregator.cs
- HealthMonitoringSection.cs
- CngKeyBlobFormat.cs
- _SslStream.cs
- HtmlContainerControl.cs
- BitmapData.cs
- DirectoryInfo.cs
- GlyphElement.cs
- EventLogPermissionEntryCollection.cs
- HtmlTable.cs
- Help.cs
- CheckedPointers.cs
- LineBreakRecord.cs
- ReturnType.cs
- TextRange.cs
- SpotLight.cs
- Error.cs
- DataGridViewCellMouseEventArgs.cs
- MenuItem.cs
- cookiecontainer.cs
- RectangleGeometry.cs
- CountAggregationOperator.cs
- ObjectPersistData.cs
- EmulateRecognizeCompletedEventArgs.cs
- ListViewInsertedEventArgs.cs
- GestureRecognitionResult.cs
- oledbmetadatacolumnnames.cs
- JobPageOrder.cs
- DbCommandTree.cs
- ApplicationTrust.cs
- TakeOrSkipWhileQueryOperator.cs
- CodeMemberMethod.cs
- ServiceInstallComponent.cs
- PageCodeDomTreeGenerator.cs
- DataSourceCacheDurationConverter.cs
- ErrorRuntimeConfig.cs
- AdornerPresentationContext.cs
- DataError.cs
- loginstatus.cs
- PassportPrincipal.cs
- HashAlgorithm.cs
- documentation.cs
- MimeParameter.cs
- LinkedList.cs
- Image.cs
- Matrix.cs
- APCustomTypeDescriptor.cs
- ClientConvert.cs
- QuaternionKeyFrameCollection.cs
- ColorPalette.cs
- XmlCountingReader.cs
- NotifyInputEventArgs.cs
- ExpressionEditorAttribute.cs
- ConfigXmlElement.cs
- _CacheStreams.cs
- Atom10FeedFormatter.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs