Last active
December 10, 2015 04:48
-
-
Save parsnips/4383300 to your computer and use it in GitHub Desktop.
A clrzmq router that actually compiles & routes with the 3.x stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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