Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / MaxMessageSizeStream.cs / 1 / MaxMessageSizeStream.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.IO; using System.ServiceModel; using System.Diagnostics; class MaxMessageSizeStream : DelegatingStream { long maxMessageSize; long totalBytesRead; long bytesWritten; public MaxMessageSizeStream(Stream stream, long maxMessageSize) : base(stream) { this.maxMessageSize = maxMessageSize; } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { count = PrepareRead(count); return base.BeginRead(buffer, offset, count, callback, state); } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { PrepareWrite(count); return base.BeginWrite(buffer, offset, count, callback, state); } public override int EndRead(IAsyncResult result) { return FinishRead(base.EndRead(result)); } public override int Read(byte[] buffer, int offset, int count) { count = PrepareRead(count); return FinishRead(base.Read(buffer, offset, count)); } public override int ReadByte() { PrepareRead(1); int i = base.ReadByte(); if (i != -1) FinishRead(1); return i; } public override void Write(byte[] buffer, int offset, int count) { PrepareWrite(count); base.Write(buffer, offset, count); } public override void WriteByte(byte value) { PrepareWrite(1); base.WriteByte(value); } internal static Exception CreateMaxReceivedMessageSizeExceededException(long maxMessageSize) { string message = SR.GetString(SR.MaxReceivedMessageSizeExceeded, maxMessageSize); Exception inner = new QuotaExceededException(message); return new CommunicationException(message, inner); } internal static Exception CreateMaxSentMessageSizeExceededException(long maxMessageSize) { string message = SR.GetString(SR.MaxSentMessageSizeExceeded, maxMessageSize); Exception inner = new QuotaExceededException(message); return new CommunicationException(message, inner); } int PrepareRead(int bytesToRead) { if (totalBytesRead >= maxMessageSize) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateMaxReceivedMessageSizeExceededException(maxMessageSize)); } long bytesRemaining = maxMessageSize - totalBytesRead; if (bytesRemaining > int.MaxValue) { return bytesToRead; } else { return Math.Min(bytesToRead, (int)(maxMessageSize - totalBytesRead)); } } int FinishRead(int bytesRead) { totalBytesRead += bytesRead; return bytesRead; } void PrepareWrite(int bytesToWrite) { if (bytesWritten + bytesToWrite > maxMessageSize) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateMaxSentMessageSizeExceededException(maxMessageSize)); } bytesWritten += bytesToWrite; } } } // 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
- CodeGeneratorOptions.cs
- SizeConverter.cs
- TreeViewBindingsEditorForm.cs
- BinaryKeyIdentifierClause.cs
- Int32CollectionValueSerializer.cs
- TreeNodeMouseHoverEvent.cs
- EmptyControlCollection.cs
- ExpressionNode.cs
- ArgumentOutOfRangeException.cs
- XmlSchemaNotation.cs
- ConfigXmlWhitespace.cs
- ProxyRpc.cs
- ObjectDataSourceDisposingEventArgs.cs
- DeclarativeCatalogPart.cs
- Publisher.cs
- HtmlButton.cs
- BuildProvidersCompiler.cs
- SvcMapFileSerializer.cs
- HostVisual.cs
- Visitor.cs
- CultureSpecificStringDictionary.cs
- StaticTextPointer.cs
- DebugView.cs
- ObservableCollection.cs
- ExpressionEditorAttribute.cs
- DbConnectionInternal.cs
- TabletDevice.cs
- CriticalHandle.cs
- ExclusiveTcpTransportManager.cs
- GridViewSelectEventArgs.cs
- XmlSchemaAny.cs
- IResourceProvider.cs
- AllMembershipCondition.cs
- BrowserTree.cs
- DocumentGridContextMenu.cs
- Point4D.cs
- SettingsContext.cs
- ChooseAction.cs
- SafeWaitHandle.cs
- Repeater.cs
- jithelpers.cs
- SecurityManager.cs
- COM2PropertyDescriptor.cs
- Unit.cs
- BaseCodeDomTreeGenerator.cs
- SiteMapNodeCollection.cs
- ScriptServiceAttribute.cs
- BigInt.cs
- BaseProcessor.cs
- DataStreams.cs
- Sql8ExpressionRewriter.cs
- WebPartRestoreVerb.cs
- AuthorizationSection.cs
- EventArgs.cs
- CompositeCollection.cs
- SafeEventHandle.cs
- ControlBindingsCollection.cs
- NameValuePair.cs
- ValueSerializer.cs
- CategoryEditor.cs
- TimeSpanStorage.cs
- SHA512Managed.cs
- TimeZone.cs
- FontStyles.cs
- ReflectionPermission.cs
- ConnectionStringsExpressionBuilder.cs
- BlobPersonalizationState.cs
- StylusEditingBehavior.cs
- _LazyAsyncResult.cs
- StringAttributeCollection.cs
- ManifestBasedResourceGroveler.cs
- SystemUnicastIPAddressInformation.cs
- WebPartConnectionsCancelEventArgs.cs
- Polyline.cs
- RegistryConfigurationProvider.cs
- CurrentChangingEventArgs.cs
- FieldNameLookup.cs
- SystemDiagnosticsSection.cs
- ListBase.cs
- ImageDrawing.cs
- PrinterResolution.cs
- ReceiveContext.cs
- RuleElement.cs
- TypeUtils.cs
- NonSerializedAttribute.cs
- SqlWriter.cs
- XmlHierarchyData.cs
- TypeReference.cs
- ScriptMethodAttribute.cs
- TimeoutValidationAttribute.cs
- Encoding.cs
- AttributeEmitter.cs
- InvariantComparer.cs
- SafeRightsManagementHandle.cs
- StringDictionary.cs
- Number.cs
- TextSchema.cs
- Column.cs
- BindToObject.cs
- LiteralDesigner.cs