Some 4.0 API tweaks
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / TcpChannelFactory.cs
1 // 
2 // TcpChannelFactory.cs
3 // 
4 // Author: 
5 //     Marcos Cobena (marcoscobena@gmail.com)
6 // 
7 // Copyright 2007 Marcos Cobena (http://www.youcannoteatbits.org/)
8 // 
9
10 using System;
11 using System.Collections.Generic;
12 using System.Net;
13 using System.Net.Security;
14 using System.ServiceModel;
15 using System.ServiceModel.Description;
16 using System.ServiceModel.Security;
17 using System.Text;
18 using System.Xml;
19
20 namespace System.ServiceModel.Channels
21 {
22         internal class TcpChannelInfo
23         {
24                 public TcpChannelInfo (TransportBindingElement element, MessageEncoder encoder, XmlDictionaryReaderQuotas readerQuotas)
25                 {
26                         this.BindingElement = element;
27                         this.MessageEncoder = encoder;
28                         this.ReaderQuotas = readerQuotas ?? new XmlDictionaryReaderQuotas ();
29                 }
30
31                 public TransportBindingElement BindingElement { get; private set; }
32
33                 public MessageEncoder MessageEncoder { get; private set; }
34
35                 public XmlDictionaryReaderQuotas ReaderQuotas { get; private set; }
36         }
37
38         internal class TcpChannelFactory<TChannel> : TransportChannelFactoryBase<TChannel>
39         {
40                 TcpChannelInfo info;
41
42                 public TcpChannelFactory (TcpTransportBindingElement source, BindingContext ctx)
43                         : base (source, ctx)
44                 {
45                         XmlDictionaryReaderQuotas quotas = null;
46                         foreach (BindingElement be in ctx.Binding.Elements) {
47                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
48                                 if (mbe != null) {
49                                         MessageEncoder = CreateEncoder<TChannel> (mbe);
50                                         quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (ctx);
51                                         break;
52                                 }
53                         }
54                         if (MessageEncoder == null)
55                                 MessageEncoder = new BinaryMessageEncoder ();
56                         info = new TcpChannelInfo (source, MessageEncoder, quotas);
57                 }
58
59                 protected override TChannel OnCreateChannel (
60                         EndpointAddress address, Uri via)
61                 {                       
62                         ThrowIfDisposedOrNotOpen ();
63
64                         var targetUri = via ?? address.Uri;
65                         if (info.BindingElement.Scheme != targetUri.Scheme)
66                                 throw new ArgumentException (String.Format ("Argument EndpointAddress has unsupported URI scheme: {0}", targetUri.Scheme));
67
68                         Type t = typeof (TChannel);
69                         
70                         if (t == typeof (IDuplexSessionChannel))
71                                 return (TChannel) (object) new TcpDuplexSessionChannel (this, info, address, targetUri);
72                         
73                         if (t == typeof (IRequestChannel))
74                                 return (TChannel) (object) new TcpRequestChannel (this, info, address, targetUri);
75
76                         throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));
77                 }
78         }
79 }