* support-test-*.cs: Rename from test-*-p2.cs.
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Ipc.Win32 / NamedPipeClient.cs
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.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Runtime.InteropServices;
33
34 namespace System.Runtime.Remoting.Channels.Ipc.Win32
35 {
36     /// <summary>
37     /// Provides client connections for local Named Pipes.
38     /// </summary>
39     internal class NamedPipeClient
40     {
41         readonly string pipeName;
42
43         public string Name 
44         {
45             get { return pipeName; }
46         }
47
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"))
54         {
55         }
56
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)
62         {
63             this.pipeName = NamedPipeHelper.FormatPipeName(pipeName);
64         }
65
66         /// <summary>
67         /// Connects to a local Named Pipe server.
68         /// </summary>
69         /// <returns>The NamedPipeSocket</returns>
70         public NamedPipeSocket Connect() 
71         {
72             return Connect(2000);
73         }
74
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) 
81         {
82             while (true) 
83             {
84                 IntPtr hPipe = NamedPipeHelper.CreateFile( 
85                     pipeName,
86                     NamedPipeHelper.GENERIC_READ |
87                     NamedPipeHelper.GENERIC_WRITE, 
88                     0,
89                     IntPtr.Zero,
90                     NamedPipeHelper.OPEN_EXISTING,
91                     0,
92                     IntPtr.Zero
93                     );
94
95                 if (hPipe.ToInt32() == NamedPipeHelper.INVALID_HANDLE_VALUE) 
96                 {
97                     int lastError = Marshal.GetLastWin32Error();
98                     if (lastError != NamedPipeHelper.ERROR_PIPE_BUSY)
99                         throw new NamedPipeException(lastError);
100
101                     if (!NamedPipeHelper.WaitNamedPipe(pipeName, timeout)) 
102                     {
103                         throw new NamedPipeException();
104                     }
105                 }
106                 else 
107                 {
108                     return new NamedPipeSocket(hPipe);
109                 }
110             }
111         }
112     }
113 }
114
115 #endif