2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Ipc.Win32 / NamedPipeClient.cs
blob3cb468b36a107281f5b8e1f43c1ea2dd4f78fc9d
1 //
2 // System.Runtime.Remoting.Channels.Ipc.Win32.NamedPipeClient.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.
29 #if NET_2_0
31 using System;
32 using System.Runtime.InteropServices;
34 namespace System.Runtime.Remoting.Channels.Ipc.Win32
36 /// <summary>
37 /// Provides client connections for local Named Pipes.
38 /// </summary>
39 internal class NamedPipeClient
41 readonly string pipeName;
43 public string Name
45 get { return pipeName; }
48 /// <summary>
49 /// Creates a new instance with the local Named Pipe name specified as an unique ID.
50 /// </summary>
51 /// <param name="uid">The Guid.</param>
52 public NamedPipeClient(Guid uid)
53 : this(uid.ToString("N"))
57 /// <summary>
58 /// Creates a new instance for the specified pipe name.
59 /// </summary>
60 /// <param name="pipeName">The pipe name omiting the leading <c>\\.\pipe\</c></param>
61 public NamedPipeClient(string pipeName)
63 this.pipeName = NamedPipeHelper.FormatPipeName(pipeName);
66 /// <summary>
67 /// Connects to a local Named Pipe server.
68 /// </summary>
69 /// <returns>The NamedPipeSocket</returns>
70 public NamedPipeSocket Connect()
72 return Connect(2000);
75 /// <summary>
76 /// Connects to a local Named Pipe server.
77 /// </summary>
78 /// <param name="timeout">Timeout in millisecons to wait for the connection.</param>
79 /// <returns></returns>
80 public NamedPipeSocket Connect(int timeout)
82 while (true)
84 IntPtr hPipe = NamedPipeHelper.CreateFile(
85 pipeName,
86 NamedPipeHelper.GENERIC_READ |
87 NamedPipeHelper.GENERIC_WRITE,
89 IntPtr.Zero,
90 NamedPipeHelper.OPEN_EXISTING,
92 IntPtr.Zero
95 if (hPipe.ToInt32() == NamedPipeHelper.INVALID_HANDLE_VALUE)
97 int lastError = Marshal.GetLastWin32Error();
98 if (lastError != NamedPipeHelper.ERROR_PIPE_BUSY)
99 throw new NamedPipeException(lastError);
101 if (!NamedPipeHelper.WaitNamedPipe(pipeName, timeout))
103 throw new NamedPipeException();
106 else
108 return new NamedPipeSocket(hPipe);
115 #endif