IpcClientChannel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Unix.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.Messaging;
  34. using Win32 = System.Runtime.Remoting.Channels.Ipc.Win32;
  35. namespace System.Runtime.Remoting.Channels.Ipc.Unix
  36. {
  37. internal class IpcClientChannel : IChannelSender, IChannel
  38. {
  39. object _innerChannel;
  40. public IpcClientChannel ()
  41. {
  42. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadClientChannel ());
  43. }
  44. public IpcClientChannel (IDictionary properties,
  45. IClientChannelSinkProvider sinkProvider)
  46. {
  47. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadClientChannel (), new object [] {properties, sinkProvider});
  48. }
  49. public IpcClientChannel (string name,
  50. IClientChannelSinkProvider sinkProvider)
  51. {
  52. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadClientChannel (), new object [] {name, sinkProvider});
  53. }
  54. public string ChannelName
  55. {
  56. get { return ((IChannel)_innerChannel).ChannelName; }
  57. }
  58. public int ChannelPriority
  59. {
  60. get { return ((IChannel)_innerChannel).ChannelPriority; }
  61. }
  62. public string Parse (string url, out string objectUri)
  63. {
  64. return Win32.IpcChannelHelper.Parse (url, out objectUri);
  65. }
  66. //
  67. // Converts an ipc URL to a unix URL.
  68. // Returns the URL unchanged if it was not ipc.
  69. //
  70. internal static string IpcToUnix (string url)
  71. {
  72. if (url == null)
  73. return null;
  74. string portName;
  75. string objectUri;
  76. Win32.IpcChannelHelper.Parse (url, out portName, out objectUri);
  77. if (objectUri != null)
  78. url = "unix://" + Path.Combine (Path.GetTempPath (), portName) + "?" + objectUri;
  79. return url;
  80. }
  81. public IMessageSink CreateMessageSink(string url,
  82. object remoteChannelData,
  83. out string objectUri)
  84. {
  85. url = IpcToUnix (url);
  86. IMessageSink sink = ((IChannelSender)_innerChannel).CreateMessageSink (url, remoteChannelData, out objectUri);
  87. if (sink != null)
  88. return new UrlMapperSink (sink);
  89. else
  90. return null;
  91. }
  92. }
  93. //
  94. // Simple message sink that changes ipc URLs to unix URLs.
  95. //
  96. sealed class UrlMapperSink : IMessageSink
  97. {
  98. readonly IMessageSink _sink;
  99. public UrlMapperSink (IMessageSink sink)
  100. {
  101. _sink = sink;
  102. }
  103. public IMessageSink NextSink
  104. {
  105. get { return _sink.NextSink; }
  106. }
  107. static void ChangeUri (IMessage msg)
  108. {
  109. string uri = msg.Properties ["__Uri"] as string;
  110. if (uri != null) {
  111. msg.Properties ["__Uri"] = IpcClientChannel.IpcToUnix (uri);
  112. }
  113. }
  114. public IMessage SyncProcessMessage(IMessage msg)
  115. {
  116. ChangeUri (msg);
  117. return _sink.SyncProcessMessage (msg);
  118. }
  119. public IMessageCtrl AsyncProcessMessage(IMessage msg,
  120. IMessageSink replySink)
  121. {
  122. ChangeUri (msg);
  123. return _sink.AsyncProcessMessage (msg, replySink);
  124. }
  125. }
  126. }
  127. #endif