Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C++

Reliable Multicast with PGM and WCF

Rate me:
Please Sign up or sign in to vote.
4.81/5 (17 votes)
1 Mar 2010CPOL6 min read 80.5K   1.6K   35  
PGM is a transport layer protocol which interfaces directly with network layer using raw sockets. In this article, we will see how to implement a reliable multicast with PGM and WCF.
using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using PgmTransport.Configuration;
using PgmTransport.Sockets;


namespace PgmTransport
{
   class PgmTransportBindingElement : TransportBindingElement, IWsdlExportExtension, IPolicyExportExtension
   {
      public PgmTransportBindingElement()
      {
      }

      public PgmTransportBindingElement(PgmTransportBindingElement other)
         : base(other)
      {
         this.FecMode = other.FecMode;
         this.SendRate = other.SendRate;
         this.LateJoin = other.LateJoin;
         this.SetMessageBoundary = other.SetMessageBoundary;
         this.DataMode = other.DataMode;
         this.SenderWidnowAdvance = other.SenderWidnowAdvance;
         this.MulticastTTL = other.MulticastTTL;
         this.SenderInterface = other.SenderInterface;
      }

      public override string Scheme
      {
         get 
         {
            return PgmConstants.Scheme;
         }
      }

      public FecMode FecMode { get; set; }
      public long SendRate { get; set; }
      public int LateJoin { get; set; }
      public bool SetMessageBoundary { get; set; }
      public DataMode DataMode { get; set; }
      public int SenderWidnowAdvance { get; set; }
      public string SenderInterface { get; set; }
      public int MulticastTTL { get; set; }

      public override T GetProperty<T>(BindingContext context)
      {
         if (context == null)
         {
            throw new ArgumentNullException("context");
         }

         return context.GetInnerProperty<T>();
      }

      public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
      {
         if (typeof(TChannel) == typeof(IOutputChannel))
         {
            return (IChannelFactory<TChannel>)(object)new PgmChannelFactory(this, context);
         }
         else if (typeof(TChannel) == typeof(IOutputSessionChannel))
         {
            if (DataMode == DataMode.Stream)
            {
               throw new InvalidOperationException("Sessionful contracts cannot use Stream mode. Either configure dataMode to Message or change the contract to non sessionful");
            }

            return (IChannelFactory<TChannel>)(object)new PgmSessionChannelFactory(this, context);
         }
         else
         {
            throw new ArgumentException("Unsupported channel type " + typeof(TChannel).Name);
         }
      }

      public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
      {
         if (typeof(TChannel) == typeof(IInputChannel))
         {
            return (IChannelListener<TChannel>)(object)new PgmChannelListener(this, context);
         }
         else if (typeof(TChannel) == typeof(IInputSessionChannel))
         {
            if (DataMode == DataMode.Stream)
            {
               throw new InvalidOperationException("Sessionful contracts cannot use Stream mode. Either configure dataMode to Message or change the contract to non sessionful");
            }

            return (IChannelListener<TChannel>)(object)new PgmSessionChannelListener(this, context);
         }
         else
         {
            throw new ArgumentException("Unsupported channel type " + typeof(TChannel).Name);
         }
      }

      public override bool CanBuildChannelListener<TChannel>(BindingContext context)
      {
         return typeof(TChannel) == typeof(IInputChannel) ||
                typeof(TChannel) == typeof(IInputSessionChannel);
      }

      public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
      {
         return typeof(TChannel) == typeof(IOutputChannel) ||
                typeof(TChannel) == typeof(IOutputSessionChannel);
      }

      public override BindingElement Clone()
      {
         return new PgmTransportBindingElement(this);
      }

      #region IWsdlExportExtension Members

      public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
      {
         
      }

      public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
      {
         Wsdl.ExportEndpoint(exporter, context);
      }

      #endregion

      #region IPolicyExportExtension Members

      public void ExportPolicy(MetadataExporter exporter, PolicyConversionContext context)
      {
         Wsdl.ExportPolicy(exporter, context);
      }

      #endregion
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions