Merge pull request #3248 from esdrubal/web_request_abort
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / ConnectionOrientedTransportBindingElement.cs
1 //
2 // ConnectionOrientedTransportBindingElement.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Martin Baulig <martin.baulig@xamarin.com>
7 //
8 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
9 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.Net;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.Xml;
36 #if !MOBILE && !XAMMAC_4_5
37 using WS = System.Web.Services.Description;
38 #endif
39
40 namespace System.ServiceModel.Channels
41 {
42         [MonoTODO]
43         public abstract class ConnectionOrientedTransportBindingElement
44                 : TransportBindingElement, IPolicyExportExtension, IWsdlExportExtension
45         {
46                 int connection_buf_size = 0x2000, max_buf_size = 0x10000,
47                         max_pending_conn = 10, max_pending_accepts = 1;
48                 HostNameComparisonMode host_cmp_mode = HostNameComparisonMode.StrongWildcard;
49                 TimeSpan max_output_delay = TimeSpan.FromMilliseconds (200);
50                 TimeSpan ch_init_timeout = TimeSpan.FromSeconds (5);
51                 TransferMode transfer_mode = TransferMode.Buffered;
52
53                 internal ConnectionOrientedTransportBindingElement ()
54                 {
55                 }
56
57                 internal ConnectionOrientedTransportBindingElement (
58                         ConnectionOrientedTransportBindingElement other)
59                         : base (other)
60                 {
61                         connection_buf_size = other.connection_buf_size;
62                         max_buf_size = other.max_buf_size;
63                         max_pending_conn = other.max_pending_conn;
64                         max_pending_accepts = other.max_pending_accepts;
65                         host_cmp_mode = other.host_cmp_mode;
66                         max_output_delay = other.max_output_delay;
67                         transfer_mode = other.transfer_mode;
68                 }
69
70                 public TimeSpan ChannelInitializationTimeout {
71                         get { return ch_init_timeout; }
72                         set { ch_init_timeout = value; }
73                 }
74
75                 public int ConnectionBufferSize {
76                         get { return connection_buf_size; }
77                         set { connection_buf_size = value; }
78                 }
79
80                 public HostNameComparisonMode HostNameComparisonMode {
81                         get { return host_cmp_mode; }
82                         set { host_cmp_mode = value; }
83                 }
84
85                 public int MaxBufferSize {
86                         get { return max_buf_size; }
87                         set { max_buf_size = value; }
88                 }
89
90                 public int MaxPendingConnections {
91                         get { return max_pending_conn; }
92                         set { max_pending_conn = value; }
93                 }
94
95                 public TimeSpan MaxOutputDelay {
96                         get { return max_output_delay; }
97                         set { max_output_delay = value; }
98                 }
99
100                 public int MaxPendingAccepts {
101                         get { return max_pending_accepts; }
102                         set { max_pending_accepts = value; }
103                 }
104
105                 public TransferMode TransferMode {
106                         get { return transfer_mode; }
107                         set { transfer_mode = value; }
108                 }
109                 
110                 public override bool CanBuildChannelFactory<TChannel> (
111                         BindingContext context)
112                 {
113                         switch (TransferMode) {
114                         case TransferMode.Buffered:
115                         case TransferMode.StreamedResponse:
116                                 return typeof (TChannel) == typeof (IDuplexSessionChannel);
117                         case TransferMode.Streamed:
118                         case TransferMode.StreamedRequest:
119                                 return typeof (TChannel) == typeof (IRequestChannel);
120                         }
121                         return false;
122                 }
123
124 #if !MOBILE && !XAMMAC_4_5
125                 public override bool CanBuildChannelListener<TChannel> (
126                         BindingContext context)
127                 {
128                         switch (TransferMode) {
129                         case TransferMode.Buffered:
130                         case TransferMode.StreamedRequest:
131                                 return typeof (TChannel) == typeof (IDuplexSessionChannel);
132                         case TransferMode.Streamed:
133                         case TransferMode.StreamedResponse:
134                                 return typeof (TChannel) == typeof (IReplyChannel);
135                         }
136                         return false;
137                 }
138 #endif
139
140                 public override T GetProperty<T> (BindingContext context)
141                 {
142                         // since this class cannot be derived (internal .ctor
143                         // only), we cannot examine what this should do.
144                         // So, handle all properties in the derived types.
145                         return base.GetProperty<T> (context);
146                 }
147
148 #if !MOBILE && !XAMMAC_4_5
149                 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
150                                                           WsdlContractConversionContext context)
151                 {
152                         ;
153                 }
154                 
155                 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
156                                                           WsdlEndpointConversionContext context)
157                 {
158                         var soap_binding = new WS.Soap12Binding ();
159                         soap_binding.Transport = "http://schemas.microsoft.com/soap/tcp";
160                         soap_binding.Style = WS.SoapBindingStyle.Document;
161                         context.WsdlBinding.Extensions.Add (soap_binding);
162
163                         var address = context.Endpoint.Address;
164                         var uri = address.Uri.AbsoluteUri;
165
166                         var soap_address = new WS.Soap12AddressBinding ();
167                         soap_address.Location = uri;
168                         context.WsdlPort.Extensions.Add (soap_address);
169
170                         var doc = new XmlDocument ();
171                         var endpoint_ref = doc.CreateElement (
172                                 "EndpointReference", AddressingVersion.WSAddressing10.Namespace);
173                         var endpoint_addr = doc.CreateElement (
174                                 "Address", AddressingVersion.WSAddressing10.Namespace);
175                         endpoint_addr.InnerText = uri;
176                         endpoint_ref.AppendChild (endpoint_addr);
177                         context.WsdlPort.Extensions.Add (endpoint_ref);
178                 }
179
180                 void IPolicyExportExtension.ExportPolicy (MetadataExporter exporter, PolicyConversionContext context)
181                 {
182                         if (exporter == null)
183                                 throw new ArgumentNullException ("exporter");
184                         if (context == null)
185                                 throw new ArgumentNullException ("context");
186
187                         PolicyAssertionCollection assertions = context.GetBindingAssertions ();
188                         XmlDocument doc = new XmlDocument ();
189
190                         var messageEncodingElement = ExportAddressingPolicy (context);
191                         if (messageEncodingElement == null)
192                                 assertions.Add (doc.CreateElement (
193                                         "msb", "BinaryEncoding",
194                                         "http://schemas.microsoft.com/ws/06/2004/mspolicy/netbinary1"));
195
196                         if (transfer_mode == TransferMode.Streamed || transfer_mode == TransferMode.StreamedRequest ||
197                                 transfer_mode == TransferMode.StreamedResponse)
198                                 assertions.Add (doc.CreateElement ("msf", "Streamed", "http://schemas.microsoft.com/ws/2006/05/framing/policy"));
199                 }
200 #endif
201         }
202 }