2 // DuplexClientRuntimeChannel.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Reflection;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Dispatcher;
33 using System.ServiceModel.Security;
34 using System.Threading;
37 namespace System.ServiceModel
39 internal class DuplexClientRuntimeChannel
40 : ClientRuntimeChannel, IDuplexContextChannel
42 public DuplexClientRuntimeChannel (ServiceEndpoint endpoint,
43 ChannelFactory factory, EndpointAddress remoteAddress, Uri via)
44 : base (endpoint, factory, remoteAddress, via)
46 var cd = ContractDescription.GetContract (endpoint.Contract.CallbackContractType);
47 var se = new ServiceEndpoint (cd, factory.Endpoint.Binding, remoteAddress);
48 var ed = new EndpointDispatcher (remoteAddress, cd.Name, cd.Namespace);
49 ed.InitializeServiceEndpoint (true, null, se);
50 Runtime.CallbackDispatchRuntime = ed.DispatchRuntime;
53 public bool AutomaticInputSessionShutdown {
54 get { throw new NotImplementedException (); }
55 set { throw new NotImplementedException (); }
58 InstanceContext callback_instance;
60 public InstanceContext CallbackInstance {
61 get { return callback_instance; }
63 callback_instance = value;
64 Runtime.CallbackDispatchRuntime.InstanceContextProvider = new CallbackInstanceContextProvider (callback_instance);
68 Action<TimeSpan> session_shutdown_delegate;
70 public void CloseOutputSession (TimeSpan timeout)
72 throw new NotImplementedException ();
75 public IAsyncResult BeginCloseOutputSession (TimeSpan timeout, AsyncCallback callback, object state)
77 if (session_shutdown_delegate == null)
78 session_shutdown_delegate = new Action<TimeSpan> (CloseOutputSession);
79 return session_shutdown_delegate.BeginInvoke (timeout, callback, state);
82 public void EndCloseOutputSession (IAsyncResult result)
84 session_shutdown_delegate.EndInvoke (result);
87 // listener loop manager
91 TimeSpan receive_timeout;
92 bool receive_synchronously = true; // FIXME
94 IAsyncResult loop_result;
95 AutoResetEvent loop_handle = new AutoResetEvent (false);
96 AutoResetEvent finish_handle = new AutoResetEvent (false);
98 protected override void OnOpen (TimeSpan timeout)
101 base.OnOpen (timeout);
102 receive_timeout = TimeSpan.FromSeconds (10);
105 protected override void OnOpened ()
108 loop_result = new Action<IInputChannel> (ProcessRequestOrInput).BeginInvoke (DuplexChannel, null, null);
111 protected override void OnClose (TimeSpan timeout)
113 DateTime start = DateTime.Now;
114 base.OnClose (timeout);
116 if (!loop_handle.WaitOne (timeout - (DateTime.Now - start)))
117 throw new TimeoutException ();
118 if (!finish_handle.WaitOne (timeout - (DateTime.Now - start)))
119 throw new TimeoutException ();
122 void ProcessRequestOrInput (IInputChannel input)
128 if (receive_synchronously) {
130 if (input.TryReceive (receive_timeout, out msg))
131 ProcessInput (input, msg);
133 input.BeginTryReceive (receive_timeout, TryReceiveDone, input);
134 loop_handle.WaitOne (receive_timeout);
139 void TryReceiveDone (IAsyncResult result)
143 var input = (IInputChannel) result.AsyncState;
144 if (input.EndTryReceive (result, out msg)) {
146 ProcessInput (input, msg);
148 } catch (Exception ex) {
149 // FIXME: rather log it
150 Console.WriteLine ("Error at duplex client receiver side");
151 Console.WriteLine (ex);
156 void ProcessInput (IInputChannel input, Message message)
159 if (!MessageMatchesEndpointDispatcher (message, Runtime.CallbackDispatchRuntime.EndpointDispatcher))
160 throw new EndpointNotFoundException (String.Format ("The request message has the target '{0}' with action '{1}' which is not reachable in this service contract", message.Headers.To, message.Headers.Action));
161 new InputOrReplyRequestProcessor (Runtime.CallbackDispatchRuntime, input).ProcessInput (message);
162 } catch (Exception ex) {
164 Console.WriteLine (ex);
166 // unless it is closed by session/call manager, move it back to the loop to receive the next message.
167 if (loop && input.State != CommunicationState.Closed)
168 ProcessRequestOrInput (input);
172 bool MessageMatchesEndpointDispatcher (Message req, EndpointDispatcher endpoint)
174 // FIXME: no need to filter address? It'd be mostly anonymous URI though.
176 return endpoint.ContractFilter.Match (req);