2009-10-06 Atsushi Enomoto <atsushi@ximian.com>
[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> : ChannelFactoryBase<TChannel>
39         {
40                 TcpChannelInfo info;
41
42                 [MonoTODO]
43                 public TcpChannelFactory (TcpTransportBindingElement source, BindingContext ctx)
44                 {
45                         MessageEncoder encoder = null;
46                         XmlDictionaryReaderQuotas quotas = null;
47                         foreach (BindingElement be in ctx.RemainingBindingElements) {
48                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
49                                 if (mbe != null) {
50                                         encoder = CreateEncoder<TChannel> (mbe);
51                                         quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (ctx);
52                                         break;
53                                 }
54                         }
55                         if (encoder == null)
56                                 encoder = new BinaryMessageEncoder ();
57                         info = new TcpChannelInfo (source, encoder, quotas);
58                 }
59
60                 protected override TChannel OnCreateChannel (
61                         EndpointAddress address, Uri via)
62                 {                       
63                         ThrowIfDisposedOrNotOpen ();
64
65                         var targetUri = via ?? address.Uri;
66                         if (info.BindingElement.Scheme != targetUri.Scheme)
67                                 throw new ArgumentException (String.Format ("Argument EndpointAddress has unsupported URI scheme: {0}", targetUri.Scheme));
68
69                         Type t = typeof (TChannel);
70                         
71                         if (t == typeof (IDuplexSessionChannel))
72                                 return (TChannel) (object) new TcpDuplexSessionChannel (this, info, address, targetUri);
73                         
74                         if (t == typeof (IRequestChannel))
75                                 return (TChannel) (object) new TcpRequestChannel (this, info, address, targetUri);
76
77                         throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));
78                 }
79
80                 [MonoTODO]
81                 protected override void OnOpen (TimeSpan timeout)
82                 {
83                 }
84         }
85 }