Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.ServiceModel.Channels / System / ServiceModel / Channels / ByteStreamMessage.cs / 1305376 / ByteStreamMessage.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System; using System.IO; using System.Runtime; using System.Xml; class ByteStreamMessage : Message { BodyWriter bodyWriter; MessageHeaders headers; MessageProperties properties; XmlDictionaryReader reader; public ByteStreamMessage(ByteStreamBufferedMessageData bufferedMessageData, XmlDictionaryReaderQuotas quota) { // Assign both writer and reader here so that we can CreateBufferedCopy without the need to // abstract between a streamed or buffered message. We're protected here by the state on Message // preventing both a read/write. this.bodyWriter = new BufferedBodyWriter(bufferedMessageData); this.headers = new MessageHeaders(MessageVersion.None); this.properties = new MessageProperties(); this.reader = new XmlBufferedByteStreamReader(bufferedMessageData, quota); } public ByteStreamMessage(Stream stream, XmlDictionaryReaderQuotas quota) { // Assign both writer and reader here so that we can CreateBufferedCopy without the need to // abstract between a streamed or buffered message. We're protected here by the state on Message // preventing both a read/write on the same stream. if (quota == null) { quota = EncoderDefaults.ReaderQuotas; } this.bodyWriter = new StreamedBodyWriter(stream); this.headers = new MessageHeaders(MessageVersion.None); this.properties = new MessageProperties(); this.reader = new XmlStreamedByteStreamReader(stream, quota); } ByteStreamMessage(ByteStreamBufferedMessageData messageData, MessageHeaders headers, MessageProperties properties, XmlDictionaryReaderQuotas quotas) { this.headers = new MessageHeaders(headers); this.properties = new MessageProperties(properties); this.bodyWriter = new BufferedBodyWriter(messageData); this.reader = new XmlBufferedByteStreamReader(messageData, quotas); } public override MessageHeaders Headers { get { if (this.IsDisposed) { throw FxTrace.Exception.ObjectDisposed(SR.ObjectDisposed("message")); } return this.headers; } } public override bool IsEmpty { get { if (this.IsDisposed) { throw FxTrace.Exception.ObjectDisposed(SR.ObjectDisposed("message")); } return false; } } public override bool IsFault { get { if (this.IsDisposed) { throw FxTrace.Exception.ObjectDisposed(SR.ObjectDisposed("message")); } return false; } } public override MessageProperties Properties { get { if (this.IsDisposed) { throw FxTrace.Exception.ObjectDisposed(SR.ObjectDisposed("message")); } return this.properties; } } public override MessageVersion Version { get { if (this.IsDisposed) { throw FxTrace.Exception.ObjectDisposed(SR.ObjectDisposed("message")); } return MessageVersion.None; } } protected override void OnBodyToString(XmlDictionaryWriter writer) { if (this.bodyWriter.IsBuffered) { bodyWriter.WriteBodyContents(writer); } else { writer.WriteString(SR.MessageBodyIsStream); } } protected override void OnClose() { Exception ex = null; try { base.OnClose(); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } ex = e; } try { if (properties != null) { properties.Dispose(); } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } if (ex == null) { ex = e; } } try { if (reader != null) { reader.Close(); } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } if (ex == null) { ex = e; } } if (ex != null) { throw FxTrace.Exception.AsError(ex); } this.bodyWriter = null; } protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize) { BufferedBodyWriter bufferedBodyWriter; if (this.bodyWriter.IsBuffered) { // Can hand this off in buffered case without making a new one. bufferedBodyWriter = (BufferedBodyWriter)this.bodyWriter; } else { bufferedBodyWriter = (BufferedBodyWriter)this.bodyWriter.CreateBufferedCopy(maxBufferSize); } // Protected by Message state to be called only once. this.bodyWriter = null; return new ByteStreamMessageBuffer(bufferedBodyWriter.MessageData, this.headers, this.properties, this.reader.Quotas); } protected override XmlDictionaryReader OnGetReaderAtBodyContents() { XmlDictionaryReader r = this.reader; this.reader = null; return r; } protected override void OnWriteBodyContents(XmlDictionaryWriter writer) { this.bodyWriter.WriteBodyContents(writer); } class BufferedBodyWriter : BodyWriter { ByteStreamBufferedMessageData bufferedMessageData; public BufferedBodyWriter(ByteStreamBufferedMessageData bufferedMessageData) : base(true) { this.bufferedMessageData = bufferedMessageData; } internal ByteStreamBufferedMessageData MessageData { get { return bufferedMessageData; } } protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) { // Never called because when copying a Buffered message, we simply hand off the existing BodyWriter // to the new message. Fx.Assert(false, "This is never called"); return null; } protected override void OnWriteBodyContents(XmlDictionaryWriter writer) { writer.WriteStartElement(ByteStreamMessageUtility.StreamElementName, string.Empty); writer.WriteBase64(this.bufferedMessageData.Buffer, 0, this.bufferedMessageData.Count); writer.WriteEndElement(); } } class StreamedBodyWriter : BodyWriter { Stream stream; public StreamedBodyWriter(Stream stream) : base(false) { this.stream = stream; } // OnCreateBufferedCopy / OnWriteBodyContents can only be called once - protected by state on Message (either copied or written once) protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) { using (BufferManagerOutputStream bufferedStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExceeded("{0}"), maxBufferSize)) { using (XmlDictionaryWriter writer = new XmlByteStreamWriter(bufferedStream, true)) { OnWriteBodyContents(writer); writer.Flush(); int size; byte[] bytesArray = bufferedStream.ToArray(out size); ByteStreamBufferedMessageData bufferedMessageData = new ByteStreamBufferedMessageData(bytesArray, size); return new BufferedBodyWriter(bufferedMessageData); } } } // OnCreateBufferedCopy / OnWriteBodyContents can only be called once - protected by state on Message (either copied or written once) protected override void OnWriteBodyContents(XmlDictionaryWriter writer) { writer.WriteStartElement(ByteStreamMessageUtility.StreamElementName, string.Empty); writer.WriteValue(new ByteStreamStreamProvider(this.stream)); writer.WriteEndElement(); } class ByteStreamStreamProvider : IStreamProvider { Stream stream; internal ByteStreamStreamProvider(Stream stream) { this.stream = stream; } public Stream GetStream() { return stream; } public void ReleaseStream(Stream stream) { //Noop } } } class ByteStreamMessageBuffer : MessageBuffer { bool closed; MessageHeaders headers; ByteStreamBufferedMessageData messageData; MessageProperties properties; XmlDictionaryReaderQuotas quotas; object thisLock = new object(); public ByteStreamMessageBuffer(ByteStreamBufferedMessageData messageData, MessageHeaders headers, MessageProperties properties, XmlDictionaryReaderQuotas quotas) : base() { this.messageData = messageData; this.headers = new MessageHeaders(headers); this.properties = new MessageProperties(properties); this.quotas = new XmlDictionaryReaderQuotas(); quotas.CopyTo(this.quotas); this.messageData.Open(); } public override int BufferSize { get { return this.messageData.Count; } } object ThisLock { get { return this.thisLock; } } public override void Close() { lock (ThisLock) { if (!closed) { closed = true; this.headers = null; if (properties != null) { properties.Dispose(); properties = null; } this.messageData.Close(); this.messageData = null; this.quotas = null; } } } public override Message CreateMessage() { lock (ThisLock) { if (closed) { throw FxTrace.Exception.ObjectDisposed(SR.ObjectDisposed("message")); } return new ByteStreamMessage(this.messageData, this.headers, this.properties, this.quotas); } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- NativeMethods.cs
- ReadOnlyHierarchicalDataSource.cs
- HelpInfo.cs
- RadioButtonPopupAdapter.cs
- ObjectStateEntryDbUpdatableDataRecord.cs
- ClosableStream.cs
- HtmlCommandAdapter.cs
- QueryResults.cs
- MatrixTransform.cs
- SQLDoubleStorage.cs
- Missing.cs
- UniqueConstraint.cs
- Group.cs
- InvalidAsynchronousStateException.cs
- CodeStatement.cs
- MarkupExtensionParser.cs
- HandledMouseEvent.cs
- DataGridCell.cs
- Attributes.cs
- DbLambda.cs
- ForceCopyBuildProvider.cs
- PropertyInformationCollection.cs
- SEHException.cs
- X509ChainElement.cs
- VisualStyleRenderer.cs
- LOSFormatter.cs
- MessagePropertyFilter.cs
- XamlTypeMapperSchemaContext.cs
- KeyedQueue.cs
- SequentialActivityDesigner.cs
- IPAddress.cs
- ImageProxy.cs
- PersonalizationStateInfoCollection.cs
- SecurityRuntime.cs
- RemoteWebConfigurationHost.cs
- NamespaceQuery.cs
- SqlAliaser.cs
- WindowsToolbarAsMenu.cs
- CommonXSendMessage.cs
- Model3DCollection.cs
- ObjectDataSourceDesigner.cs
- ReadWriteSpinLock.cs
- XmlSchemaComplexType.cs
- GradientStop.cs
- ObjectHelper.cs
- ConfigXmlCDataSection.cs
- WebPartsSection.cs
- ElapsedEventArgs.cs
- ActivityStatusChangeEventArgs.cs
- ImmutableObjectAttribute.cs
- AnonymousIdentificationSection.cs
- LookupNode.cs
- UriTemplateLiteralQueryValue.cs
- XmlNodeChangedEventArgs.cs
- PasswordBox.cs
- XAMLParseException.cs
- ReadOnlyCollection.cs
- FixedDSBuilder.cs
- KnownTypesHelper.cs
- ListViewItemSelectionChangedEvent.cs
- ContainerSelectorGlyph.cs
- SafeReversePInvokeHandle.cs
- Image.cs
- AxHost.cs
- DataKey.cs
- CssClassPropertyAttribute.cs
- DataSourceXmlAttributeAttribute.cs
- Size3DConverter.cs
- MenuEventArgs.cs
- Point4DConverter.cs
- ObjectHandle.cs
- WebPartPersonalization.cs
- DiscoveryEndpoint.cs
- BufferedGraphics.cs
- HeaderedItemsControl.cs
- SmtpFailedRecipientsException.cs
- DetailsViewInsertedEventArgs.cs
- RunInstallerAttribute.cs
- DataGridColumnStyleMappingNameEditor.cs
- ClientOperation.cs
- SiteMapPath.cs
- RefreshEventArgs.cs
- FileChangesMonitor.cs
- CreateCardRequest.cs
- StyleXamlParser.cs
- ReferentialConstraint.cs
- XamlPointCollectionSerializer.cs
- DataContractFormatAttribute.cs
- RelationshipConstraintValidator.cs
- CheckBoxField.cs
- SqlDataSourceCommandEventArgs.cs
- BuildProvidersCompiler.cs
- Calendar.cs
- TraceInternal.cs
- CodePageEncoding.cs
- WithStatement.cs
- ToolStripRendererSwitcher.cs
- EtwTrace.cs
- UseAttributeSetsAction.cs
- TimeSpan.cs