2 // System.Runtime.Remoting.Channels.Ipc.Win32.IpcChannelHelper.cs
4 // Author: Robert Jordan (robertj@gmx.net)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Runtime.Remoting;
33 using System.Runtime.Remoting.Channels;
34 using System.Runtime.Remoting.Messaging;
35 using System.Runtime.Serialization.Formatters.Binary;
37 namespace System.Runtime.Remoting.Channels.Ipc.Win32
40 /// Provides helper services to the IpcChannel implementation.
42 internal sealed class IpcChannelHelper
44 public const string Scheme = "ipc";
45 public const string SchemeStart = "ipc://";
51 static readonly char[] InvalidPipeNameChars =
52 new char[] {'\\', '/'};
55 /// Validates a pipe name.
57 /// <param name="pipeName">The pipe name.</param>
58 /// <returns></returns>
59 public static bool IsValidPipeName(string pipeName)
61 if (pipeName == null || pipeName.Trim() == "")
64 if (pipeName.IndexOfAny(Path.InvalidPathChars) >= 0)
67 if (pipeName.IndexOfAny(InvalidPipeNameChars) >= 0)
74 /// Parses an url against IpcChannel's rules.
76 /// <param name="url">The url.</param>
77 /// <param name="pipeName">The pipe name.</param>
78 /// <param name="objectUri">The uri of the object.</param>
79 /// <returns>All but the object uri.</returns>
80 public static string Parse(string url, out string pipeName, out string objectUri)
82 if (url.StartsWith(SchemeStart))
84 int i = url.IndexOf('/', SchemeStart.Length);
87 pipeName = url.Substring(SchemeStart.Length, i - SchemeStart.Length);
88 objectUri = url.Substring(i);
89 return SchemeStart + pipeName;
93 pipeName = url.Substring(SchemeStart.Length);
95 return SchemeStart + pipeName;
105 /// Parses an url against IpcChannel's rules.
107 /// <param name="url">The url.</param>
108 /// <param name="objectUri">The uri of the object.</param>
109 /// <returns>All but the object uri.</returns>
110 public static string Parse(string url, out string objectUri)
113 return Parse(url, out pipeName, out objectUri);
119 /// <param name="input"></param>
120 /// <param name="output"></param>
121 public static void Copy(Stream input, Stream output)
123 MemoryStream ms = input as MemoryStream;
130 // TODO: find out the optimal chunk size.
131 const int size = 1024 * 1024;
132 byte[] buffer = new byte[size];
135 while ((count = input.Read(buffer, 0, size)) > 0)
137 output.Write(buffer, 0, count);