spkl.CLI.IPC
1.2.0
See the version list below for details.
dotnet add package spkl.CLI.IPC --version 1.2.0
NuGet\Install-Package spkl.CLI.IPC -Version 1.2.0
<PackageReference Include="spkl.CLI.IPC" Version="1.2.0" />
paket add spkl.CLI.IPC --version 1.2.0
#r "nuget: spkl.CLI.IPC, 1.2.0"
// Install spkl.CLI.IPC as a Cake Addin #addin nuget:?package=spkl.CLI.IPC&version=1.2.0 // Install spkl.CLI.IPC as a Cake Tool #tool nuget:?package=spkl.CLI.IPC&version=1.2.0
CLI.IPC
A .NET library that helps you implement a client/server architecture for command line interface applications. Using spkl.CLI.IPC, you can let your CLI application delegate its workload to a separate server process.
How does it work? When a user starts your client application, it sends its working directory, command line arguments and process ID to the server application. While processing the request, the server application can send console outputs (standard and error) back to the client. At the end, the server sends an exit code and closes the connection.
spkl.CLI.IPC was designed for clients and servers that run on the same machine, but because it is based on sockets, network communication between different machines can also be used.
Example Usage
Server
Start a server by calling Host.Start
and supplying the following information:
- How will the connection be established (
ITransport
)? Any streaming socket is supported; support for TCP loopback and Unix Domain Sockets* is built-in. - What will happen when a client connects (
IClientConnectionHandler
)?
* Unix Domain Sockets are not supported for .NET Standard 2.0 or before Windows 10.
In this example, lets implement a fictional database dump command:
static void Main(string[] args)
{
Host host = Host.Start(new UdsTransport("path/to/socket_file"), new MyClientConnectionHandler());
// [ wait until it is time to shut down the server ]
host.Shutdown();
host.WaitUntilAllClientsDisconnected();
}
private class MyClientConnectionHandler : IClientConnectionHandler
{
public void HandleCall(ClientConnection connection)
{
if (connection.Properties.Arguments is [_, "dump-db", string fileName])
{
connection.Out.WriteLine("Dumping database to file...");
Db.DumpTo(Path.Combine(connection.Properties.CurrentDirectory, fileName));
connection.Exit(0);
}
connection.Error.WriteLine("Unknown command.");
connection.Exit(1);
}
// [...]
}
Client
Implementing a client can be as easy as this one-liner:
Client.Attach(
new UdsTransport("path/to/socket_file"),
new DefaultHostConnectionHandler());
Just like for the server, you need to specify connection and behavior information (ITransport
, IHostConnectionHandler2
).
For most scenarios, the built-in DefaultHostConnectionHandler
can be used. It writes the received outputs to the console and exits the application with the received exit code when the server closes the connection.
Other Features
SingletonApplication: Starting a Server On Demand
A client application obviously can't connect to a server when the server is not running. If the server should be started on demand, i.e. when a client wants to connect, the SingletonApplication
class can help.
With SingletonApplication
, you can ensure that only one instance of the server is started, even when multiple clients ask for it simultaneously.
Usage as a client application: Call RequestInstance
to ensure that an application instance is running before connecting to it.
IStartupBehavior startupBehavior = [...];
new SingletonApplication(startupBehavior).RequestInstance();
Client.Attach([...]);
Usage as a server application: Call ReportInstanceRunning
when ready for incoming connections. Call ShutdownInstance
before no longer accepting connections.
IStartupBehavior startupBehavior = [...];
SingletonApplication singletonApplication = new(startupBehavior);
Host host = Host.Start([...]);
singletonApplication.ReportInstanceRunning();
// [...]
singletonApplication.ShutdownInstance();
host.Shutdown();
Using the IStartupBehavior
interface, you can customize the following aspects:
- How a server instance is started. Typically, by starting a new process.
- Which time period is used for polling whether a server is starting or running.
- After what timeout the
RequestInstance
orReportInstanceRunning
methods will fail. - Which file path is used to determine server state (because
SingletonApplication
uses file-based locking). This can be used to provide a server process per machine, per user, or arbitrarily.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- The client connection is now closed automatically if IClientConnectionHandler.HandleCall did not close it.
- New method: Host.WaitUntilUnusedFor returns after a specified idle time of the host instance.