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

https://github.com/iainlane/mono · C# · 113 lines · 56 code · 12 blank · 45 comment · 7 complexity · 5c817f8db6a5517a7a6645c4bd91024d MD5 · raw file

  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.NamedPipeListener.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. #if NET_2_0
  29. using System;
  30. using System.Runtime.InteropServices;
  31. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  32. {
  33. /// <summary>
  34. /// Listens for connections from local local Named Pipe clients.
  35. /// </summary>
  36. internal class NamedPipeListener
  37. {
  38. const uint DefaultBufferSize = 4096;
  39. readonly string pipeName;
  40. public string Name
  41. {
  42. get { return pipeName; }
  43. }
  44. /// <summary>
  45. /// Creates a new listener with the local local Named Pipe name obtained form the specified UID.
  46. /// </summary>
  47. /// <param name="uid">The UID.</param>
  48. public NamedPipeListener(Guid uid)
  49. : this(uid.ToString("N"))
  50. {
  51. }
  52. /// <summary>
  53. /// Creates a new listener with the specified name.
  54. /// </summary>
  55. /// <param name="pipeName">The pipe name omiting the leading <c>\\.\pipe\</c></param>
  56. public NamedPipeListener(string pipeName)
  57. {
  58. this.pipeName = NamedPipeHelper.FormatPipeName(pipeName);
  59. }
  60. /// <summary>
  61. /// Accepts a pending connection request
  62. /// </summary>
  63. /// <remarks>
  64. /// Accept is a blocking method that returns a NamedPipeSocket you can use to send and receive data.
  65. /// </remarks>
  66. /// <returns>The NamedPipeSocket.</returns>
  67. public NamedPipeSocket Accept()
  68. {
  69. IntPtr hPipe = NamedPipeHelper.CreateNamedPipe(
  70. pipeName,
  71. NamedPipeHelper.PIPE_ACCESS_DUPLEX,
  72. NamedPipeHelper.PIPE_TYPE_MESSAGE
  73. | NamedPipeHelper.PIPE_READMODE_MESSAGE
  74. | NamedPipeHelper.PIPE_WAIT,
  75. NamedPipeHelper.PIPE_UNLIMITED_INSTANCES,
  76. DefaultBufferSize,
  77. DefaultBufferSize,
  78. NamedPipeHelper.NMPWAIT_USE_DEFAULT_WAIT,
  79. IntPtr.Zero
  80. );
  81. if (hPipe.ToInt32() == NamedPipeHelper.INVALID_HANDLE_VALUE)
  82. {
  83. throw new NamedPipeException();
  84. }
  85. bool canConnect = NamedPipeHelper.ConnectNamedPipe(hPipe, IntPtr.Zero);
  86. int lastError = Marshal.GetLastWin32Error();
  87. if (!canConnect && lastError == NamedPipeHelper.ERROR_PIPE_CONNECTED)
  88. canConnect = true;
  89. if (canConnect)
  90. {
  91. return new NamedPipeSocket(hPipe);
  92. }
  93. else
  94. {
  95. NamedPipeHelper.CloseHandle(hPipe);
  96. throw new NamedPipeException(lastError);
  97. }
  98. }
  99. }
  100. }
  101. #endif