IpcChannelHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.IpcChannelHelper.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.IO;
  31. using System.Runtime.Remoting;
  32. using System.Runtime.Remoting.Channels;
  33. using System.Runtime.Remoting.Messaging;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  36. {
  37. /// <summary>
  38. /// Provides helper services to the IpcChannel implementation.
  39. /// </summary>
  40. internal sealed class IpcChannelHelper
  41. {
  42. public const string Scheme = "ipc";
  43. public const string SchemeStart = "ipc://";
  44. IpcChannelHelper()
  45. {
  46. }
  47. static readonly char[] InvalidPipeNameChars =
  48. new char[] {'\\', '/'};
  49. /// <summary>
  50. /// Validates a pipe name.
  51. /// </summary>
  52. /// <param name="pipeName">The pipe name.</param>
  53. /// <returns></returns>
  54. public static bool IsValidPipeName(string pipeName)
  55. {
  56. if (pipeName == null || pipeName.Trim() == "")
  57. return false;
  58. if (pipeName.IndexOfAny(Path.InvalidPathChars) >= 0)
  59. return false;
  60. if (pipeName.IndexOfAny(InvalidPipeNameChars) >= 0)
  61. return false;
  62. return true;
  63. }
  64. /// <summary>
  65. /// Parses an url against IpcChannel's rules.
  66. /// </summary>
  67. /// <param name="url">The url.</param>
  68. /// <param name="pipeName">The pipe name.</param>
  69. /// <param name="objectUri">The uri of the object.</param>
  70. /// <returns>All but the object uri.</returns>
  71. public static string Parse(string url, out string pipeName, out string objectUri)
  72. {
  73. if (url.StartsWith(SchemeStart))
  74. {
  75. int i = url.IndexOf('/', SchemeStart.Length);
  76. if (i >= 0)
  77. {
  78. pipeName = url.Substring(SchemeStart.Length, i - SchemeStart.Length);
  79. objectUri = url.Substring(i);
  80. return SchemeStart + pipeName;
  81. }
  82. else
  83. {
  84. pipeName = url.Substring(SchemeStart.Length);
  85. objectUri = null;
  86. return SchemeStart + pipeName;
  87. }
  88. }
  89. pipeName = null;
  90. objectUri = null;
  91. return null;
  92. }
  93. /// <summary>
  94. /// Parses an url against IpcChannel's rules.
  95. /// </summary>
  96. /// <param name="url">The url.</param>
  97. /// <param name="objectUri">The uri of the object.</param>
  98. /// <returns>All but the object uri.</returns>
  99. public static string Parse(string url, out string objectUri)
  100. {
  101. string pipeName;
  102. return Parse(url, out pipeName, out objectUri);
  103. }
  104. /// <summary>
  105. /// Copies a stream.
  106. /// </summary>
  107. /// <param name="from"></param>
  108. /// <param name="to"></param>
  109. public static void Copy(Stream from, Stream to)
  110. {
  111. // TODO: find out the optimal chunk size.
  112. const int size = 1024 * 1024;
  113. byte[] buffer = new byte[size];
  114. int count;
  115. while ((count = from.Read(buffer, 0, size)) > 0)
  116. {
  117. to.Write(buffer, 0, count);
  118. }
  119. }
  120. }
  121. }
  122. #endif