/mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Ipc.Win32/NamedPipeClient.cs

https://github.com/QuickJack/mono · C# · 113 lines · 55 code · 11 blank · 47 comment · 7 complexity · 1475b6bf4c6ce91418d498e94ce3282d MD5 · raw file

  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. using System;
  29. using System.Runtime.InteropServices;
  30. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  31. {
  32. /// <summary>
  33. /// Provides client connections for local Named Pipes.
  34. /// </summary>
  35. internal class NamedPipeClient
  36. {
  37. readonly string pipeName;
  38. public string Name
  39. {
  40. get { return pipeName; }
  41. }
  42. /// <summary>
  43. /// Creates a new instance with the local Named Pipe name specified as an unique ID.
  44. /// </summary>
  45. /// <param name="uid">The Guid.</param>
  46. public NamedPipeClient(Guid uid)
  47. : this(uid.ToString("N"))
  48. {
  49. }
  50. /// <summary>
  51. /// Creates a new instance for the specified pipe name.
  52. /// </summary>
  53. /// <param name="pipeName">The pipe name omiting the leading <c>\\.\pipe\</c></param>
  54. public NamedPipeClient(string pipeName)
  55. {
  56. this.pipeName = NamedPipeHelper.FormatPipeName(pipeName);
  57. }
  58. /// <summary>
  59. /// Connects to a local Named Pipe server.
  60. /// </summary>
  61. /// <returns>The NamedPipeSocket</returns>
  62. public NamedPipeSocket Connect()
  63. {
  64. return Connect(2000);
  65. }
  66. /// <summary>
  67. /// Connects to a local Named Pipe server.
  68. /// </summary>
  69. /// <param name="timeout">Timeout in millisecons to wait for the connection.</param>
  70. /// <returns></returns>
  71. public NamedPipeSocket Connect(int timeout)
  72. {
  73. while (true)
  74. {
  75. IntPtr hPipe = NamedPipeHelper.CreateFile(
  76. pipeName,
  77. NamedPipeHelper.GENERIC_READ |
  78. NamedPipeHelper.GENERIC_WRITE,
  79. 0,
  80. IntPtr.Zero,
  81. NamedPipeHelper.OPEN_EXISTING,
  82. 0,
  83. IntPtr.Zero
  84. );
  85. if (hPipe.ToInt32() == NamedPipeHelper.INVALID_HANDLE_VALUE)
  86. {
  87. int lastError = Marshal.GetLastWin32Error();
  88. if (lastError != NamedPipeHelper.ERROR_PIPE_BUSY)
  89. throw new NamedPipeException(lastError);
  90. if (!NamedPipeHelper.WaitNamedPipe(pipeName, timeout))
  91. {
  92. throw new NamedPipeException();
  93. }
  94. }
  95. else
  96. {
  97. return new NamedPipeSocket(hPipe);
  98. }
  99. }
  100. }
  101. }
  102. }