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