IpcClientChannel.cs 5.6 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. string portName;
  73. string objectUri;
  74. Win32.IpcChannelHelper.Parse (url, out portName, out objectUri);
  75. if (objectUri != null)
  76. url = "unix://" + Path.Combine (Path.GetTempPath (), portName) + "?" + objectUri;
  77. return url;
  78. }
  79. public IMessageSink CreateMessageSink(string url,
  80. object remoteChannelData,
  81. out string objectUri)
  82. {
  83. url = IpcToUnix (url);
  84. IMessageSink sink = ((IChannelSender)_innerChannel).CreateMessageSink (url, remoteChannelData, out objectUri);
  85. if (sink != null)
  86. return new UrlMapperSink (sink);
  87. else
  88. return null;
  89. }
  90. }
  91. //
  92. // Simple message sink that changes ipc URLs to unix URLs.
  93. //
  94. class UrlMapperSink : IMessageSink
  95. {
  96. readonly IMessageSink _sink;
  97. public UrlMapperSink (IMessageSink sink)
  98. {
  99. _sink = sink;
  100. }
  101. public IMessageSink NextSink
  102. {
  103. get { return _sink.NextSink; }
  104. }
  105. void ChangeUri (IMessage msg)
  106. {
  107. string uri = msg.Properties ["__Uri"] as string;
  108. if (uri != null) {
  109. string objectUri;
  110. Win32.IpcChannelHelper.Parse (uri, out objectUri);
  111. if (objectUri != null)
  112. msg.Properties ["__Uri"] = objectUri;
  113. }
  114. }
  115. public IMessage SyncProcessMessage(IMessage msg)
  116. {
  117. ChangeUri (msg);
  118. return _sink.SyncProcessMessage (msg);
  119. }
  120. public IMessageCtrl AsyncProcessMessage(IMessage msg,
  121. IMessageSink replySink)
  122. {
  123. ChangeUri (msg);
  124. return _sink.AsyncProcessMessage (msg, replySink);
  125. }
  126. }
  127. }
  128. #endif