Skip to content

Instantly share code, notes, and snippets.

@parsnips
Last active December 10, 2015 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsnips/4383300 to your computer and use it in GitHub Desktop.
Save parsnips/4383300 to your computer and use it in GitHub Desktop.
A clrzmq router that actually compiles & routes with the 3.x stuff.
using System.Collections.Generic;
using ZeroMQ;
namespace Parsnips.ZeroMQ
{
class Program
{
static void Main(string[] args)
{
using (var context = ZmqContext.Create())
{
using (
ZmqSocket frontend = context.CreateSocket(SocketType.ROUTER),
backend = context.CreateSocket(SocketType.DEALER))
{
frontend.Bind("tcp://*:5559");
backend.Bind("tcp://*:5560");
frontend.ReceiveReady += (sender, eventArgs) => RelayMessage(eventArgs.Socket, backend);
backend.ReceiveReady += (sender, eventArgs) => RelayMessage(eventArgs.Socket, frontend);
using (var poller = new ZeroMQ.Poller(new List<ZmqSocket> {frontend, backend}))
{
while(true)
{
poller.Poll();
}
}
}
}
}
private static void RelayMessage(ZmqSocket source, ZmqSocket destination)
{
bool hasMore = true;
while (hasMore)
{
byte[] buffer = new byte[0];
int size;
byte[] message = source.Receive(buffer, out size);
hasMore = source.ReceiveMore;
destination.Send(message, size, hasMore ? SocketFlags.SendMore : SocketFlags.None);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment