Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / BufferedMessageWriter.cs / 1 / BufferedMessageWriter.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.IO; using System.Xml; using System.Diagnostics; abstract class BufferedMessageWriter { int[] sizeHistory; int sizeHistoryIndex; const int sizeHistoryCount = 4; const int expectedSizeVariance = 256; BufferedOutputStream stream; public BufferedMessageWriter() { stream = new BufferedOutputStream(SR.MaxSentMessageSizeExceeded); InitMessagePredicter(); } protected abstract XmlDictionaryWriter TakeXmlWriter(Stream stream); protected abstract void ReturnXmlWriter(XmlDictionaryWriter writer); public ArraySegmentWriteMessage(Message message, BufferManager bufferManager, int initialOffset, int maxSizeQuota) { int effectiveMaxSize; // make sure that maxSize has room for initialOffset without overflowing, since // the effective buffer size is message size + initialOffset if (maxSizeQuota <= int.MaxValue - initialOffset) effectiveMaxSize = maxSizeQuota + initialOffset; else effectiveMaxSize = int.MaxValue; int predictedMessageSize = PredictMessageSize(); if (predictedMessageSize > effectiveMaxSize) predictedMessageSize = effectiveMaxSize; else if (predictedMessageSize < initialOffset) predictedMessageSize = initialOffset; try { stream.Init(predictedMessageSize, maxSizeQuota, effectiveMaxSize, bufferManager); stream.Skip(initialOffset); XmlDictionaryWriter writer = TakeXmlWriter(stream); OnWriteStartMessage(writer); message.WriteMessage(writer); OnWriteEndMessage(writer); writer.Flush(); ReturnXmlWriter(writer); int size; byte[] buffer = stream.ToArray(out size); RecordActualMessageSize(size); return new ArraySegment (buffer, initialOffset, size - initialOffset); } finally { stream.Clear(); } } protected virtual void OnWriteStartMessage(XmlDictionaryWriter writer) { } protected virtual void OnWriteEndMessage(XmlDictionaryWriter writer) { } void InitMessagePredicter() { sizeHistory = new int[4]; for (int i = 0; i < sizeHistoryCount; i++) sizeHistory[i] = 256; } int PredictMessageSize() { int max = 0; for (int i = 0; i < sizeHistoryCount; i++) if (sizeHistory[i] > max) max = sizeHistory[i]; return max + expectedSizeVariance; } void RecordActualMessageSize(int size) { sizeHistory[sizeHistoryIndex] = size; sizeHistoryIndex = (sizeHistoryIndex + 1) % sizeHistoryCount; } } } // 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
- XamlSerializationHelper.cs
- ComponentResourceKeyConverter.cs
- HandlerWithFactory.cs
- SecurityManager.cs
- QualificationDataAttribute.cs
- PropertyFilter.cs
- TitleStyle.cs
- PropertyGroupDescription.cs
- NotFiniteNumberException.cs
- EndPoint.cs
- ApplicationDirectory.cs
- ItemList.cs
- DataGridViewColumn.cs
- BitArray.cs
- OletxVolatileEnlistment.cs
- Pipe.cs
- TextServicesLoader.cs
- NativeWrapper.cs
- AlphabeticalEnumConverter.cs
- CellIdBoolean.cs
- LocatorPartList.cs
- ServiceDeploymentInfo.cs
- ProviderSettingsCollection.cs
- SerialStream.cs
- TypeDefinition.cs
- controlskin.cs
- DataGridLinkButton.cs
- ProxyWebPartManager.cs
- ToolStripDropDownButton.cs
- XmlException.cs
- RelationshipSet.cs
- ArgIterator.cs
- XmlSchemaProviderAttribute.cs
- KeyNotFoundException.cs
- SiteMapProvider.cs
- FileUpload.cs
- IisTraceWebEventProvider.cs
- DiscoveryClientProtocol.cs
- InfiniteTimeSpanConverter.cs
- ProfileEventArgs.cs
- WizardStepBase.cs
- DateTimePicker.cs
- httpstaticobjectscollection.cs
- ByteStreamGeometryContext.cs
- HintTextMaxWidthConverter.cs
- Error.cs
- Border.cs
- FontStretchConverter.cs
- HttpCacheVary.cs
- CodeMethodReturnStatement.cs
- DesignerHelpers.cs
- FixedLineResult.cs
- XamlBrushSerializer.cs
- AddInSegmentDirectoryNotFoundException.cs
- SystemFonts.cs
- AxHost.cs
- BulletChrome.cs
- diagnosticsswitches.cs
- AuthenticationException.cs
- SubclassTypeValidator.cs
- DataView.cs
- CacheHelper.cs
- SiteMapNodeItem.cs
- ControlCachePolicy.cs
- IgnoreDeviceFilterElement.cs
- SequenceNumber.cs
- TextEditorCharacters.cs
- HelloMessageApril2005.cs
- DataGridViewAdvancedBorderStyle.cs
- StdValidatorsAndConverters.cs
- TreeNodeEventArgs.cs
- ConfigXmlElement.cs
- ListParagraph.cs
- NavigationHelper.cs
- ImmutableCollection.cs
- TextTreeRootTextBlock.cs
- FtpWebRequest.cs
- RegexTypeEditor.cs
- OleDbCommand.cs
- Set.cs
- PassportAuthentication.cs
- BinarySerializer.cs
- FlowDocumentPaginator.cs
- DesignerAttribute.cs
- TemplateField.cs
- KeyTime.cs
- GridEntryCollection.cs
- ClientUtils.cs
- WpfKnownTypeInvoker.cs
- WindowsSspiNegotiation.cs
- XmlNodeWriter.cs
- ConvertEvent.cs
- FragmentQueryProcessor.cs
- UnsafeNativeMethods.cs
- InputScopeNameConverter.cs
- ButtonChrome.cs
- StringBuilder.cs
- ExpandCollapseProviderWrapper.cs
- CapabilitiesState.cs
- InvokeMethod.cs