Welcome to Windows Communication Foundation (WCF)
Top Tasks :

WCF Team Bloggers

Browse by Tags

All Tags » Channel Authors » Samples   (RSS)

  • ROT 128 Stream Upgrade Sample, Part 4

    The final pieces needed for the ROT 128 sample are a stream upgrade initiator and a stream upgrade acceptor. The initiator starts the upgrade process by providing an upgrade type string from GetNextUpgrade. I've coded this so that the initiator and acceptor share a type string that is stored as a static member back on the binding element . You can produce or store your upgrade type string however you want. This is an opaque string to the runtime. using System; using System.IO; using System.ServiceModel; using System.ServiceModel.Channels; namespace Microsoft.ServiceModel.Samples { class InitiateAsyncResult : TypedCompletedAsyncResult<Stream> { internal InitiateAsyncResult(Stream stream, AsyncCallback callback, object state) : base (stream, callback, state) { } } class ROT128StreamUpgradeInitiator : StreamUpgradeInitiator { ROT128StreamUpgradeProvider provider; string nextUpgrade = ROT128StreamUpgradeBindingElement.ROT128UpgradeType; internal ROT128StreamUpgradeInitiator(ROT128StreamUpgradeProvider provider, EndpointAddress remoteAddress, Uri via) : base () { this .provider = provider; } public override IAsyncResult BeginInitiateUpgrade(Stream stream, AsyncCallback callback, object state) { return new InitiateAsyncResult( new ROT128Stream(stream), callback, state); } public override Stream EndInitiateUpgrade(IAsyncResult result) { return ((InitiateAsyncResult)result).Data; } public override string GetNextUpgrade() { string result = nextUpgrade; nextUpgrade = null ; return result; } public override Stream InitiateUpgrade(Stream stream) { return new ROT128Stream(stream); } } } Each time that GetNextUpgrade is called, you're expected to provide a different upgrade type string if you support multiple upgrades. Once you're out of upgrade types to suggest, GetNextUpgrade should return null forever afterwards. This sample only supports a single upgrade type. At some point in the future, after the upgrade is accepted, you'll get a call on InitiateUpgrade to actually perform the Stream transformation. There's no type string given to InitiateUpgrade so, implicitly, calling InitiateUpgrade means to perform the upgrade for the last upgrade type that you passed out of GetNextUpgrade. To get to the point where you're transforming Streams, you first need to make it through the upgrade acceptor. The upgrade acceptor takes an upgrade type string argument to the CanUpgrade method and returns whether it recognizes this upgrade type. If CanUpgrade returns false, then the Read More...
  • ROT 128 Stream Upgrade Sample, Part 3

    Last time, we built the binding element for the stream upgrade sample . The job of the binding element was to stash itself away in the binding context so that the transport could later pull out the stream upgrade and build the provider. This time we'll look at the implementation of the stream upgrade provider. The stream upgrade provider needs to do three things: Build the upgrade initiator for the client side of the connection. The initiator gets the remote address and via of the connection so that we can differentiate behavior based on where the connection is actually going. Build the upgrade acceptor for the server side of the connection. The acceptor and initiator pieces are the first time we have asymmetry in this simple example. Fundamentally, the initiator and acceptor use a request-reply exchange regardless of the shape of the contract or connection. This means that you're always going to get asymmetry at least at this level. Run any part of the protocol that happens independently from making the individual connections. These actions go in the Open, Close, and Abort methods of the provider. I've given all of these empty implementations because this sample doesn't need to do anything here. If you had resources allocated at pre-connect time, such as a socket connection, you would do the allocation and deallocation in these methods. using System; using System.ServiceModel; using System.ServiceModel.Channels; namespace Microsoft.ServiceModel.Samples { class OpenAsyncResult : CompletedAsyncResult { internal OpenAsyncResult(AsyncCallback callback, object state) : base (callback, state) { } } class CloseAsyncResult : CompletedAsyncResult { internal CloseAsyncResult(AsyncCallback callback, object state) : base (callback, state) { } } class ROT128StreamUpgradeProvider : StreamUpgradeProvider { public ROT128StreamUpgradeProvider(ROT128StreamUpgradeBindingElement element, BindingContext context) : base (context.Binding) { } public override StreamUpgradeAcceptor CreateUpgradeAcceptor() { return new ROT128StreamUpgradeAcceptor( this ); } public override StreamUpgradeInitiator CreateUpgradeInitiator(EndpointAddress remoteAddress, Uri via) { return new ROT128StreamUpgradeInitiator( this , remoteAddress, via); } protected override void OnAbort() { } protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state) { return new CloseAsyncResult(callback, state); } protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback Read More...
  • ROT 128 Stream Upgrade Sample, Part 2

    Building a stream upgrade for ROT 128 starts with creating a binding element to put in the channel stack. This binding element extends the special StreamUpgradeBindingElement base class , which functions very similarly to the specialized binding element base classes for transports and message encoders. We then need to override the channel factory and listener build processes because the stream upgrade binding element does not actually generate a channel. Stream upgrades are handled internally by the transport if it supports them. using System; using System.ServiceModel.Channels; namespace Microsoft.ServiceModel.Samples { public class ROT128StreamUpgradeBindingElement : StreamUpgradeBindingElement { internal static string ROT128UpgradeType = "application/rot128" ; public ROT128StreamUpgradeBindingElement() : base () { } protected ROT128StreamUpgradeBindingElement(ROT128StreamUpgradeBindingElement copyFrom) : base (copyFrom) { } public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context) { context.BindingParameters.Add( this ); return context.BuildInnerChannelFactory<TChannel>(); } public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context) { context.BindingParameters.Add( this ); return context.BuildInnerChannelListener<TChannel>(); } public override StreamUpgradeProvider BuildClientStreamUpgradeProvider(BindingContext context) { return new ROT128StreamUpgradeProvider( this , context); } public override StreamUpgradeProvider BuildServerStreamUpgradeProvider(BindingContext context) { return new ROT128StreamUpgradeProvider( this , context); } public override bool CanBuildChannelFactory<TChannel>(BindingContext context) { context.BindingParameters.Add( this ); return context.CanBuildInnerChannelFactory<TChannel>(); } public override bool CanBuildChannelListener<TChannel>(BindingContext context) { context.BindingParameters.Add( this ); return context.CanBuildInnerChannelListener<TChannel>(); } public override BindingElement Clone() { return new ROT128StreamUpgradeBindingElement( this ); } public override T GetProperty<T>(BindingContext context) { return context.GetInnerProperty<T>(); } } } The only other important task when writing a stream upgrade binding element is to build your stream upgrade provider when asked by the transport. ROT 128 works the same on both the client and server so I've made those methods do exactly Read More...
  • ROT 128 Stream Upgrade Sample, Part 1

    The mission for the next five days is to build and demonstrate an implementation of a stream upgrade. For review, a stream upgrade is a component that plugs into the transport and rewrites the byte stream as it goes on and off of the network. Stream upgrades are stackable and composable. You add stream upgrades by including stream upgrade binding elements in the desired order in the binding. I went over the basics of stream upgrades a few weeks ago. Here are the articles in that series: Stream Upgrades, Part 1 Stream Upgrades, Part 2 Stream Upgrades, Part 3 The example I've picked out is a stream upgrade that applies ROT 128 (ROTate by 128). ROT 13 is a famous example of a Caesar cipher that occludes messages by replacing letters in the English alphabet with the letter that is 13 places higher. When you go past 'Z', you wrap back around to 'A'. Since there are 26 letters, applying ROT 13 twice returns a letter back to where it started. ROT 128 is the equivalent for a single byte value. Here is the stream class that I want to have my messages run through. It includes some debugging so that we can see the messages as they go in and out. using System; using System.IO; namespace Microsoft.ServiceModel.Samples { class ROT128Stream : Stream { Stream stream; public ROT128Stream(Stream stream) { this .stream = stream; } public override bool CanRead { get { return this .stream.CanRead; } } public override bool CanSeek { get { return this .stream.CanSeek; } } public override bool CanWrite { get { return this .stream.CanWrite; } } protected override void Dispose( bool disposing) { if (disposing) { this .stream.Dispose(); } else { this .stream.Close(); } base .Dispose(disposing); } public override void Flush() { this .stream.Flush(); } public override long Length { get { return this .stream.Length; } } public override long Position { get { return this .stream.Position; } set { this .stream.Position = value ; } } static void DumpBuffer( byte [] buffer, int offset, int count) { int pos = 0; while (pos < count) { int lineCount = count - pos; if (lineCount > 15) { lineCount = 15; } for ( int linePos = 0; linePos < lineCount; linePos++) { Console.Write( " {0:X2}" , buffer[offset + pos + linePos]); } for ( int linePos = 15 - lineCount; linePos >= 0; linePos--) { Console.Write( " " ); } for ( int linePos = 0; linePos < lineCount; linePos++) { byte item = buffer[offset + pos + linePos]; if (item < 0x20 || item >= 0x80) { Console.Write( "." ); } else { Console.Write(( Read More...
  • ReplyMangler Channel

    To finish up the series on one-way HTTP requests , I promised to supply a custom channel that fixes the scenario of using the POX message encoder together with one-way requests. This is primarily a code post since most of the interesting discussion is already taken care of. I'm supplying a binding element, channel factory, and request channel. There isn't a channel listener or reply channel because there's no conflict on the server-side between one-way and POX. It's only the client side that requires this patch. Stick the binding element in your channel stack between the transport and the OneWay channel. CustomBinding binding = new CustomBinding( new OneWayBindingElement(), new ReplyMangler(), new TextMessageEncodingBindingElement(), new HttpTransportBindingElement() ); The channel code is mostly plumbing to make the reply run through the FilterMessage method that I posted last time. I spent less than 15 seconds testing this so it would be unwise to drop the code directly into your production system. It does appear however to do the proper job of swallowing messages that come back from the POX message encoder. class ReplyManglerChannel : ChannelBase, IRequestChannel { IRequestChannel innerChannel; public ReplyManglerChannel(ChannelManagerBase channelManager, IRequestChannel innerChannel) : base (channelManager) { this .innerChannel = innerChannel; } public IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, object state) { return innerChannel.BeginRequest(message, timeout, callback, state); } public IAsyncResult BeginRequest(Message message, AsyncCallback callback, object state) { return BeginRequest(message, DefaultSendTimeout, callback, state); } public Message EndRequest(IAsyncResult result) { return FilterMessage(innerChannel.EndRequest(result)); } Message FilterMessage(Message reply) { if (reply == null ) { return null ; } HttpResponseMessageProperty properties = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name]; if (properties != null && properties.StatusCode == HttpStatusCode.Accepted) { return null ; } return reply; } protected override void OnAbort() { innerChannel.Abort(); } protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state) { return innerChannel.BeginClose(timeout, callback, state); } protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) { return innerChannel.BeginOpen(timeout, callback, Read More...

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us