Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 7cbc755

Browse files
author
Ian Hays
committed
Merge pull request #2875 from ianhays/pipes_serverinstances
Removed the Unix restriction on MaxNumberOfServerInstances
2 parents 69b9b44 + 7d1906c commit 7cbc755

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Unix.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private void Create(string pipeName, PipeDirection direction, int maxNumberOfSer
3131
Debug.Assert(direction >= PipeDirection.In && direction <= PipeDirection.InOut, "invalid pipe direction");
3232
Debug.Assert(inBufferSize >= 0, "inBufferSize is negative");
3333
Debug.Assert(outBufferSize >= 0, "outBufferSize is negative");
34-
Debug.Assert((maxNumberOfServerInstances >= 1 && maxNumberOfServerInstances <= 254) || (maxNumberOfServerInstances == MaxAllowedServerInstances), "maxNumberOfServerInstances is invalid");
34+
Debug.Assert((maxNumberOfServerInstances >= 1) || (maxNumberOfServerInstances == MaxAllowedServerInstances), "maxNumberOfServerInstances is invalid");
3535
Debug.Assert(transmissionMode >= PipeTransmissionMode.Byte && transmissionMode <= PipeTransmissionMode.Message, "transmissionMode is out of range");
3636

3737
if (transmissionMode == PipeTransmissionMode.Message)
@@ -132,6 +132,16 @@ public String GetImpersonationUserName()
132132
throw new PlatformNotSupportedException();
133133
}
134134

135+
private void ValidateMaxNumberOfServerInstances(int maxNumberOfServerInstances)
136+
{
137+
// Since Unix has no notion of Max allowed Server Instances per named pipe, we don't enforce an
138+
// upper bound on maxNumberOfServerInstances.
139+
if ((maxNumberOfServerInstances < 1) && (maxNumberOfServerInstances != MaxAllowedServerInstances))
140+
{
141+
throw new ArgumentOutOfRangeException("maxNumberOfServerInstances", SR.ArgumentOutOfRange_MaxNumServerInstances);
142+
}
143+
}
144+
135145
// -----------------------------
136146
// ---- PAL layer ends here ----
137147
// -----------------------------

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,15 @@ private void CheckConnectOperationsServerWithHandle()
298298
}
299299
CheckConnectOperationsServer();
300300
}
301+
302+
private void ValidateMaxNumberOfServerInstances(int maxNumberOfServerInstances)
303+
{
304+
// win32 allows fixed values of 1-254 or 255 to mean max allowed by system. We expose 255 as -1 (unlimited)
305+
// through the MaxAllowedServerInstances constant. This is consistent e.g. with -1 as infinite timeout, etc
306+
if ((maxNumberOfServerInstances < 1 || maxNumberOfServerInstances > 254) && (maxNumberOfServerInstances != MaxAllowedServerInstances))
307+
{
308+
throw new ArgumentOutOfRangeException("maxNumberOfServerInstances", SR.ArgumentOutOfRange_MaxNumServerInstances);
309+
}
310+
}
301311
}
302312
}

src/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public NamedPipeServerStream(String pipeName, PipeDirection direction, int maxNu
6161
/// Win32 note: this gets OR'd into dwOpenMode to CreateNamedPipe
6262
/// </param>
6363
/// <param name="maxNumberOfServerInstances">Maximum number of server instances. Specify a fixed value between
64-
/// 1 and 254, or use NamedPipeServerStream.MaxAllowedServerInstances to use the maximum amount allowed by
65-
/// system resources.</param>
64+
/// 1 and 254 (Windows)/greater than 1 (Unix), or use NamedPipeServerStream.MaxAllowedServerInstances to use the
65+
/// maximum amount allowed by system resources.</param>
6666
/// <param name="transmissionMode">Byte mode or message mode.
6767
/// Win32 note: this gets used for dwPipeMode. CreateNamedPipe allows you to specify PIPE_TYPE_BYTE/MESSAGE
6868
/// and PIPE_READMODE_BYTE/MESSAGE independently, but this sets type and readmode to match.
@@ -102,12 +102,7 @@ private NamedPipeServerStream(String pipeName, PipeDirection direction, int maxN
102102
{
103103
throw new ArgumentOutOfRangeException("inBufferSize", SR.ArgumentOutOfRange_NeedNonNegNum);
104104
}
105-
// win32 allows fixed values of 1-254 or 255 to mean max allowed by system. We expose 255 as -1 (unlimited)
106-
// through the MaxAllowedServerInstances constant. This is consistent e.g. with -1 as infinite timeout, etc
107-
if ((maxNumberOfServerInstances < 1 || maxNumberOfServerInstances > 254) && (maxNumberOfServerInstances != MaxAllowedServerInstances))
108-
{
109-
throw new ArgumentOutOfRangeException("maxNumberOfServerInstances", SR.ArgumentOutOfRange_MaxNumServerInstances);
110-
}
105+
ValidateMaxNumberOfServerInstances(maxNumberOfServerInstances);
111106
// inheritability will always be None since this private constructor is only called from other constructors from which
112107
// inheritability is always set to None. Desktop has a public constructor to allow setting it to something else, but Core
113108
// doesnt.

src/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CreateServer.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,42 @@ public static void InvalidPipeDirection_Throws_ArgumentOutOfRangeException()
8989
Assert.Throws<ArgumentOutOfRangeException>("direction", () => new NamedPipeServerStream("tempx", (PipeDirection)123, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0));
9090
}
9191

92+
[Theory]
93+
[InlineData(0)]
94+
[InlineData(-2)]
95+
public static void InvalidServerInstances_Throws_ArgumentOutOfRangeException(int numberOfServerInstances)
96+
{
97+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", PipeDirection.In, numberOfServerInstances));
98+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", PipeDirection.In, numberOfServerInstances, PipeTransmissionMode.Byte));
99+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", PipeDirection.In, numberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.None));
100+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", PipeDirection.In, numberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0));
101+
}
102+
92103
[Theory]
93104
[InlineData(PipeDirection.In)]
94105
[InlineData(PipeDirection.InOut)]
95106
[InlineData(PipeDirection.Out)]
96-
public static void InvalidServerInstances_Throws_ArgumentOutOfRangeException(PipeDirection direction)
107+
[PlatformSpecific(PlatformID.AnyUnix)]
108+
public static void Unix_ServerInstancesOver254_Allowed(PipeDirection direction)
109+
{
110+
// MaxNumberOfServerInstances has functionality on Unix and as such is not upper bound.
111+
new NamedPipeServerStream(GetUniquePipeName(), direction, 255).Dispose();
112+
new NamedPipeServerStream(GetUniquePipeName(), direction, 255, PipeTransmissionMode.Byte).Dispose();
113+
new NamedPipeServerStream(GetUniquePipeName(), direction, 255, PipeTransmissionMode.Byte, PipeOptions.None).Dispose();
114+
new NamedPipeServerStream(GetUniquePipeName(), direction, 255, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0).Dispose();
115+
}
116+
117+
[Theory]
118+
[InlineData(PipeDirection.In)]
119+
[InlineData(PipeDirection.InOut)]
120+
[InlineData(PipeDirection.Out)]
121+
[PlatformSpecific(PlatformID.Windows)]
122+
public static void Windows_ServerInstancesOver254_Throws_ArgumentOutOfRangeException(PipeDirection direction)
97123
{
98-
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 0));
99-
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 0, PipeTransmissionMode.Byte));
100-
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 0, PipeTransmissionMode.Byte, PipeOptions.None));
101-
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 0, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0));
124+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 255));
125+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 255, PipeTransmissionMode.Byte));
126+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 255, PipeTransmissionMode.Byte, PipeOptions.None));
127+
Assert.Throws<ArgumentOutOfRangeException>("maxNumberOfServerInstances", () => new NamedPipeServerStream("temp3", direction, 255, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0));
102128
}
103129

104130
[Theory]

0 commit comments

Comments
 (0)