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.
33 using System.Runtime.Remoting;
34 using System.Runtime.Remoting.Channels;
35 using System.Runtime.Remoting.Messaging;
36 using System.Runtime.Serialization.Formatters.Binary;
38 namespace System.Runtime.Remoting.Channels.Ipc.Win32
41 /// Provides helper services to the IpcChannel implementation.
43 internal sealed class IpcChannelHelper
45 public const string Scheme = "ipc";
46 public const string SchemeStart = "ipc://";
52 static readonly char[] InvalidPipeNameChars =
53 new char[] {'\\', '/'};
56 /// Validates a pipe name.
58 /// <param name="pipeName">The pipe name.</param>
59 /// <returns></returns>
60 public static bool IsValidPipeName(string pipeName)
62 if (pipeName == null || pipeName.Trim() == "")
65 if (pipeName.IndexOfAny(Path.InvalidPathChars) >= 0)
68 if (pipeName.IndexOfAny(InvalidPipeNameChars) >= 0)
75 /// Parses an url against IpcChannel's rules.
77 /// <param name="url">The url.</param>
78 /// <param name="pipeName">The pipe name.</param>
79 /// <param name="objectUri">The uri of the object.</param>
80 /// <returns>All but the object uri.</returns>
81 public static string Parse(string url, out string pipeName, out string objectUri)
83 if (url.StartsWith(SchemeStart))
85 int i = url.IndexOf('/', SchemeStart.Length);
88 pipeName = url.Substring(SchemeStart.Length, i - SchemeStart.Length);
89 objectUri = url.Substring(i);
90 return SchemeStart + pipeName;
94 pipeName = url.Substring(SchemeStart.Length);
96 return SchemeStart + pipeName;
106 /// Parses an url against IpcChannel's rules.
108 /// <param name="url">The url.</param>
109 /// <param name="objectUri">The uri of the object.</param>
110 /// <returns>All but the object uri.</returns>
111 public static string Parse(string url, out string objectUri)
114 return Parse(url, out pipeName, out objectUri);
120 /// <param name="from"></param>
121 /// <param name="to"></param>
122 public static void Copy(Stream from, Stream to)
124 // TODO: find out the optimal chunk size.
125 const int size = 1024 * 1024;
126 byte[] buffer = new byte[size];
129 while ((count = from.Read(buffer, 0, size)) > 0)
131 to.Write(buffer, 0, count);