2010-01-21 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Ipc.Win32 / IpcChannelHelper.cs
1 //
2 // System.Runtime.Remoting.Channels.Ipc.Win32.IpcChannelHelper.cs
3 //
4 // Author: Robert Jordan (robertj@gmx.net)
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
29 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Runtime.Remoting;
34 using System.Runtime.Remoting.Channels;
35 using System.Runtime.Remoting.Messaging;
36 using System.Runtime.Serialization.Formatters.Binary;
37
38 namespace System.Runtime.Remoting.Channels.Ipc.Win32
39 {
40     /// <summary>
41     /// Provides helper services to the IpcChannel implementation.
42     /// </summary>
43     internal sealed class IpcChannelHelper
44     {
45         public const string Scheme = "ipc";
46         public const string SchemeStart = "ipc://";
47
48         IpcChannelHelper()
49         {
50         }
51
52         static readonly char[] InvalidPipeNameChars =
53             new char[] {'\\', '/'};
54
55         /// <summary>
56         /// Validates a pipe name.
57         /// </summary>
58         /// <param name="pipeName">The pipe name.</param>
59         /// <returns></returns>
60         public static bool IsValidPipeName(string pipeName) 
61         {
62             if (pipeName == null || pipeName.Trim() == "")
63                 return false;
64
65             if (pipeName.IndexOfAny(Path.InvalidPathChars) >= 0)
66                 return false;
67
68             if (pipeName.IndexOfAny(InvalidPipeNameChars) >= 0)
69                 return false;
70
71             return true;
72         }
73
74         /// <summary>
75         /// Parses an url against IpcChannel's rules.
76         /// </summary>
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)
82         {
83             if (url.StartsWith(SchemeStart)) 
84             {
85                 int i = url.IndexOf('/', SchemeStart.Length);
86                 if (i >= 0) 
87                 {
88                     pipeName = url.Substring(SchemeStart.Length, i - SchemeStart.Length);
89                     objectUri = url.Substring(i);
90                     return SchemeStart + pipeName;
91                 }
92                 else 
93                 {
94                     pipeName = url.Substring(SchemeStart.Length);
95                     objectUri = null;
96                     return SchemeStart + pipeName;
97                 }
98             }
99
100             pipeName = null;
101             objectUri = null;
102             return null;
103         }
104
105         /// <summary>
106         /// Parses an url against IpcChannel's rules.
107         /// </summary>
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)
112         {
113             string pipeName;
114             return Parse(url, out pipeName, out objectUri);
115         }
116
117         /// <summary>
118         /// Copies a stream.
119         /// </summary>
120         /// <param name="from"></param>
121         /// <param name="to"></param>
122         public static void Copy(Stream from, Stream to) 
123         {
124             // TODO: find out the optimal chunk size.
125             const int size = 1024 * 1024;
126             byte[] buffer = new byte[size];
127
128             int count;
129             while ((count = from.Read(buffer, 0, size)) > 0)
130             {
131                 to.Write(buffer, 0, count);
132             }
133         }
134
135     }
136 }
137
138 #endif