2009-08-20 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / NamedPipeChannelListener.cs
1 //
2 // NamedPipeChannelListener.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.IO.Pipes;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.ServiceModel.Description;
36 using System.Text;
37 using System.Threading;
38 using System.Xml;
39
40 namespace System.ServiceModel.Channels
41 {
42         internal class NamedPipeChannelListener<TChannel> : InternalChannelListenerBase<TChannel> 
43                 where TChannel : class, IChannel
44         {
45                 NamedPipeTransportBindingElement source;
46                 MessageEncoder encoder = null;
47                 XmlDictionaryReaderQuotas quotas = null;
48                 BindingContext context;
49                 
50                 public NamedPipeChannelListener (NamedPipeTransportBindingElement source, BindingContext context)
51                         : base (context)
52                 {
53                         foreach (BindingElement be in context.RemainingBindingElements) {
54                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
55                                 if (mbe != null) {
56                                         encoder = CreateEncoder<TChannel> (mbe);
57                                         quotas = mbe.GetProperty<XmlDictionaryReaderQuotas> (context);
58                                         break;
59                                 }
60                         }
61                         
62                         if (encoder == null)
63                                 encoder = new BinaryMessageEncoder ();
64                 }
65
66                 NamedPipeServerStream active_server;
67                 AutoResetEvent server_release_handle = new AutoResetEvent (false);
68
69                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
70                 {
71 Console.WriteLine ("NamedPipeChannelListener.OnAcceptChannel");
72
73                         DateTime start = DateTime.Now;
74                         if (active_server != null) {
75                                 try {
76                                         server_release_handle.WaitOne (timeout);
77                                 } catch (TimeoutException) {
78                                         return null;
79                                 }
80                         }
81 Console.WriteLine ("NamedPipeChannelListener.OnAcceptChannel.2");
82
83                         var server = new NamedPipeServerStream (Uri.LocalPath.Substring (1).Replace ('/', '\\'), PipeDirection.InOut);
84                         active_server = server;
85
86 Console.WriteLine ("NamedPipeChannelListener.OnAcceptChannel.3");
87                         Action a = delegate {
88                                 server.WaitForConnection ();
89                         };
90                         IAsyncResult r = a.BeginInvoke (null, null);
91                         try {
92                                 r.AsyncWaitHandle.WaitOne (timeout);
93                         } catch (TimeoutException) {
94                                 server.Close ();
95                                 return null;
96                         }
97
98                         // FIXME: support IDuplexSessionChannel
99                         TChannel ch;
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);
104                         else
105                                 throw new InvalidOperationException (String.Format ("Channel type {0} is not supported.", typeof (TChannel).Name));
106
107                         ((CommunicationObject) (object) ch).Closed += delegate {
108                                 active_server = null;
109                                 server_release_handle.Set ();
110                         };
111                         return ch;
112                 }
113
114                 [MonoTODO]
115                 protected override bool OnWaitForChannel (TimeSpan timeout)
116                 {
117                         throw new NotImplementedException ();
118                 }
119                 
120                 // CommunicationObject
121                 
122                 protected override void OnAbort ()
123                 {
124                 }
125
126                 protected override void OnClose (TimeSpan timeout)
127                 {
128                 }
129
130                 protected override void OnOpen (TimeSpan timeout)
131                 {
132                         if (!Uri.IsLoopback)
133                                 throw new NotSupportedException ("Only local namde pipes are supported in this binding");
134                 }
135         }
136 }