Skip to content

Commit

Permalink
Try to prevent insane connection bruteforcing on Windows side while s…
Browse files Browse the repository at this point in the history
…erver hasn't started.
  • Loading branch information
darvell committed Nov 24, 2014
1 parent a47086f commit f7267a4
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions NamedPipeWrapper/NamedPipeClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using NamedPipeWrapper.IO;
Expand Down Expand Up @@ -141,6 +143,8 @@ public void WaitForDisconnection(TimeSpan timeout)

#region Private methods



private void ListenSync()
{
// Get the name of the data pipe that should be used from now on by this NamedPipeClient
Expand Down Expand Up @@ -209,10 +213,23 @@ static class PipeClientFactory
return new PipeStreamWrapper<TRead, TWrite>(CreateAndConnectPipe(pipeName));
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool WaitNamedPipe(string name, int timeout);

public static NamedPipeClientStream CreateAndConnectPipe(string pipeName)
{
var pipe = CreatePipe(pipeName);
// Connect() will attempt to connect once every ms by default regardless of existence.
// Let's not make the entire thing melt and ensure we wait on pipe existence.
string fullPipePath = Path.GetFullPath(String.Format(@"\\.\pipe\{0}", pipeName));
while (!File.Exists(fullPipePath))
{
Thread.Sleep(250);
}
WaitNamedPipe(fullPipePath, 0xFFFFFFF);
pipe.Connect();


return pipe;
}

Expand Down

0 comments on commit f7267a4

Please sign in to comment.