IpcServerChannel.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Unix.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 Win32 = System.Runtime.Remoting.Channels.Ipc.Win32;
  34. namespace System.Runtime.Remoting.Channels.Ipc.Unix
  35. {
  36. internal class IpcServerChannel : IChannelReceiver, IChannel
  37. {
  38. object _innerChannel;
  39. string _portName;
  40. string _path;
  41. internal static string BuildPathFromPortName (string portName)
  42. {
  43. if (!Win32.IpcChannelHelper.IsValidPipeName (portName))
  44. throw new RemotingException ("Invalid IPC port name");
  45. return Path.Combine (Path.GetTempPath (), portName);
  46. }
  47. internal static IDictionary MapProperties (IDictionary props)
  48. {
  49. if (props == null) return null;
  50. Hashtable h = new Hashtable ();
  51. foreach (DictionaryEntry e in props) {
  52. switch (e.Key as string) {
  53. case "portName":
  54. h ["path"] = BuildPathFromPortName ((string)e.Value);
  55. break;
  56. default:
  57. h [e.Key] = e.Value;
  58. break;
  59. }
  60. }
  61. return h;
  62. }
  63. public IpcServerChannel (string portName)
  64. {
  65. _portName = portName;
  66. _path = portName = BuildPathFromPortName (portName);
  67. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadServerChannel (), new object [] {portName});
  68. }
  69. public IpcServerChannel (string name, string portName,
  70. IServerChannelSinkProvider serverSinkProvider)
  71. {
  72. _portName = portName;
  73. _path = portName = BuildPathFromPortName (portName);
  74. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadServerChannel (), new object [] {name, portName, serverSinkProvider});
  75. }
  76. public IpcServerChannel (string name, string portName)
  77. {
  78. _portName = portName = BuildPathFromPortName (portName);
  79. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadServerChannel (), new object [] {name, portName});
  80. }
  81. public IpcServerChannel (IDictionary properties,
  82. IServerChannelSinkProvider serverSinkProvider)
  83. {
  84. properties = MapProperties (properties);
  85. if (properties != null) {
  86. _portName = properties ["portName"] as string;
  87. _path = properties ["path"] as string;
  88. }
  89. _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadServerChannel (), new object [] {properties, serverSinkProvider});
  90. }
  91. public string ChannelName
  92. {
  93. get { return ((IChannel)_innerChannel).ChannelName; }
  94. }
  95. public int ChannelPriority
  96. {
  97. get { return ((IChannel)_innerChannel).ChannelPriority; }
  98. }
  99. public string Parse (string url, out string objectUri)
  100. {
  101. return Win32.IpcChannelHelper.Parse (url, out objectUri);
  102. }
  103. public object ChannelData
  104. {
  105. get { return ((IChannelReceiver)_innerChannel).ChannelData; }
  106. }
  107. public string[] GetUrlsForUri(string objectUri)
  108. {
  109. string[] res = ((IChannelReceiver)_innerChannel).GetUrlsForUri (objectUri);
  110. if (res != null) {
  111. string[] urls = new string [res.Length + 1];
  112. for (int i = 0; i < res.Length; i++)
  113. urls [i] = res [i];
  114. if (!objectUri.StartsWith ("/"))
  115. objectUri = "/" + objectUri;
  116. urls [res.Length] = "ipc://" + _portName + objectUri;
  117. return urls;
  118. }
  119. return res;
  120. }
  121. public void StartListening (object data)
  122. {
  123. ((IChannelReceiver)_innerChannel).StartListening (data);
  124. }
  125. public void StopListening(object data)
  126. {
  127. ((IChannelReceiver)_innerChannel).StopListening (data);
  128. }
  129. }
  130. }
  131. #endif