IpcNetMq 26.10.10.10
dotnet add package IpcNetMq --version 26.10.10.10
NuGet\Install-Package IpcNetMq -Version 26.10.10.10
<PackageReference Include="IpcNetMq" Version="26.10.10.10" />
<PackageVersion Include="IpcNetMq" Version="26.10.10.10" />
<PackageReference Include="IpcNetMq" />
paket add IpcNetMq --version 26.10.10.10
#r "nuget: IpcNetMq, 26.10.10.10"
#:package IpcNetMq@26.10.10.10
#addin nuget:?package=IpcNetMq&version=26.10.10.10
#tool nuget:?package=IpcNetMq&version=26.10.10.10
IpcNetMq
<p align="center"> <img src="Resources/IpcNetMq.png" alt="IpcNetMq logo" width="256"> </p>
IpcNetMq is a small request/reply interprocess communication library for .NET, built on NetMQ. It provides a structured JSON packet protocol, synchronous and asynchronous .NET client APIs, blocking and non-blocking .NET server modes, and a compatible Python server example using pyzmq.
The library is intended for applications that need a clean process boundary between .NET and another process or language—for example, scientific simulations, neural-network inference, hardware interfaces, and independently restartable services—without embedding Python or another runtime inside the .NET process.
Key Features
- NetMQ/ZeroMQ
REQ/REPtransport over configurable endpoints such astcp://127.0.0.1:5555. - Targets .NET Framework 4.8, .NET 8, and .NET 10.
- One-frame UTF-8 JSON wire format based on
IpcPacket. - Automatic request/reply sequence numbering and validation.
- Synchronous and asynchronous .NET client calls.
- Dedicated client I/O thread so a NetMQ socket remains owned by one thread.
- Automatic client reconnection after send or receive failures.
- Blocking server loop for console and service applications.
- Non-blocking polling server API for game loops and simulation update loops.
- Canonical name/value payload helpers with tolerant JSON deserialization.
- C# client, C# server, and interoperable Python server examples.
- Out-of-process isolation: independent runtimes, dependencies, memory, failure handling, and restart behavior.
Requirements
.NET
- .NET 10 SDK for the supplied C# examples.
- .NET 8 and .NET 10 SDKs/runtimes for the multi-target test project.
- Visual Studio 2026 or the
dotnetcommand-line tools. - NetMQ 4.0.4.3.
- System.Text.Json 10.0.11.
The library itself targets:
net48
net8.0
net10.0
Python example
- Python 3.11 or later recommended.
- pyzmq.
Install the Python dependency with:
py -3.11 -m pip install pyzmq
Quick Start
1. Build the solution
dotnet build IpcNetMq.sln -c Release
2. Start the C# server
dotnet run --project .\Examples\TestServerCSharp\TestServerIpc.csproj -- tcp://127.0.0.1:5555
3. Start the C# client in another terminal
dotnet run --project .\Examples\TestClientCSharp\TestClientIpc.csproj -- tcp://127.0.0.1:5555
The client repeatedly sends an IpcPacket, waits for the matching reply, validates the sequence number, and periodically reports average round-trip time.
Basic Client Usage
Create one IpcClientNetMq for each logical client connection. The current client API queues calls onto a dedicated I/O thread; do not call OpenConnection() when using CallIpcMethod() or CallIpcMethodAsync() because the dispatcher opens and reconnects the socket as needed.
using IpcNetMq;
using IpcNetMq.IpcNetMqHelpers;
const string address = "tcp://127.0.0.1:5555";
using var client = new IpcClientNetMq("SimulationClient", address);
var request = new IpcPacket
{
Action = "do_get1",
ContextString = JsonHelpers.BuildNameValuePairs(
("SimTime", "12.5")),
RequestString = JsonHelpers.BuildNameValuePairs(
("exprOne", "10"),
("exprTwo", "23.4")),
ReplyString = JsonHelpers.BuildNameValuePairs(
("stateOne", ""),
("stateTwo", ""))
};
IpcPacket reply = await client.CallIpcMethodAsync(
request,
sendTimeout: TimeSpan.FromSeconds(2),
receiveTimeout: TimeSpan.FromSeconds(5));
Console.WriteLine($"{reply.Action}: {reply.ReplyString}");
The client assigns SequenceNumber. Application code should not attempt to assign or modify it.
For callers that cannot use async, the equivalent blocking call is:
IpcPacket reply = client.CallIpcMethod(
request,
sendTimeout: TimeSpan.FromSeconds(2),
receiveTimeout: TimeSpan.FromSeconds(5));
Basic Server Usage
Blocking server loop
The blocking server loop is appropriate for a console program, background service, or dedicated server thread:
using IpcNetMq;
const string address = "tcp://127.0.0.1:5555";
using var server = new IpcServerNetMq("SimulationServer", address);
server.RunIpcServerLoop(HandleAction);
static IpcPacket HandleAction(IpcPacket request)
{
return request.Action switch
{
"Ping" => new IpcPacket
{
Action = "SUCCESS",
Status = "Success",
ReplyString = "Pong"
},
_ => new IpcPacket
{
Action = "FAIL",
Status = $"Unknown action: {request.Action}"
}
};
}
RunIpcServerLoop() receives requests and invokes the handler serially. The server assigns the reply sequence number and copies the request's ClientId into the reply.
Non-blocking polling server
Polling mode is intended for hosts that own their main loop, such as Stride, another game engine, or a discrete-event simulation:
private readonly IpcServerNetMq server =
new("SimulationServer", "tcp://127.0.0.1:5555");
public void Start()
{
server.EnsurePollingReady();
}
public void Update()
{
if (!server.TryGetRequest(out IpcPacket request))
return;
IpcPacket reply = HandleAction(request);
server.SendReply(reply);
}
TryGetRequest() never blocks. After it returns true, SendReply() must be called exactly once before the server can receive another request. This is required by the ZeroMQ REP socket state machine.
For a frame-based host that wants to limit IPC work per update, poll up to a configured maximum:
for (int i = 0; i < maxRequestsPerFrame; i++)
{
if (!server.TryGetRequest(out IpcPacket request))
break;
server.SendReply(HandleAction(request));
}
With a REP socket, there can only be one received-but-not-yet-replied request at a time, so each successful poll must still be paired immediately with its reply.
Architecture
flowchart LR
A["Application code"] --> B["IpcClientNetMq queue"]
B --> C["Dedicated NetMQ I/O thread"]
C --> D["REQ/REP JSON frame"]
D --> E["C# or Python server"]
Client
IpcClientNetMq owns a NetMQ RequestSocket and a bounded work queue. Calls to CallIpcMethodAsync() enqueue work; a single background I/O thread performs the following operations:
- Create or reconnect the socket.
- Assign the next odd request sequence number.
- Serialize the request to JSON.
- Send one ZeroMQ frame.
- Receive one reply frame.
- Deserialize the reply.
- Verify that its sequence number is the request number plus one.
- Complete the caller's task.
The single I/O thread is intentional. NetMQ sockets are thread-affine and must not be used concurrently from arbitrary application threads.
The work queue is currently bounded to 1,024 calls. REQ/REP permits only one request in flight on a socket, so queued requests are processed serially.
Server
IpcServerNetMq derives from IpcServerBaseNetMq, which owns the NetMQ ResponseSocket, endpoint binding, shutdown, retry, and disposal behavior.
The concrete server exposes two hosting models:
RunIpcServerLoop(...): blocking, serial request processing.EnsurePollingReady()+TryGetRequest(...)+SendReply(...): non-blocking integration into an externally controlled loop.
Do not mix blocking-loop mode and polling mode on the same server instance.
Wire protocol
Each transaction uses one UTF-8 JSON frame in each direction. No manual byte count, delimiter, or length prefix is added; ZeroMQ preserves frame boundaries.
The outer object is IpcPacket:
| JSON property | C# property | Purpose |
|---|---|---|
version |
Version |
Packet schema version; currently V260107. |
client_id |
ClientId |
Logical client identifier. |
sequence_number |
SequenceNumber |
Odd request number or following even reply number. |
world_time |
WorldTime |
UTC packet construction time. |
action |
Action |
Operation or method name. |
status |
Status |
Result status or diagnostic text. |
options_string |
OptionsString |
JSON text for IPC options or transaction metadata. |
context_string |
ContextString |
JSON text for persistent or call-specific context. |
request_string |
RequestString |
JSON text containing request arguments. |
reply_string |
ReplyString |
JSON text containing the reply or a reply template. |
The four *_string fields are strings in the outer JSON packet. When they contain structured data, that inner data is serialized as JSON text and is therefore escaped once by the outer packet serializer.
Example outer packet:
{
"version": "V260107",
"client_id": "SimulationClient",
"sequence_number": 1,
"world_time": "2026-08-19T18:00:00.0000000Z",
"action": "do_get1",
"status": "",
"options_string": "",
"context_string": "{\"SimTime\":\"12.5\"}",
"request_string": "{\"exprOne\":\"10\"}",
"reply_string": "{\"stateOne\":\"\"}"
}
Sequence-number contract
Sequence numbers are transport metadata owned by IpcNetMq:
| Message | Sequence |
|---|---|
| First request | 1 |
| First reply | 2 |
| Second request | 3 |
| Second reply | 4 |
- The .NET client assigns odd request numbers.
- The server assigns
request.SequenceNumber + 1to the reply. - The client rejects a reply whose sequence number does not match the expected even number.
- Application handlers should construct reply content but should not set
SequenceNumber.
The Python server follows the same contract explicitly.
Name/value payloads
The canonical inner payload is a JSON array of lowercase name/value objects:
[
{ "name": "exprOne", "value": "10" },
{ "name": "exprTwo", "value": "23.4" }
]
Create and read these payloads with:
var values = new List<NameValuePair>
{
new("exprOne", "10"),
new("exprTwo", "23.4")
};
string json = NameValuePairJson.SerializeList(values);
List<NameValuePair> decoded =
NameValuePairJson.DeserializeListTolerant<NameValuePair>(json);
The tolerant deserializer accepts an array, one object, empty input, or legacy double-encoded JSON. New code should emit the canonical single-encoded array form.
JsonHelpers.BuildNameValuePairs(...) remains available for the legacy dictionary representation used by the current examples.
Python Server Example
The Python example demonstrates that the protocol is language-neutral. A .NET RequestSocket can communicate directly with a Python pyzmq REP socket because both use the same ZeroMQ framing and JSON schema.
The example is located in:
Examples/ServerPython/
├── IpcPacket.py
├── ServerZeroMQ.py
└── UserActions.py
Run the Python server
From the repository root:
cd .\Examples\ServerPython
py -3.11 -m pip install pyzmq
py -3.11 .\ServerZeroMQ.py tcp://127.0.0.1:5555
If no endpoint is supplied, the script prompts for one and defaults to tcp://127.0.0.1:5555.
Run the C# client against Python
In a second terminal, from the repository root:
dotnet run --project .\Examples\TestClientCSharp\TestClientIpc.csproj -- tcp://127.0.0.1:5555
The client sends do_get1 requests. ServerZeroMQ.py deserializes the packet, dispatches the action to UserActions.py, increments the sequence number for the reply, serializes the result, and returns one UTF-8 JSON frame.
Add a Python action
Define a handler in UserActions.py:
def handle_ping(in_packet):
out_packet = in_packet.clone()
out_packet.sequence_number = in_packet.sequence_number + 1
out_packet.action = "SUCCESS"
out_packet.status = "Success"
out_packet.reply_string = "Pong"
return out_packet
Then register it in ServerZeroMQ.py:
action_handlers = {
"Ping": handle_ping,
"do_get1": handle_do_get1,
"do_get2": do_get2,
}
Every Python REP request must receive exactly one reply, including invalid actions and error cases. Otherwise the socket cannot receive the next request.
Failure and Recovery Behavior
Client behavior
- Default send timeout: 2 seconds.
- Default receive timeout: 5 seconds. Timeouts must be finite and non-negative. Positive waits are rounded up to whole milliseconds to avoid NetMQ truncating a sub-millisecond wait into a non-expiring path.
- Failed exchanges close the socket; the next call opens a fresh socket. Requests are never automatically replayed.
- A request that was sent but did not receive a valid reply causes the REQ socket to be recreated before subsequent work.
- A sequence mismatch completes the call with an exception.
- Cancellation completes the task as canceled and interrupts socket waits in slices of at most 25 ms. The worker abandons the incomplete exchange before processing another call.
- Calls are queued and processed serially. The bounded queue rejects excess work immediately rather than blocking the caller.
- Disposal stops the worker, closes its socket on that worker, and completes active/queued calls with
ObjectDisposedException. Further calls are rejected.
Timeouts may occur normally while debugging a server. The caller should catch TimeoutException, apply an appropriate backoff, and retry at the application level when the operation is safe to repeat.
Server behavior
- Requests are processed serially.
- The loop uses a dedicated socket-owning thread and closes/rebuilds its socket after transport failures. Handler exceptions or null replies produce correlated error packets.
- Socket-open failures use a capped linear retry delay of up to five seconds.
- The loop exits after ten consecutive failed attempts to open the socket.
- Polling mode is non-blocking but requires exactly one reply for every request received.
- Malformed and unsupported multipart messages receive an error reply so the REP socket remains usable. Polling replies echo both the request sequence plus one and its client ID.
- Background-loop cancellation interrupts idle receive polling within 25 ms. Disposal requests loop shutdown and waits for the owner thread to close its socket. A running user handler must return before shutdown can finish.
- Instance disposal does not invoke
NetMQConfig.Cleanup(). Only the application should call global cleanup, after all its NetMQ sockets have stopped. - Polling operations and disposal must use the same thread. Polling and loop modes cannot be mixed.
Delivery semantics
IpcNetMq provides request/reply correlation; it does not provide durable messaging or exactly-once execution.
If a server completes an operation but its reply is lost, the client can observe only a timeout. Retrying a state-changing action may execute it again. Such operations should carry an application-level operation ID or implement idempotency when duplicate execution would be unsafe.
Benchmarks
An observed localhost interoperability test using the supplied .NET client and Python pyzmq server measured approximately:
| Path | Transport | Payload | Average round trip |
|---|---|---|---|
| C# client → Python server → C# client | Loopback TCP, NetMQ/pyzmq, JSON | Small IpcPacket request/reply |
210 μs |
That latency corresponds to a theoretical maximum of approximately 4,760 sequential round trips per second before application work (and yes, it is 210 microseconds, not milliseconds). It is not a measured sustained-throughput result, and this is on the same machine (IP loopback).
The figure is an informal development measurement, not a controlled cross-platform benchmark. Results depend on processor, operating system, power state, Python and .NET versions, payload size, logging, debugger attachment, endpoint type, and server handler work.
For simulation and neural-network calls that take milliseconds or longer, the measured IPC cost is generally small relative to the invoked work. The process boundary also provides benefits that in-process Python integration does not: runtime isolation, independent dependency environments, failure containment, restartability, easier diagnostics, and the option to move the service to another machine later.
When publishing benchmark results, record at least:
- CPU and operating system.
- .NET and Python versions.
- NetMQ and pyzmq versions.
- Release versus Debug build.
- Transport endpoint.
- Payload size.
- Warm-up count and measured iteration count.
- Median, mean, p95, p99, minimum, and maximum latency.
- Whether logging and a debugger were enabled.
Directory Layout
IpcNetMq/
├── IpcNetMq.csproj Main multi-target library project
├── IpcNetMq.sln Visual Studio solution
├── IpcPacket.cs Wire packet and sequence metadata
├── IpcClientNetMq.cs REQ client, queue, I/O thread, reconnect logic
├── IpcServerNetMq.cs REP server: blocking and polling APIs
├── IIpcClient.cs Legacy/general client interface
├── IIpcServer.cs Legacy/general server interface
├── IpcNetMqHelpers/
│ ├── IpcServerBase.cs Socket binding, retry loop, disposal
│ ├── JsonHelpers.cs Packet and payload serialization
│ ├── NameValuePair.cs Canonical name/value DTO and JSON options
│ ├── NameValuePairSerialization.cs
│ │ Tolerant name/value serialization
│ ├── CommunicationHelpers.cs Endpoint hashing helper
│ ├── Extensions.cs Conversion and string helpers
│ └── Logger.cs Example asynchronous file logger
├── Examples/
│ ├── TestClientCSharp/ .NET 10 client and timing example
│ ├── TestServerCSharp/ .NET 10 blocking/polling server example
│ └── ServerPython/ Python/pyzmq interoperable server
├── IpcNetMq.Tests/ .NET 8/10 xUnit serialization and transport tests
├── Documentation/ Additional project documentation
├── Resources/ Package icon assets
└── LICENSE.txt Repository license text
IpcServerNetMqBeta.cs is experimental code and is not part of the documented primary API.
Build, Test, and Package
Build
dotnet build IpcNetMq.sln -c Release
To build one target explicitly:
dotnet build IpcNetMq.csproj -c Release -f net10.0
Test
dotnet test .\IpcNetMq.Tests\IpcNetMq.Tests.csproj -c Release
Automated tests cover serialization, concurrent request correlation, cancellation and socket reset, queued-call shutdown, disposal isolation, idle server cancellation, polling identity, malformed/multipart recovery, handler errors, and socket thread/mode ownership. CI builds all three library targets and runs tests on .NET 8 and .NET 10 on Windows and Linux.
Package
The main project is configured to generate a NuGet package on build. It can also be packed explicitly:
dotnet pack IpcNetMq.csproj -c Release
Package outputs are written beneath bin\Release.
Troubleshooting
Address already in use
Only one server can bind a particular endpoint. Stop the existing server or select another port, such as tcp://127.0.0.1:5556.
Client receive timeout
Confirm that:
- The server is running and bound to the same address.
- The server sends exactly one reply for every request.
- The action name is registered by the server.
- The server handler did not throw before sending its reply.
- A debugger pause did not exceed the receive timeout.
The .NET client recreates its REQ socket after an incomplete request/reply exchange.
Sequence mismatch
Do not set IpcPacket.SequenceNumber in application handlers. The client owns request numbering; the .NET server assigns the next even reply number. A non-.NET server must return request.sequence_number + 1.
NetMQ socket thread error
Do not access the underlying socket from another thread. Use CallIpcMethod() or CallIpcMethodAsync() so the client dispatcher retains socket ownership.
Python IndentationError
Python indentation is syntax. Use four spaces consistently, avoid mixing tabs and spaces, and compile-check all three example files:
py -3.11 -m py_compile .\IpcPacket.py .\ServerZeroMQ.py .\UserActions.py
Python cannot import zmq
Install pyzmq into the same Python interpreter used to start the server:
py -3.11 -m pip install pyzmq
py -3.11 -c "import zmq; print(zmq.zmq_version())"
Python edits appear to have no effect
Confirm that the file being edited is the same file shown in the traceback. In particular, IpcPacket.py and a copied file such as IpcPacket(1).py are different modules.
Design Scope and Limitations
- The primary transport pattern is synchronous REQ/REP.
- One client socket has at most one request in flight.
- Server handlers run serially.
- The transport is not a durable queue.
- Authentication, encryption, authorization, schema negotiation, and service discovery are outside the current library.
- TCP endpoints exposed beyond loopback require application-specific network security.
- Large binary payloads are not optimized; the current protocol is JSON-oriented.
- Higher concurrency may eventually require multiple clients/workers or a different ZeroMQ pattern such as ROUTER/DEALER.
Contributing
Issues and pull requests should include:
- A clear description of the behavior or protocol change.
- Tests covering packet compatibility and sequence handling.
- Updates to both C# and Python examples when the wire contract changes.
- Benchmark conditions when making performance claims.
- No application-level assignment of
SequenceNumber.
Keep wire-format changes backward compatible when possible. If compatibility cannot be maintained, update IpcPacket.Version and document the schema change.
License
See LICENSE.txt for the repository's license terms. Package metadata and the repository license file should declare the same license before release.
Release versions
Use yy.mm.dd.HH for NuGet, assembly, and file versions, with the maintainer's local hour. For example, 3 pm on October 10, 2026 is 26.10.10.15. Numeric version components may omit leading zeros.
26.10.10.10
Adds IntelliSense summaries for public types, members, constructors, and methods, including socket ownership, cancellation, timeout, and shutdown behavior. Corrects malformed XML documentation.
26.10.10.8
Fixes per-instance disposal affecting other connections, client cancellation and shutdown, background server cancellation, malformed/multipart request recovery, and polling reply correlation. Wire schema V260107 remains unchanged. Legacy manual packet APIs stay available, but enforce socket thread ownership. Their obsolete async wrappers execute synchronously on the caller's thread; use CallIpcMethodAsync for asynchronous dispatch. Close a legacy connection before switching to dispatcher mode; use Dispose to stop a dispatcher client.
Build packages from the SDK project:
dotnet pack .\IpcNetMq.csproj -c Release -o D:\nuget
The historical standalone .nuspec is not used by SDK packing. The project defines the current target frameworks and dependencies.
Publish to NuGet.org with Trusted Publishing
Publishing is manually triggered; pushes and pull requests only run build/test CI. No stored NuGet API key is required.
One-time setup on NuGet.org, under your username > Trusted Publishing:
| Setting | Value |
|---|---|
| Repository owner | batpox |
| Repository | IpcNetMq |
| Workflow file | publish.yml (file name only) |
| Environment | Leave empty |
| Package pattern/scope | IpcNetMq; allow publishing new versions |
Choose the NuGet account or organization that owns the package. The profile username used below must be the NuGet username associated with the policy, not an email address. For private repositories, NuGet may initially activate the policy for seven days; a successful publish makes that activation permanent.
To publish:
- Commit the desired
yy.mm.dd.HHversion consistently in the project, assembly metadata, and legacy nuspec, and merge it intomain. - Open Actions > Publish NuGet > Run workflow in this GitHub repository.
- Select main, enter your NuGet.org profile username, leave dry_run unchecked, and click Run workflow.
- The workflow builds the solution (net48/net8/net10 library targets), runs .NET 8/10
tests, packs the project, and authenticates via
NuGet/login@v1immediately before publishing the package and its paired symbol package.
Check dry_run to build and test without publishing. Download the resulting packages
from the run's nuget-release artifact. The publishing job receives OIDC permission;
the build job does not.
The workflow publishes the version already committed in IpcNetMq.csproj; it does not
change versions automatically. NuGet.org rejects a package version that already exists.
A failed NuGet login usually indicates a mismatched username or trusted policy.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Framework | net48 is compatible. net481 was computed. |
-
.NETFramework 4.8
- NetMQ (>= 4.0.4.3)
- System.Text.Json (>= 10.0.11)
-
net10.0
- NetMQ (>= 4.0.4.3)
-
net8.0
- NetMQ (>= 4.0.4.3)
- System.Text.Json (>= 10.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.10.10.10 | 132 | 10/10/2026 |
| 26.10.10.8 | 98 | 10/10/2026 |
| 26.8.19 | 236 | 8/20/2026 |
| 26.4.11 | 166 | 4/11/2026 |
| 26.2.1 | 157 | 2/1/2026 |
| 26.1.9 | 166 | 1/10/2026 |
| 26.1.8 | 162 | 1/8/2026 |
| 26.1.6 | 148 | 1/8/2026 |
| 25.11.19 | 471 | 11/19/2025 |
| 1.0.0 | 466 | 11/19/2025 |
Added complete XML summaries for public APIs and corrected XML documentation markup.