Giter Site home page Giter Site logo

isynaptic / castlezmq Goto Github PK

View Code? Open in Web Editor NEW

This project forked from castleproject-deprecated/castlezmq

0.0 3.0 0.0 3.78 MB

Modern and robust .net binding for 0mq (zmq)

License: GNU Lesser General Public License v3.0

Shell 0.08% C# 99.92%

castlezmq's Introduction

castle.zmq

Modern and robust .net binding for 0mq (zmq). From the http://www.castleproject.org/ people.

Why

The existing bindings for .net are either staled or not really built for real world usage. Out of need, we had to create a new one.

Notes

  • The libzmq.dll included has been compiled with FD_SETSIZE=2400. The default (winsock) config is 64
  • You must use the correct dll and libzmq for your processor architecture (or, for the hosting process). In other words, if the hosting process is x86, use the x86. Otherwise x64. If you don't, expect BadImageFormatException

Using it

First of all, be familiar with zmq. Read its fantastic amazing guide & check their api.

Before using it, initiate a context.

using Castle.Zmq;
... 
var ctx = new Context();
...
// some serious profitable work here
...
ctx.Dispose();

Multipart messages

Use the hasMoreToSend which internally is mapped to SNDMORE

socket.Send(byteBuffer1, hasMoreToSend: true);
socket.Send(byteBuffer2, hasMoreToSend: true);
socket.Send(finalBuffer); // no flag

On the receiving side, you can check if there's more to be received:

var data1 = subSocket.Recv();
if (subSocket.HasMoreToRecv()) 
{
	var data2 = subSocket.Recv(); 
}

Req/Rep

const string MsgReq = "Hello";
const string MsgReply = "World";

using (var reqSocket = base.Context.CreateSocket(SocketType.Req))
using (var repSocket = base.Context.CreateSocket(SocketType.Rep))
{
	repSocket.Bind("tcp://0.0.0.0:90002");

	reqSocket.Connect("tcp://127.0.0.1:90002");

	reqSocket.Send(MsgReq);

	var msg = repSocket.Recv();
	var msgStr = Encoding.UTF8.GetString(msg);

	repSocket.Send(MsgReply);

	msg = reqSocket.Recv();
	msgStr = Encoding.UTF8.GetString(msg);
}

Pub/Sub

A publisher should send the topic then send the data. Use the flag to indicate there's more coming:

var pubSocket = base.Context.CreateSocket(SocketType.Pub)

pubSocket.Send("topic", null, hasMoreToSend: true);
pubSocket.Send("data");

The subscriber socket should (duh) subscribe to the topics of interest or all.

var subSocket = base.Context.CreateSocket(SocketType.Sub)

// specific topic
subSocket.Subscribe("topicX");

// everything
subSocket.SubscribeAll();

Note that on the sub side the first Recv will get the topic, the next the actual data.

Polling

Use the Polling class to specify the events and sockets you're interested in polling.

using (var repSocket = base.Context.CreateSocket(SocketType.Rep))
using (var reqSocket = base.Context.CreateSocket(SocketType.Req))
{
	repSocket.Bind("tcp://0.0.0.0:90001");

	var polling = new Polling(PollingEvents.RecvReady, repSocket, reqSocket);

	polling.RecvReady += (socket) =>
	{
		// using socket.Recv() here is guaranted to return stuff
	};
	
	reqSocket.Connect("tcp://127.0.0.1:90001");
	reqSocket.Send("Hello");
	
	polling.PollForever(); // this returns once some socket event happens
}

Monitoring

In progress

Writing test

The Socket and Context use the IZmqContext and IZmqSocket in order to allow you to mock/stub their real implementation.

Additions & Extensions

In progress

Device

WorkerPool

RPC

In progress

How to help

Send a pull request

Contact

Check Castle's Get Involved page.

castlezmq's People

Contributors

hammett avatar isynaptic avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.