2 // NamedPipeChannelListener.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.
30 using System.Collections.Generic;
32 using System.IO.Pipes;
34 using System.Net.Sockets;
35 using System.ServiceModel.Description;
37 using System.Threading;
40 namespace System.ServiceModel.Channels
42 internal class NamedPipeChannelListener<TChannel> : InternalChannelListenerBase<TChannel>
43 where TChannel : class, IChannel
45 NamedPipeTransportBindingElement source;
46 MessageEncoder encoder = null;
47 XmlDictionaryReaderQuotas quotas = null;
48 BindingContext context;
50 public NamedPipeChannelListener (NamedPipeTransportBindingElement source, BindingContext context)
53 foreach (BindingElement be in context.RemainingBindingElements) {
54 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
56 encoder = CreateEncoder<TChannel> (mbe);
57 quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (context);
63 encoder = new BinaryMessageEncoder ();
66 NamedPipeServerStream active_server;
67 AutoResetEvent server_release_handle = new AutoResetEvent (false);
69 protected override TChannel OnAcceptChannel (TimeSpan timeout)
71 Console.WriteLine ("NamedPipeChannelListener.OnAcceptChannel");
73 DateTime start = DateTime.Now;
74 if (active_server != null) {
76 server_release_handle.WaitOne (timeout);
77 } catch (TimeoutException) {
81 Console.WriteLine ("NamedPipeChannelListener.OnAcceptChannel.2");
83 var server = new NamedPipeServerStream (Uri.LocalPath.Substring (1).Replace ('/', '\\'), PipeDirection.InOut);
84 active_server = server;
86 Console.WriteLine ("NamedPipeChannelListener.OnAcceptChannel.3");
88 server.WaitForConnection ();
90 IAsyncResult r = a.BeginInvoke (null, null);
92 r.AsyncWaitHandle.WaitOne (timeout);
93 } catch (TimeoutException) {
98 // FIXME: support IDuplexSessionChannel
100 if (typeof (TChannel) == typeof (IDuplexSessionChannel))
101 throw new NotImplementedException ();
102 else if (typeof (TChannel) == typeof (IReplyChannel))
103 ch = (TChannel) (object) new NamedPipeReplyChannel (this, encoder, server);
105 throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));
107 ((CommunicationObject) (object) ch).Closed += delegate {
108 active_server = null;
109 server_release_handle.Set ();
115 protected override bool OnWaitForChannel (TimeSpan timeout)
117 throw new NotImplementedException ();
120 // CommunicationObject
122 protected override void OnAbort ()
126 protected override void OnClose (TimeSpan timeout)
130 protected override void OnOpen (TimeSpan timeout)
133 throw new NotSupportedException ("Only local namde pipes are supported in this binding");