Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / ConnectionModeReader.cs / 1 / ConnectionModeReader.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Threading;
using System.ServiceModel;
using System.Collections.Generic;
using System.Diagnostics;
delegate void ConnectionModeCallback(ConnectionModeReader connectionModeReader);
sealed class ConnectionModeReader : InitialServerConnectionReader
{
Exception readException;
ServerModeDecoder decoder;
byte[] buffer;
int offset;
int size;
ConnectionModeCallback callback;
static WaitCallback readCallback;
TimeoutHelper receiveTimeoutHelper;
public ConnectionModeReader(IConnection connection, ConnectionModeCallback callback, ConnectionClosedCallback closedCallback)
: base(connection, closedCallback)
{
this.callback = callback;
}
public int BufferOffset
{
get { return offset; }
}
public int BufferSize
{
get { return size; }
}
public long StreamPosition
{
get { return decoder.StreamPosition; }
}
public TimeSpan GetRemainingTimeout()
{
return this.receiveTimeoutHelper.RemainingTime();
}
void Complete(Exception e)
{
// exception will be logged by the caller
readException = e;
Complete();
}
void Complete()
{
callback(this);
}
bool ContinueReading()
{
for (; ; )
{
if (size == 0)
{
if (readCallback == null)
{
readCallback = new WaitCallback(ReadCallback);
}
if (Connection.BeginRead(0, Connection.AsyncReadBufferSize, GetRemainingTimeout(),
readCallback, this) == AsyncReadResult.Queued)
{
break;
}
if (!GetReadResult()) // we're at EOF, bail
{
return false;
}
}
for (; ; )
{
int bytesDecoded;
try
{
bytesDecoded = decoder.Decode(buffer, offset, size);
}
catch (CommunicationException e)
{
// see if we need to send back a framing fault
string framingFault;
if (FramingEncodingString.TryGetFaultString(e, out framingFault))
{
byte[] drainBuffer = new byte[128];
InitialServerConnectionReader.SendFault(
Connection, framingFault, drainBuffer, GetRemainingTimeout(),
MaxViaSize + MaxContentTypeSize);
base.Close(GetRemainingTimeout());
}
throw;
}
if (bytesDecoded > 0)
{
offset += bytesDecoded;
size -= bytesDecoded;
}
if (decoder.CurrentState == ServerModeDecoder.State.Done)
{
return true;
}
if (size == 0)
{
break;
}
}
}
return false;
}
static void ReadCallback(object state)
{
ConnectionModeReader reader = (ConnectionModeReader)state;
bool completeSelf = false;
Exception completionException = null;
try
{
if (reader.GetReadResult())
{
completeSelf = reader.ContinueReading();
}
}
#pragma warning suppress 56500 // [....], transferring exception to caller
catch (Exception e)
{
if (DiagnosticUtility.IsFatal(e))
{
throw;
}
completeSelf = true;
completionException = e;
}
if (completeSelf)
{
reader.Complete(completionException);
}
}
bool GetReadResult()
{
offset = 0;
size = Connection.EndRead();
if (size == 0)
{
if (this.decoder.StreamPosition == 0) // client timed out a cached connection
{
base.Close(GetRemainingTimeout());
return false;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
}
}
// restore ExceptionEventType to Error after the initial read for cached connections
Connection.ExceptionEventType = TraceEventType.Error;
if (buffer == null)
{
buffer = Connection.AsyncReadBuffer;
}
return true;
}
public FramingMode GetConnectionMode()
{
if (readException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelper(readException, Connection.ExceptionEventType);
}
return decoder.Mode;
}
public void StartReading(TimeSpan receiveTimeout, ItemDequeuedCallback connectionDequeuedCallback)
{
this.decoder = new ServerModeDecoder();
this.receiveTimeoutHelper = new TimeoutHelper(receiveTimeout);
this.ConnectionDequeuedCallback = connectionDequeuedCallback;
bool completeSelf = false;
Exception completionException = null;
try
{
completeSelf = ContinueReading();
}
#pragma warning suppress 56500 // [....], transferring exception to caller
catch (Exception e)
{
if (DiagnosticUtility.IsFatal(e))
{
throw;
}
completeSelf = true;
completionException = e;
}
if (completeSelf)
{
Complete(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
- NamedElement.cs
- DesignerProperties.cs
- AnnouncementEndpointElement.cs
- CornerRadiusConverter.cs
- ADConnectionHelper.cs
- ToolStripStatusLabel.cs
- ToolTipService.cs
- UriTemplate.cs
- CodeCompileUnit.cs
- OutputCacheProfile.cs
- securitycriticaldataClass.cs
- SmtpTransport.cs
- XmlBoundElement.cs
- WmlPhoneCallAdapter.cs
- TypePresenter.xaml.cs
- Substitution.cs
- ServiceHttpHandlerFactory.cs
- PersonalizationStateQuery.cs
- ProfileEventArgs.cs
- HandleCollector.cs
- ToolStripPanel.cs
- BinaryConverter.cs
- BinHexEncoder.cs
- FrameworkPropertyMetadata.cs
- ObjectMaterializedEventArgs.cs
- StrongNameKeyPair.cs
- Attribute.cs
- InheritanceContextChangedEventManager.cs
- BufferModeSettings.cs
- Vector.cs
- XmlSchemaComplexContentRestriction.cs
- GridViewUpdatedEventArgs.cs
- XmlObjectSerializerReadContext.cs
- ByteRangeDownloader.cs
- TokenBasedSet.cs
- WebPartMovingEventArgs.cs
- HighlightComponent.cs
- CanonicalXml.cs
- Size.cs
- AQNBuilder.cs
- ReferenceCountedObject.cs
- ChangePassword.cs
- DataGridViewSortCompareEventArgs.cs
- DataGridViewImageCell.cs
- SystemKeyConverter.cs
- InvokePattern.cs
- Selection.cs
- PolicyManager.cs
- Message.cs
- ZipIOLocalFileDataDescriptor.cs
- WebConfigurationFileMap.cs
- Crypto.cs
- MembershipPasswordException.cs
- FormView.cs
- KeyInstance.cs
- CodeValidator.cs
- FrugalList.cs
- SettingsPropertyWrongTypeException.cs
- StorageEntityContainerMapping.cs
- NamespaceImport.cs
- WebBrowser.cs
- Rotation3D.cs
- SHA256Cng.cs
- ReadWriteSpinLock.cs
- WizardForm.cs
- DictionaryTraceRecord.cs
- DesignTimeDataBinding.cs
- ToolStripContentPanelRenderEventArgs.cs
- PropertyKey.cs
- TableChangeProcessor.cs
- ClientTarget.cs
- StylusPlugInCollection.cs
- SafeThemeHandle.cs
- ActivityBindForm.Designer.cs
- CalendarTable.cs
- CachedPathData.cs
- ComNativeDescriptor.cs
- IndexObject.cs
- CatalogPartCollection.cs
- ColorBuilder.cs
- DataServiceQuery.cs
- HttpStreamXmlDictionaryReader.cs
- RelatedCurrencyManager.cs
- FileSystemInfo.cs
- SkinBuilder.cs
- CodeTryCatchFinallyStatement.cs
- ApplicationContext.cs
- WindowShowOrOpenTracker.cs
- TemplateControlBuildProvider.cs
- PropertyEmitterBase.cs
- SQLGuid.cs
- SystemPens.cs
- mediaclock.cs
- TypeListConverter.cs
- UnsafeNativeMethods.cs
- FragmentNavigationEventArgs.cs
- PackagingUtilities.cs
- WorkflowItemPresenter.cs
- EntityDataSourceChangingEventArgs.cs
- MetadataItem_Static.cs