RandomEngy / PipeMethodCalls

Lightweight .NET Standard 2.0 library for method calls over named pipes for IPC. Supports two-way communication with callbacks.
https://www.nuget.org/packages/PipeMethodCalls
MIT License
145 stars 24 forks source link

Support for connect-timeout in the PipeClient #19

Closed BhaaLseN closed 2 years ago

BhaaLseN commented 2 years ago

I'm currently looking at .NET 6 compatible alternatives to WCF with Named Pipes for local IPC, and this one seems like a good fit (that doesn't require a lot of changes, other than writing a quick serializer based on the NetDataContractSerializer). However, one of the things I do a lot is simply try and connect to the server - and do nothing if there's no server. In WCF, i simply use the open timeout and wait for it to fail, but PipeClient has no such overload.

NamedPipeClientStream does have ConnectAsync overloads that accept a timeout value (in milliseconds), but they're not exposed in PipeClient.

I can work around it like this:

var client = new PipeClient<IServerContract>(new NetJsonPipeSerializer(), ServerPipeName);
var cts = new CancellationTokenSource(millisecondsDelay: 200);
try
{
    await client.ConnectAsync(cts.Token);
    // do things when the connection succeeds.
}
catch (OperationCanceledException)
{
    // no server (or too slow to respond), keep going without a server.
}

...but it would certainly feel nicer if I could just explicitly call client.ConnectAsync(timeout: 200) there (without abusing the CancellationToken for it). Won't really help me get rid of the try/catch (since I'm assuming you'd simply pass on the TimeoutException from the pipe), but would free up the CancellationToken for other things.

RandomEngy commented 2 years ago

Good suggestion. I added these overloads in 3.2.0, which I just released.