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
- ControlBuilder.cs
- UnsafeNativeMethods.cs
- PromptEventArgs.cs
- WebServicesSection.cs
- MobileUITypeEditor.cs
- SynchronizationContext.cs
- ConvertEvent.cs
- BatchServiceHost.cs
- ReadOnlyCollection.cs
- Literal.cs
- ReverseInheritProperty.cs
- AllMembershipCondition.cs
- AddInBase.cs
- EntityDataSourceUtil.cs
- COAUTHINFO.cs
- ProviderMetadata.cs
- SessionIDManager.cs
- XamlBrushSerializer.cs
- Stacktrace.cs
- ConfigurationLoaderException.cs
- IdnMapping.cs
- DataServiceClientException.cs
- UnsafeNativeMethods.cs
- MULTI_QI.cs
- PeerApplication.cs
- WebPartZoneDesigner.cs
- TextRangeBase.cs
- InvalidCardException.cs
- ConfigurationValidatorBase.cs
- Pair.cs
- InkPresenter.cs
- ParameterCollection.cs
- QilParameter.cs
- DataStreams.cs
- RecordConverter.cs
- ServiceConfigurationTraceRecord.cs
- KoreanCalendar.cs
- AddInActivator.cs
- ArcSegment.cs
- DrawingContextFlattener.cs
- SessionPageStatePersister.cs
- ExpressionHelper.cs
- WindowsComboBox.cs
- DataColumnMapping.cs
- IconEditor.cs
- PageCache.cs
- SqlTriggerAttribute.cs
- ContentOperations.cs
- SmtpFailedRecipientsException.cs
- WindowsFormsLinkLabel.cs
- TreeChangeInfo.cs
- RouteParameter.cs
- LockedHandleGlyph.cs
- RectAnimationBase.cs
- URL.cs
- TextSimpleMarkerProperties.cs
- CodeLabeledStatement.cs
- StreamSecurityUpgradeProvider.cs
- InvokeSchedule.cs
- ScriptingScriptResourceHandlerSection.cs
- CheckBox.cs
- MimeMultiPart.cs
- AssertFilter.cs
- MaterialGroup.cs
- MemberMemberBinding.cs
- Stacktrace.cs
- X509CertificateValidationMode.cs
- ReadOnlyAttribute.cs
- _IPv4Address.cs
- SmtpCommands.cs
- AsyncPostBackTrigger.cs
- Encoder.cs
- EFTableProvider.cs
- ImpersonateTokenRef.cs
- HtmlListAdapter.cs
- MatrixAnimationUsingKeyFrames.cs
- AssemblyNameUtility.cs
- AutoScrollExpandMessageFilter.cs
- TdsRecordBufferSetter.cs
- ErrorInfoXmlDocument.cs
- DelayedRegex.cs
- _SSPIWrapper.cs
- ObjectListTitleAttribute.cs
- OLEDB_Enum.cs
- UriSection.cs
- UdpAnnouncementEndpoint.cs
- TextEditorDragDrop.cs
- HelpPage.cs
- CommentEmitter.cs
- AssemblyResourceLoader.cs
- SiteMap.cs
- MimeMapping.cs
- XmlDataSourceView.cs
- IRCollection.cs
- Code.cs
- ClientRolePrincipal.cs
- HwndAppCommandInputProvider.cs
- MatrixAnimationUsingKeyFrames.cs
- CryptoSession.cs
- PropertyBuilder.cs