2009-05-21 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
19 namespace System.ServiceModel.Channels
20 {
21         internal class TcpChannelInfo
22         {
23                 public TcpChannelInfo (TcpTransportBindingElement element, MessageEncoder encoder)
24                 {
25                         this.element = element;
26                         this.encoder = encoder;
27                 }
28
29                 TcpTransportBindingElement element;
30                 MessageEncoder encoder;
31
32                 public TcpTransportBindingElement BindingElement {
33                         get { return element; }
34                 }
35
36                 public MessageEncoder MessageEncoder {
37                         get { return encoder; }
38                 }
39         }
40
41         internal class TcpChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
42         {
43                 TcpChannelInfo info;
44
45                 [MonoTODO]
46                 public TcpChannelFactory (TcpTransportBindingElement source, BindingContext ctx)
47                 {
48                         MessageEncoder encoder = null;
49                         foreach (BindingElement be in ctx.RemainingBindingElements) {
50                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
51                                 if (mbe != null) {
52                                         encoder = mbe.CreateMessageEncoderFactory ().Encoder;
53                                         break;
54                                 }
55                         }
56                         if (encoder == null)
57                                 encoder = new BinaryMessageEncoder ();
58                         info = new TcpChannelInfo (source, encoder);
59                 }
60
61                 [MonoTODO]
62                 protected override TChannel OnCreateChannel (
63                         EndpointAddress address, Uri via)
64                 {                       
65                         ThrowIfDisposedOrNotOpen ();
66
67                         if (info.BindingElement.Scheme != address.Uri.Scheme)
68                                 throw new ArgumentException (String.Format ("Argument EndpointAddress has unsupported URI scheme: {0}", address.Uri.Scheme));
69
70                         Type t = typeof (TChannel);
71                         
72                         if (t == typeof (IDuplexSessionChannel))
73                                 return (TChannel) (object) new TcpDuplexSessionChannel (this, info, address, via);
74                         
75                         throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));
76                 }
77
78                 [MonoTODO]
79                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
80                         AsyncCallback callback, object state)
81                 {
82                         throw new NotImplementedException ();
83                 }
84
85                 [MonoTODO]
86                 protected override void OnEndOpen (IAsyncResult result)
87                 {
88                         throw new NotImplementedException ();
89                 }
90
91                 [MonoTODO]
92                 protected override void OnOpen (TimeSpan timeout)
93                 {
94                 }
95         }
96 }