IpcClientChannel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.IpcClientChannel.cs
  3. //
  4. // Author: Robert Jordan ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Runtime.Remoting;
  33. using System.Runtime.Remoting.Channels;
  34. using System.Runtime.Remoting.Messaging;
  35. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  36. {
  37. internal class IpcClientChannel : IChannelSender, IChannel
  38. {
  39. readonly string channelName = IpcChannelHelper.Scheme;
  40. readonly int channelPriority = 1;
  41. readonly IClientChannelSinkProvider clientProvider;
  42. /// <summary>
  43. /// Creates a default client channel.
  44. /// </summary>
  45. public IpcClientChannel()
  46. : this ((IDictionary)null, null)
  47. {
  48. }
  49. /// <summary>
  50. /// Creates a default client channel.
  51. /// </summary>
  52. /// <param name="name">The channel name.</param>
  53. /// <param name="sinkProvider">The provider</param>
  54. public IpcClientChannel(string name, IClientChannelSinkProvider provider)
  55. : this (IpcServerChannel.BuildDefaultProperties (name), provider)
  56. {
  57. }
  58. /// <summary>
  59. /// Creates a client channel.
  60. /// </summary>
  61. /// <param name="properties">The properties.</param>
  62. /// <param name="sinkProvider">The provider</param>
  63. public IpcClientChannel(IDictionary properties, IClientChannelSinkProvider provider)
  64. {
  65. if (properties != null)
  66. {
  67. foreach (DictionaryEntry e in properties)
  68. {
  69. switch ((string)e.Key)
  70. {
  71. case "name":
  72. channelName = (string)e.Value;
  73. break;
  74. case "priority":
  75. channelPriority = Convert.ToInt32(e.Value);
  76. break;
  77. }
  78. }
  79. }
  80. if (provider == null)
  81. {
  82. clientProvider = new BinaryClientFormatterSinkProvider();
  83. clientProvider.Next = new IpcClientChannelSinkProvider();
  84. }
  85. else
  86. {
  87. // add us to the sink chain.
  88. clientProvider = provider;
  89. IClientChannelSinkProvider p;
  90. for (p = clientProvider; p.Next != null; p = p.Next) {}
  91. p.Next = new IpcClientChannelSinkProvider();
  92. }
  93. }
  94. #region IChannelSender Members
  95. public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI)
  96. {
  97. objectURI = null;
  98. string channelUri = null;
  99. if (url != null)
  100. {
  101. channelUri = Parse(url, out objectURI);
  102. }
  103. if (channelUri == null)
  104. {
  105. // get url from the channel data
  106. IChannelDataStore ds = remoteChannelData as IChannelDataStore;
  107. if (ds != null)
  108. {
  109. channelUri = Parse(ds.ChannelUris[0], out objectURI);
  110. if (channelUri != null)
  111. url = ds.ChannelUris[0];
  112. }
  113. }
  114. if (channelUri != null)
  115. {
  116. return (IMessageSink) clientProvider.CreateSink(this, url, remoteChannelData);
  117. }
  118. else
  119. {
  120. return null;
  121. }
  122. }
  123. #endregion
  124. #region IChannel Members
  125. public string ChannelName
  126. {
  127. get
  128. {
  129. return channelName;
  130. }
  131. }
  132. public int ChannelPriority
  133. {
  134. get
  135. {
  136. return channelPriority;
  137. }
  138. }
  139. public string Parse(string url, out string objectURI)
  140. {
  141. return IpcChannelHelper.Parse(url, out objectURI);
  142. }
  143. #endregion
  144. }
  145. internal class IpcClientChannelSinkProvider : IClientChannelSinkProvider
  146. {
  147. #region IClientChannelSinkProvider Members
  148. public IClientChannelSink CreateSink(IChannelSender channel, string url, object remoteChannelData)
  149. {
  150. return new IpcClientChannelSink(url);
  151. }
  152. public IClientChannelSinkProvider Next
  153. {
  154. get
  155. {
  156. return null;
  157. }
  158. set
  159. {
  160. throw new NotSupportedException();
  161. }
  162. }
  163. #endregion
  164. }
  165. internal class IpcClientChannelSink : IClientChannelSink
  166. {
  167. readonly string pipeName;
  168. public IpcClientChannelSink(string url)
  169. {
  170. string unused;
  171. IpcChannelHelper.Parse(url, out pipeName, out unused);
  172. }
  173. #region IClientChannelSink Members
  174. delegate void AsyncResponse(IClientChannelSinkStack sinkStack, IpcTransport transport);
  175. public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg, ITransportHeaders headers, Stream stream)
  176. {
  177. headers[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
  178. // connect
  179. NamedPipeClient client = new NamedPipeClient(pipeName);
  180. NamedPipeSocket socket = client.Connect();
  181. IpcTransport t = new IpcTransport(socket);
  182. t.Write(headers, stream);
  183. // schedule an async call
  184. if (!RemotingServices.IsOneWay(((IMethodCallMessage)msg).MethodBase))
  185. {
  186. new AsyncResponse(AsyncHandler).BeginInvoke(sinkStack, t, null, null);
  187. }
  188. }
  189. void AsyncHandler(IClientChannelSinkStack sinkStack, IpcTransport transport)
  190. {
  191. try
  192. {
  193. // get the response headers and the response stream from the server
  194. ITransportHeaders responseHeaders;
  195. Stream responseStream;
  196. transport.Read(out responseHeaders, out responseStream);
  197. transport.Close();
  198. sinkStack.AsyncProcessResponse(responseHeaders, responseStream);
  199. }
  200. catch (Exception ex)
  201. {
  202. sinkStack.DispatchException(ex);
  203. }
  204. }
  205. public void ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream)
  206. {
  207. responseHeaders = null;
  208. responseStream = null;
  209. requestHeaders[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
  210. // connect
  211. NamedPipeClient client = new NamedPipeClient(pipeName);
  212. NamedPipeSocket socket = client.Connect();
  213. IpcTransport t = new IpcTransport(socket);
  214. t.Write(requestHeaders, requestStream);
  215. t.Read(out responseHeaders, out responseStream);
  216. t.Close();
  217. }
  218. public void AsyncProcessResponse(IClientResponseChannelSinkStack sinkStack, object state, ITransportHeaders headers, Stream stream)
  219. {
  220. throw new NotSupportedException();
  221. }
  222. public Stream GetRequestStream(IMessage msg, ITransportHeaders headers)
  223. {
  224. return null;
  225. }
  226. public IClientChannelSink NextChannelSink
  227. {
  228. get { return null; }
  229. }
  230. #endregion
  231. #region IChannelSinkBase Members
  232. public IDictionary Properties
  233. {
  234. get { return null; }
  235. }
  236. #endregion
  237. }
  238. }
  239. #endif