patterncsharpMinor
Lazy Pirate Request Response pattern using zeromq and C#/.NET bindings
Viewed 0 times
bindingsresponserequestzeromqlazynetusingandpatternpirate
Problem
I'm a zeromq newb and have implemented the REQ/RES Lazy Pirate pattern with C#. It's essentially a port from the C example given in the documentation. It's harnessed by tests, and seems to be work to my expectations. I'm interested to know if I'm understanding the pattern correctly, or if I can improve the API. If anyone knows of a pre-existing solution, I'd love to know too. Any advice would be great.
```
using System;
using System.Text;
using ZMQ;
namespace zeromq.net {
public class LazyPirateClient : ReqRes {
private readonly string _address;
private readonly long _timeOut;
private readonly int _retries;
private bool _responseReceived;
private int _attempt;
public LazyPirateClient(string address, long timeOut, int retries) {
_address = address;
_timeOut = timeOut;
_retries = retries;
}
public override void Send(Func byteProvider) {
_responseReceived = false;
using (var context = new Context(1)) {
_attempt = 0;
do {
using (var requester = context.Socket(SocketType.REQ)) {
_attempt++;
requester.Connect(_address);
requester.Send(byteProvider(), SendRecvOpt.NOBLOCK);
var item = requester.CreatePollItem(IOMultiPlex.POLLIN);
item.PollInHandler += item_PollInHandler;
context.Poll(new[] { item }, _timeOut);
requester.Linger = 0;
}
} while (_attempt byteProvider);
public delegate void ResponseReceived(object sender, ResponseReceivedEventArgs e);
public event ResponseReceived ReceivedHandler;
public void OnReceived(ResponseReceivedEventArgs e) {
var handler = ReceivedHandler;
if (handler != null)
```
using System;
using System.Text;
using ZMQ;
namespace zeromq.net {
public class LazyPirateClient : ReqRes {
private readonly string _address;
private readonly long _timeOut;
private readonly int _retries;
private bool _responseReceived;
private int _attempt;
public LazyPirateClient(string address, long timeOut, int retries) {
_address = address;
_timeOut = timeOut;
_retries = retries;
}
public override void Send(Func byteProvider) {
_responseReceived = false;
using (var context = new Context(1)) {
_attempt = 0;
do {
using (var requester = context.Socket(SocketType.REQ)) {
_attempt++;
requester.Connect(_address);
requester.Send(byteProvider(), SendRecvOpt.NOBLOCK);
var item = requester.CreatePollItem(IOMultiPlex.POLLIN);
item.PollInHandler += item_PollInHandler;
context.Poll(new[] { item }, _timeOut);
requester.Linger = 0;
}
} while (_attempt byteProvider);
public delegate void ResponseReceived(object sender, ResponseReceivedEventArgs e);
public event ResponseReceived ReceivedHandler;
public void OnReceived(ResponseReceivedEventArgs e) {
var handler = ReceivedHandler;
if (handler != null)
Solution
It seems zeromq will release sent data itself. With GC supported in C#, this feature is useless. The
byteProvider parameter seems inappropriate, so why not just use IEnumerable?Context
StackExchange Code Review Q#8564, answer score: 2
Revisions (0)
No revisions yet.