Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / Connection.cs / 1 / Connection.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.IO; using System.ServiceModel; using System.Threading; using System.Diagnostics; using System.Net; // Low level abstraction for a socket/pipe interface IConnection { byte[] AsyncReadBuffer { get; } int AsyncReadBufferSize { get; } TraceEventType ExceptionEventType { get; set;} IPEndPoint RemoteIPEndPoint { get; } void Abort(); void Close(TimeSpan timeout); void Shutdown(TimeSpan timeout); IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state); void EndWrite(IAsyncResult result); void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout); void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager); int Read(byte[] buffer, int offset, int size, TimeSpan timeout); AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state); int EndRead(); // very ugly listener stuff object DuplicateAndClose(int targetProcessId); object GetCoreTransport(); bool Validate(Uri uri); } enum AsyncReadResult { Completed, Queued, } // Low level abstraction for connecting a socket/pipe interface IConnectionInitiator { IConnection Connect(Uri uri, TimeSpan timeout); IAsyncResult BeginConnect(Uri uri, TimeSpan timeout, AsyncCallback callback, object state); IConnection EndConnect(IAsyncResult result); } // Low level abstraction for listening for sockets/pipes interface IConnectionListener : IDisposable { void Listen(); IAsyncResult BeginAccept(AsyncCallback callback, object state); IConnection EndAccept(IAsyncResult result); } abstract class DelegatingConnection : IConnection { IConnection connection; protected DelegatingConnection(IConnection connection) { this.connection = connection; } public virtual byte[] AsyncReadBuffer { get { return connection.AsyncReadBuffer; } } public virtual int AsyncReadBufferSize { get { return connection.AsyncReadBufferSize; } } public TraceEventType ExceptionEventType { get { return connection.ExceptionEventType; } set { connection.ExceptionEventType = value; } } protected IConnection Connection { get { return connection; } } public IPEndPoint RemoteIPEndPoint { get { return connection.RemoteIPEndPoint; } } public virtual void Abort() { connection.Abort(); } public virtual void Close(TimeSpan timeout) { connection.Close(timeout); } public virtual void Shutdown(TimeSpan timeout) { connection.Shutdown(timeout); } public virtual object DuplicateAndClose(int targetProcessId) { return connection.DuplicateAndClose(targetProcessId); } public virtual object GetCoreTransport() { return connection.GetCoreTransport(); } public virtual bool Validate(Uri uri) { return connection.Validate(uri); } public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state) { return connection.BeginWrite(buffer, offset, size, immediate, timeout, callback, state); } public virtual void EndWrite(IAsyncResult result) { connection.EndWrite(result); } public virtual void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout) { connection.Write(buffer, offset, size, immediate, timeout); } public virtual void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { connection.Write(buffer, offset, size, immediate, timeout, bufferManager); } public virtual int Read(byte[] buffer, int offset, int size, TimeSpan timeout) { return connection.Read(buffer, offset, size, timeout); } public virtual AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state) { return connection.BeginRead(offset, size, timeout, callback, state); } public virtual int EndRead() { return connection.EndRead(); } } class PreReadConnection : DelegatingConnection { int asyncBytesRead; byte[] preReadData; int preReadOffset; int preReadCount; public PreReadConnection(IConnection innerConnection, byte[] initialData) : this(innerConnection, initialData, 0, initialData.Length) { } public PreReadConnection(IConnection innerConnection, byte[] initialData, int initialOffset, int initialSize) : base(innerConnection) { this.preReadData = initialData; this.preReadOffset = initialOffset; this.preReadCount = initialSize; } public void AddPreReadData(byte[] initialData, int initialOffset, int initialSize) { if (this.preReadCount > 0) { byte[] tempBuffer = this.preReadData; this.preReadData = DiagnosticUtility.Utility.AllocateByteArray(initialSize + this.preReadCount); Buffer.BlockCopy(tempBuffer, this.preReadOffset, this.preReadData, 0, this.preReadCount); Buffer.BlockCopy(initialData, initialOffset, this.preReadData, this.preReadCount, initialSize); this.preReadOffset = 0; this.preReadCount += initialSize; } else { this.preReadData = initialData; this.preReadOffset = initialOffset; this.preReadCount = initialSize; } } public override int Read(byte[] buffer, int offset, int size, TimeSpan timeout) { ConnectionUtilities.ValidateBufferBounds(buffer, offset, size); if (this.preReadCount > 0) { int bytesToCopy = Math.Min(size, this.preReadCount); Buffer.BlockCopy(this.preReadData, this.preReadOffset, buffer, offset, bytesToCopy); this.preReadOffset += bytesToCopy; this.preReadCount -= bytesToCopy; return bytesToCopy; } return base.Read(buffer, offset, size, timeout); } public override AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state) { ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size); if (this.preReadCount > 0) { int bytesToCopy = Math.Min(size, this.preReadCount); Buffer.BlockCopy(this.preReadData, this.preReadOffset, AsyncReadBuffer, offset, bytesToCopy); this.preReadOffset += bytesToCopy; this.preReadCount -= bytesToCopy; this.asyncBytesRead = bytesToCopy; return AsyncReadResult.Completed; } return base.BeginRead(offset, size, timeout, callback, state); } public override int EndRead() { if (this.asyncBytesRead > 0) { int retValue = this.asyncBytesRead; this.asyncBytesRead = 0; return retValue; } return base.EndRead(); } } class ConnectionStream : Stream { TimeSpan closeTimeout; int readTimeout; int writeTimeout; IConnection connection; bool immediate; public ConnectionStream(IConnection connection, IDefaultCommunicationTimeouts defaultTimeouts) { this.connection = connection; this.closeTimeout = defaultTimeouts.CloseTimeout; this.ReadTimeout = TimeoutHelper.ToMilliseconds(defaultTimeouts.ReceiveTimeout); this.WriteTimeout = TimeoutHelper.ToMilliseconds(defaultTimeouts.SendTimeout); immediate = true; } public IConnection Connection { get { return connection; } } public override bool CanRead { get { return true; } } public override bool CanSeek { get { return false; } } public override bool CanTimeout { get { return true; } } public override bool CanWrite { get { return true; } } public TimeSpan CloseTimeout { get { return closeTimeout; } set { this.closeTimeout = value; } } public override int ReadTimeout { get { return this.readTimeout; } set { if (value < -1) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value, SR.GetString(SR.ValueMustBeInRange, -1, int.MaxValue))); } this.readTimeout = value; } } public override int WriteTimeout { get { return this.writeTimeout; } set { if (value < -1) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value, SR.GetString(SR.ValueMustBeInRange, -1, int.MaxValue))); } this.writeTimeout = value; } } public bool Immediate { get { return immediate; } set { immediate = value; } } public override long Length { get { #pragma warning suppress 56503 // [....], required by the Stream.Length contract throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported))); } } public override long Position { get { #pragma warning suppress 56503 // [....], required by the Stream.Position contract throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported))); } set { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported))); } } public TraceEventType ExceptionEventType { get { return connection.ExceptionEventType; } set { connection.ExceptionEventType = value; } } public void Abort() { connection.Abort(); } public override void Close() { connection.Close(this.CloseTimeout); } public override void Flush() { // NOP } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { return connection.BeginWrite(buffer, offset, count, this.Immediate, TimeoutHelper.FromMilliseconds(this.WriteTimeout), callback, state); } public override void EndWrite(IAsyncResult asyncResult) { connection.EndWrite(asyncResult); } public override void Write(byte[] buffer, int offset, int count) { connection.Write(buffer, offset, count, this.Immediate, TimeoutHelper.FromMilliseconds(this.WriteTimeout)); } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { return new ReadAsyncResult(connection, buffer, offset, count, TimeoutHelper.FromMilliseconds(this.ReadTimeout), callback, state); } public override int EndRead(IAsyncResult asyncResult) { return ReadAsyncResult.End(asyncResult); } public override int Read(byte[] buffer, int offset, int count) { return this.Read(buffer, offset, count, TimeoutHelper.FromMilliseconds(this.ReadTimeout)); } protected int Read(byte[] buffer, int offset, int count, TimeSpan timeout) { return connection.Read(buffer, offset, count, timeout); } public override long Seek(long offset, SeekOrigin origin) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported))); } public override void SetLength(long value) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported))); } public void Shutdown(TimeSpan timeout) { connection.Shutdown(timeout); } public bool Validate(Uri uri) { return connection.Validate(uri); } class ReadAsyncResult : AsyncResult { int bytesRead; byte[] buffer; int offset; IConnection connection; static WaitCallback onAsyncReadComplete; public ReadAsyncResult(IConnection connection, byte[] buffer, int offset, int count, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state) { this.buffer = buffer; this.offset = offset; this.connection = connection; if (onAsyncReadComplete == null) { onAsyncReadComplete = new WaitCallback(OnAsyncReadComplete); } AsyncReadResult readResult = this.connection.BeginRead(0, Math.Min(count, this.connection.AsyncReadBufferSize), timeout, onAsyncReadComplete, this); if (readResult == AsyncReadResult.Completed) { HandleRead(); base.Complete(true); } } void HandleRead() { bytesRead = this.connection.EndRead(); Buffer.BlockCopy(this.connection.AsyncReadBuffer, 0, buffer, offset, bytesRead); } public static int End(IAsyncResult result) { ReadAsyncResult thisPtr = AsyncResult.End(result); return thisPtr.bytesRead; } static void OnAsyncReadComplete(object state) { ReadAsyncResult thisPtr = (ReadAsyncResult)state; Exception completionException = null; try { thisPtr.HandleRead(); } #pragma warning suppress 56500 // [....], transferring exception to another thread catch (Exception e) { if (DiagnosticUtility.IsFatal(e)) { throw; } completionException = e; } thisPtr.Complete(false, completionException); } } } class StreamConnection : IConnection { byte[] asyncReadBuffer; int bytesRead; ConnectionStream innerStream; AsyncCallback onRead; IAsyncResult readResult; WaitCallback readCallback; Stream stream; public StreamConnection(Stream stream, ConnectionStream innerStream) { DiagnosticUtility.DebugAssert(stream != null, "StreamConnection: Stream cannot be null."); DiagnosticUtility.DebugAssert(innerStream != null, "StreamConnection: Inner stream cannot be null."); this.stream = stream; this.innerStream = innerStream; onRead = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnRead)); } public byte[] AsyncReadBuffer { get { if (this.asyncReadBuffer == null) { lock (ThisLock) { if (this.asyncReadBuffer == null) { this.asyncReadBuffer = DiagnosticUtility.Utility.AllocateByteArray(innerStream.Connection.AsyncReadBufferSize); } } } return this.asyncReadBuffer; } } public int AsyncReadBufferSize { get { return innerStream.Connection.AsyncReadBufferSize; } } public Stream Stream { get { return this.stream; } } public object ThisLock { get { return this; } } public TraceEventType ExceptionEventType { get { return innerStream.ExceptionEventType; } set { innerStream.ExceptionEventType = value; } } public IPEndPoint RemoteIPEndPoint { get { #pragma warning suppress 56503 // Not publicly accessible and this should never be called. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); } } public void Abort() { innerStream.Abort(); } Exception ConvertIOException(IOException ioException) { if (ioException.InnerException is TimeoutException) { return new TimeoutException(ioException.InnerException.Message, ioException); } else if (ioException.InnerException is CommunicationObjectAbortedException) { return new CommunicationObjectAbortedException(ioException.InnerException.Message, ioException); } else if (ioException.InnerException is CommunicationException) { return new CommunicationException(ioException.InnerException.Message, ioException); } else { return new CommunicationException(SR.GetString(SR.StreamError), ioException); } } public void Close(TimeSpan timeout) { innerStream.CloseTimeout = timeout; try { stream.Close(); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } } public void Shutdown(TimeSpan timeout) { innerStream.Shutdown(timeout); } public object DuplicateAndClose(int targetProcessId) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); } public virtual object GetCoreTransport() { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); } public bool Validate(Uri uri) { return innerStream.Validate(uri); } public IAsyncResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, AsyncCallback callback, object state) { try { innerStream.Immediate = immediate; SetWriteTimeout(timeout); return stream.BeginWrite(buffer, offset, size, callback, state); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } } public void EndWrite(IAsyncResult result) { try { stream.EndWrite(result); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } } public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout) { try { innerStream.Immediate = immediate; SetWriteTimeout(timeout); stream.Write(buffer, offset, size); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } } public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { Write(buffer, offset, size, immediate, timeout); bufferManager.ReturnBuffer(buffer); } void SetReadTimeout(TimeSpan timeout) { int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout); if (stream.CanTimeout) { stream.ReadTimeout = timeoutInMilliseconds; } innerStream.ReadTimeout = timeoutInMilliseconds; } void SetWriteTimeout(TimeSpan timeout) { int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout); if (stream.CanTimeout) { stream.WriteTimeout = timeoutInMilliseconds; } innerStream.WriteTimeout = timeoutInMilliseconds; } public int Read(byte[] buffer, int offset, int size, TimeSpan timeout) { try { SetReadTimeout(timeout); return stream.Read(buffer, offset, size); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } } public AsyncReadResult BeginRead(int offset, int size, TimeSpan timeout, WaitCallback callback, object state) { ConnectionUtilities.ValidateBufferBounds(AsyncReadBufferSize, offset, size); readCallback = callback; try { SetReadTimeout(timeout); IAsyncResult localResult = stream.BeginRead(AsyncReadBuffer, offset, size, onRead, state); if (!localResult.CompletedSynchronously) { return AsyncReadResult.Queued; } bytesRead = stream.EndRead(localResult); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } return AsyncReadResult.Completed; } public int EndRead() { if (this.readResult != null) { IAsyncResult localResult = this.readResult; this.readResult = null; try { bytesRead = stream.EndRead(localResult); } catch (IOException ioException) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertIOException(ioException)); } } return bytesRead; } void OnRead(IAsyncResult result) { if (result.CompletedSynchronously) { return; } if (this.readResult != null) { DiagnosticUtility.DebugAssert("StreamConnection: OnRead called twice."); throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); } this.readResult = result; readCallback(result.AsyncState); } } class ConnectionMessageProperty { IConnection connection; public ConnectionMessageProperty(IConnection connection) { this.connection = connection; } public static string Name { get { return "iconnection"; } } public IConnection Connection { get { return this.connection; } } } static class ConnectionUtilities { internal static void CloseNoThrow(IConnection connection, TimeSpan timeout) { bool success = false; try { connection.Close(timeout); success = true; } catch (TimeoutException e) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Information); } } catch (CommunicationException e) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Information); } } finally { if (!success) { connection.Abort(); } } } internal static void ValidateBufferBounds(ArraySegment buffer) { ValidateBufferBounds(buffer.Array, buffer.Offset, buffer.Count); } internal static void ValidateBufferBounds(byte[] buffer, int offset, int size) { if (buffer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer"); } ValidateBufferBounds(buffer.Length, offset, size); } internal static void ValidateBufferBounds(int bufferSize, int offset, int size) { if (offset < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", offset, SR.GetString( SR.ValueMustBeNonNegative))); } if (offset > bufferSize) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", offset, SR.GetString( SR.OffsetExceedsBufferSize, bufferSize))); } if (size <= 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, SR.GetString( SR.ValueMustBePositive))); } int remainingBufferSpace = bufferSize - offset; if (size > remainingBufferSpace) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, SR.GetString( SR.SizeExceedsRemainingBufferSpace, remainingBufferSpace))); } } } } // 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
- FamilyMap.cs
- ButtonColumn.cs
- WebConfigurationHost.cs
- BitVector32.cs
- HMAC.cs
- SrgsGrammar.cs
- ControlBuilderAttribute.cs
- TextAdaptor.cs
- RSAProtectedConfigurationProvider.cs
- DataSourceSelectArguments.cs
- SiteMapPathDesigner.cs
- CommandConverter.cs
- SqlCommand.cs
- PtsContext.cs
- BindingContext.cs
- AccessDataSourceView.cs
- VectorCollection.cs
- PackWebResponse.cs
- TdsParserStaticMethods.cs
- ItemType.cs
- EntityParameter.cs
- Environment.cs
- HyperLink.cs
- TextTreeInsertUndoUnit.cs
- SystemDropShadowChrome.cs
- GeneralTransform3DTo2D.cs
- smtppermission.cs
- glyphs.cs
- PolicyValidator.cs
- ImageCodecInfo.cs
- Timer.cs
- DbFunctionCommandTree.cs
- HitTestParameters.cs
- _OSSOCK.cs
- HttpHeaderCollection.cs
- UrlPath.cs
- SchemaElementLookUpTableEnumerator.cs
- DecimalFormatter.cs
- PeerNameResolver.cs
- Util.cs
- NameSpaceEvent.cs
- UndoManager.cs
- ComponentCollection.cs
- VideoDrawing.cs
- CLRBindingWorker.cs
- XPathScanner.cs
- InlineObject.cs
- SslStream.cs
- DashStyle.cs
- DbConnectionOptions.cs
- SQLCharsStorage.cs
- SmiRequestExecutor.cs
- DataSysAttribute.cs
- SizeConverter.cs
- Stacktrace.cs
- WindowsBrush.cs
- TextParagraph.cs
- CodeAttributeDeclarationCollection.cs
- RectAnimationBase.cs
- SQLInt64.cs
- DriveNotFoundException.cs
- DetailsViewPageEventArgs.cs
- CLRBindingWorker.cs
- SingleConverter.cs
- ObjectConverter.cs
- NamespaceCollection.cs
- DataPager.cs
- GradientBrush.cs
- AggregateException.cs
- ValidatingPropertiesEventArgs.cs
- PolyLineSegment.cs
- AdRotator.cs
- FileUtil.cs
- CompositeFontParser.cs
- ProtectedProviderSettings.cs
- CryptoStream.cs
- SimpleTypesSurrogate.cs
- HWStack.cs
- Constraint.cs
- ExpressionPrefixAttribute.cs
- ping.cs
- IBuiltInEvidence.cs
- SiteMap.cs
- Trace.cs
- DataStreams.cs
- AttachedAnnotation.cs
- SubMenuStyleCollection.cs
- path.cs
- TableItemPattern.cs
- OperandQuery.cs
- FindCriteriaApril2005.cs
- SchemaHelper.cs
- XmlILAnnotation.cs
- namescope.cs
- StateMachineWorkflow.cs
- QilUnary.cs
- PeerCollaborationPermission.cs
- WebPart.cs
- SerializationSectionGroup.cs
- EditorZoneBase.cs