IpcServerChannel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.IpcServerChannel.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. using System.Threading;
  36. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  37. {
  38. internal class IpcServerChannel : IChannelReceiver, IChannel
  39. {
  40. readonly string portName;
  41. readonly string channelName = IpcChannelHelper.Scheme;
  42. readonly int channelPriority = 1;
  43. readonly IServerChannelSinkProvider serverProvider;
  44. readonly IpcServerChannelSink sink;
  45. readonly ChannelDataStore dataStore;
  46. Thread worker;
  47. /// <summary>
  48. /// Builds the default channel properties
  49. /// </summary>
  50. /// <param name="portName">The pipe name.</param>
  51. /// <returns></returns>
  52. internal static IDictionary BuildDefaultProperties(string portName)
  53. {
  54. Hashtable h = new Hashtable();
  55. h.Add("portName", portName);
  56. return h;
  57. }
  58. /// <summary>
  59. /// Builds the default channel properties
  60. /// </summary>
  61. /// <param name="portName">The pipe name.</param>
  62. /// <returns></returns>
  63. internal static IDictionary BuildDefaultProperties(string name, string portName)
  64. {
  65. Hashtable h = new Hashtable();
  66. h.Add("name", name);
  67. h.Add("portName", portName);
  68. return h;
  69. }
  70. /// <summary>
  71. /// Creates a server channel
  72. /// </summary>
  73. /// <param name="portName">The port name.</param>
  74. public IpcServerChannel(string portName)
  75. : this(BuildDefaultProperties(portName), null)
  76. {
  77. }
  78. /// <summary>
  79. /// Creates a server channel
  80. /// </summary>
  81. /// <param name="mame">The channel name.</param>
  82. /// <param name="portName">The port name.</param>
  83. public IpcServerChannel(string name, string portName)
  84. : this(BuildDefaultProperties(name, portName), null)
  85. {
  86. }
  87. /// <summary>
  88. /// Creates a server channel
  89. /// </summary>
  90. /// <param name="mame">The channel name.</param>
  91. /// <param name="portName">The port name.</param>
  92. /// <param name="provider">The sink provider.</param>
  93. public IpcServerChannel(string name, string portName,
  94. IServerChannelSinkProvider provider)
  95. : this(BuildDefaultProperties(name, portName), provider)
  96. {
  97. }
  98. /// <summary>
  99. /// Creates a server channel.
  100. /// </summary>
  101. /// <param name="properties">The channel properties.</param>
  102. /// <param name="provider">The sink provider.</param>
  103. public IpcServerChannel(IDictionary properties, IServerChannelSinkProvider provider)
  104. {
  105. bool impersonate = false;
  106. if (properties != null)
  107. {
  108. foreach (DictionaryEntry e in properties)
  109. {
  110. switch ((string)e.Key)
  111. {
  112. case "name":
  113. channelName = (string)e.Value;
  114. break;
  115. case "priority":
  116. channelPriority = Convert.ToInt32(e.Value);
  117. break;
  118. case "portName":
  119. portName = (string)e.Value;
  120. if (!IpcChannelHelper.IsValidPipeName(portName))
  121. throw new ArgumentException("Invalid pipe name.", "portName");
  122. break;
  123. case "impersonate":
  124. impersonate = Boolean.Parse((string)e.Value);
  125. break;
  126. }
  127. }
  128. }
  129. if (portName == null)
  130. {
  131. portName = Guid.NewGuid().ToString("N");
  132. }
  133. serverProvider = provider;
  134. if (serverProvider == null)
  135. {
  136. serverProvider = new BinaryServerFormatterSinkProvider();
  137. }
  138. dataStore = new ChannelDataStore(
  139. new string[] {IpcChannelHelper.SchemeStart + portName}
  140. );
  141. PopulateChannelData(dataStore, serverProvider);
  142. sink = new IpcServerChannelSink(
  143. ChannelServices.CreateServerChannelSinkChain(serverProvider, this),
  144. portName,
  145. impersonate
  146. );
  147. StartListening(null);
  148. }
  149. void PopulateChannelData( ChannelDataStore channelData,
  150. IServerChannelSinkProvider provider)
  151. {
  152. while (provider != null)
  153. {
  154. provider.GetChannelData(channelData);
  155. provider = provider.Next;
  156. }
  157. }
  158. #region IChannelReceiver Members
  159. public void StartListening(object data)
  160. {
  161. if (worker == null)
  162. {
  163. worker = new Thread(new ThreadStart(sink.Listen));
  164. worker.IsBackground = true;
  165. worker.Start();
  166. }
  167. }
  168. public object ChannelData
  169. {
  170. get
  171. {
  172. return dataStore;
  173. }
  174. }
  175. public void StopListening(object data)
  176. {
  177. if (worker != null)
  178. {
  179. worker.Abort();
  180. worker = null;
  181. }
  182. }
  183. public string[] GetUrlsForUri(string objectURI)
  184. {
  185. if (!objectURI.StartsWith("/")) objectURI = "/" + objectURI;
  186. string[] urls = new string[1];
  187. urls[0] = IpcChannelHelper.SchemeStart + portName + objectURI;
  188. return urls;
  189. }
  190. #endregion
  191. #region IChannel Members
  192. public string ChannelName
  193. {
  194. get
  195. {
  196. return channelName;
  197. }
  198. }
  199. public int ChannelPriority
  200. {
  201. get
  202. {
  203. return channelPriority;
  204. }
  205. }
  206. public string Parse(string url, out string objectURI)
  207. {
  208. return IpcChannelHelper.Parse(url, out objectURI);
  209. }
  210. #endregion
  211. }
  212. internal class IpcServerChannelSink : IServerChannelSink
  213. {
  214. IServerChannelSink nextSink;
  215. string portName;
  216. bool impersonate;
  217. public IpcServerChannelSink(IServerChannelSink nextSink, string portName, bool impersonate)
  218. {
  219. this.nextSink = nextSink;
  220. this.portName = portName;
  221. this.impersonate = impersonate;
  222. }
  223. #region IServerChannelSink Members
  224. public Stream GetResponseStream(IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers)
  225. {
  226. return null;
  227. }
  228. public ServerProcessing ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
  229. {
  230. throw new NotSupportedException();
  231. }
  232. public void AsyncProcessResponse(IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers, Stream stream)
  233. {
  234. }
  235. public IServerChannelSink NextChannelSink
  236. {
  237. get
  238. {
  239. return nextSink;
  240. }
  241. }
  242. #endregion
  243. #region IChannelSinkBase Members
  244. public IDictionary Properties
  245. {
  246. get
  247. {
  248. return null;
  249. }
  250. }
  251. /// <summary>
  252. /// Listens for incoming requests.
  253. /// </summary>
  254. internal void Listen()
  255. {
  256. while (true)
  257. {
  258. try
  259. {
  260. NamedPipeListener listener = new NamedPipeListener(portName);
  261. NamedPipeSocket socket = listener.Accept();
  262. ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessClient), socket);
  263. }
  264. catch (NamedPipeException)
  265. {
  266. }
  267. }
  268. }
  269. void ProcessClient(object state)
  270. {
  271. try
  272. {
  273. NamedPipeSocket socket = (NamedPipeSocket) state;
  274. ITransportHeaders requestHeaders;
  275. Stream requestStream;
  276. IpcTransport t = new IpcTransport(socket);
  277. t.Read(out requestHeaders, out requestStream);
  278. // parse the RequestUri
  279. string objectUri;
  280. string uri = (string) requestHeaders[CommonTransportKeys.RequestUri];
  281. IpcChannelHelper.Parse(uri, out objectUri);
  282. if (objectUri == null) objectUri = uri;
  283. requestHeaders[CommonTransportKeys.RequestUri] = objectUri;
  284. ServerChannelSinkStack stack = new ServerChannelSinkStack();
  285. stack.Push(this, null);
  286. IMessage responseMsg;
  287. ITransportHeaders responseHeaders;
  288. Stream responseStream;
  289. requestHeaders["__CustomErrorsEnabled"] = false;
  290. if (impersonate)
  291. {
  292. // TODO: Impersonate might throw exceptions. What to do with them?
  293. socket.Impersonate();
  294. }
  295. ServerProcessing op = nextSink.ProcessMessage(
  296. stack,
  297. null,
  298. requestHeaders,
  299. requestStream,
  300. out responseMsg,
  301. out responseHeaders,
  302. out responseStream
  303. );
  304. if (impersonate)
  305. {
  306. NamedPipeSocket.RevertToSelf();
  307. }
  308. switch (op)
  309. {
  310. case ServerProcessing.Complete:
  311. stack.Pop(this);
  312. // send the response headers and the response data to the client
  313. t.Write(responseHeaders, responseStream);
  314. break;
  315. case ServerProcessing.Async:
  316. stack.StoreAndDispatch(nextSink, null);
  317. break;
  318. case ServerProcessing.OneWay:
  319. break;
  320. }
  321. }
  322. catch (Exception ex)
  323. {
  324. // Console.WriteLine(ex);
  325. }
  326. }
  327. #endregion
  328. }
  329. }
  330. #endif