Skip to content

Commit

Permalink
Merge pull request #93 from davidrpalmer/master
Browse files Browse the repository at this point in the history
Introduce NamedPipeServerStreamFactory
  • Loading branch information
tylerje authored Dec 1, 2024
2 parents 98cf9e3 + 40b5749 commit 7d3c406
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/ServiceWire/NamedPipes/DefaultNamedPipeServerStreamFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.IO.Pipes;

namespace ServiceWire.NamedPipes
{
public class DefaultNamedPipeServerStreamFactory : INamedPipeServerStreamFactory
{
public NamedPipeServerStream Create(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize)
{
return new NamedPipeServerStream(pipeName, direction, maxNumberOfServerInstances, transmissionMode, options, inBufferSize, outBufferSize);
}
}
}
9 changes: 9 additions & 0 deletions src/ServiceWire/NamedPipes/INamedPipeServerStreamFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.IO.Pipes;

namespace ServiceWire.NamedPipes
{
public interface INamedPipeServerStreamFactory
{
NamedPipeServerStream Create(string pipeName, PipeDirection direction, int maxNumberOfServerInstances, PipeTransmissionMode transmissionMode, PipeOptions options, int inBufferSize, int outBufferSize);
}
}
5 changes: 3 additions & 2 deletions src/ServiceWire/NamedPipes/NpHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public bool UseThreadPool
/// <param name="stats"></param>
/// <param name="serializer">Inject your own serializer for complex objects and avoid using the Newtonsoft JSON DefaultSerializer.</param>
/// <param name="compressor">Inject your own compressor and avoid using the standard GZIP DefaultCompressor.</param>
public NpHost(string pipeName, ILog log = null, IStats stats = null, ISerializer serializer = null, ICompressor compressor = null)
/// <param name="streamFactory">Inject your own factory for creating the named pipe server. Can be used with NamedPipeServerStreamAcl to set the ACL on Windows platforms.</param>
public NpHost(string pipeName, ILog log = null, IStats stats = null, ISerializer serializer = null, ICompressor compressor = null, INamedPipeServerStreamFactory streamFactory = null)
: base(serializer, compressor)
{
base.Log = log;
base.Stats = stats;
_pipeName = pipeName;
_listener = new NpListener(_pipeName, log: base.Log, stats: base.Stats);
_listener = new NpListener(_pipeName, log: base.Log, stats: base.Stats, streamFactory: streamFactory);
_listener.RequestReieved += ClientConnectionMade;
}

Expand Down
8 changes: 4 additions & 4 deletions src/ServiceWire/NamedPipes/NpListener.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO.Pipes;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -14,17 +12,19 @@ public class NpListener
private int _maxConnections = 254;
private ILog _log = new NullLogger();
private IStats _stats = new NullStats();
private INamedPipeServerStreamFactory _streamFactory;

public string PipeName { get; set; }
public event EventHandler<PipeClientConnectionEventArgs> RequestReieved;

public NpListener(string pipeName, int maxConnections = 254, ILog log = null, IStats stats = null)
public NpListener(string pipeName, int maxConnections = 254, ILog log = null, IStats stats = null, INamedPipeServerStreamFactory streamFactory = null)
{
_log = log ?? _log;
_stats = stats ?? _stats;
if (maxConnections > 254) maxConnections = 254;
_maxConnections = maxConnections;
this.PipeName = pipeName;
_streamFactory = streamFactory ?? new DefaultNamedPipeServerStreamFactory();
}

public void Start()
Expand Down Expand Up @@ -98,7 +98,7 @@ public void ProcessNextClient()
{
try
{
var pipeStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, _maxConnections, PipeTransmissionMode.Byte, PipeOptions.None, 512, 512);
var pipeStream = _streamFactory.Create(PipeName, PipeDirection.InOut, _maxConnections, PipeTransmissionMode.Byte, PipeOptions.None, 512, 512);
pipeStream.WaitForConnection();
//Task.Factory.StartNew(() => ProcessClientThread(pipeStream), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => ProcessClientThread(pipeStream));
Expand Down

0 comments on commit 7d3c406

Please sign in to comment.