本文整理汇总了C#中ZSocket类的典型用法代码示例。如果您正苦于以下问题:C# ZSocket类的具体用法?C# ZSocket怎么用?C# ZSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZSocket类属于命名空间,在下文中一共展示了ZSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Espresso0_Listener
static void Espresso0_Listener(ZContext context)
{
// The listener receives all messages flowing through the proxy, on its
// pipe. In CZMQ, the pipe is a pair of ZMQ_PAIR sockets that connect
// attached child threads. In other languages your mileage may vary:
using (var listener = new ZSocket(context, ZSocketType.PAIR))
{
listener.Connect("inproc://listener");
//Print everything that arrives on pipe
ZError error;
ZFrame frame;
while (true)
{
if (null == (frame = listener.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
using (frame)
frame.DumpZfrm();
}
}
}
示例2: Main
static void Main(string[] args)
{
var queueAddress = Config.Get("Queues.Fulfilment.Address");
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
{
receiver.Bind(queueAddress);
Console.WriteLine("Listening for messages on: " + queueAddress);
while (true)
{
using (var message = receiver.ReceiveMessage())
{
var headerFrame = message.First();
var header = JsonConvert.DeserializeObject<Header>(headerFrame.ReadString());
Console.WriteLine("* Received message, ID: {0}, body type: {1}, handled count: {2}", header.MessageId, header.BodyType, header.HandledCount);
//assume this is a permanent failure
if (header.HandledCount < 3)
{
Console.WriteLine("** Handling message. Previous attempts: {0}", header.HandledCount);
Handle(header, message.ElementAt(1));
}
else
{
Console.WriteLine("!! Message has failed {0} times. Not processing. Last exception: {1}", header.HandledCount, header.LastExceptionMessage);
//TODO - forward to error queue
}
}
Thread.Sleep(100);
}
}
}
示例3: Espresso_Publisher
static void Espresso_Publisher(ZContext context)
{
// The publisher sends random messages starting with A-J:
using (var publisher = new ZSocket(context, ZSocketType.PUB))
{
publisher.Bind("tcp://*:6000");
ZError error;
while (true)
{
var bytes = new byte[5];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(bytes);
}
if (!publisher.SendBytes(bytes, 0, bytes.Length, ZSocketFlags.None, out error))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
Thread.Sleep(1);
}
}
}
示例4: Espresso
public static void Espresso(string[] args)
{
//
// Espresso Pattern
// This shows how to capture data using a pub-sub proxy
//
// Author: metadings
//
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.XSUB))
using (var publisher = new ZSocket(context, ZSocketType.XPUB))
using (var listener = new ZSocket(context, ZSocketType.PAIR))
{
new Thread(() => Espresso_Publisher(context)).Start();
new Thread(() => Espresso_Subscriber(context)).Start();
new Thread(() => Espresso_Listener(context)).Start();
subscriber.Connect("tcp://127.0.0.1:6000");
publisher.Bind("tcp://*:6001");
listener.Bind("inproc://listener");
ZError error;
if (!ZContext.Proxy(subscriber, publisher, listener, out error))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
}
}
示例5: RRClient
public static void RRClient(string[] args)
{
//
// Hello World client
// Connects REQ socket to tcp://localhost:5559
// Sends "Hello" to server, expects "World" back
//
// Author: metadings
//
// Socket to talk to server
using (var context = new ZContext())
using (var requester = new ZSocket(context, ZSocketType.REQ))
{
requester.Connect("tcp://127.0.0.1:5559");
for (int n = 0; n < 10; ++n)
{
requester.Send(new ZFrame("Hello"));
using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine("Hello {0}!", reply.ReadString());
}
}
}
}
示例6: Espresso0_Subscriber
// The subscriber thread requests messages starting with
// A and B, then reads and counts incoming messages.
static void Espresso0_Subscriber(ZContext context)
{
// Subscrie to "A" and "B"
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:6001");
subscriber.Subscribe("A");
subscriber.Subscribe("B");
ZError error;
ZFrame frm;
int count = 0;
while (count < 5)
{
if (null == (frm = subscriber.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
++count;
}
Console.WriteLine("I: subscriber counted {0}", count);
}
}
示例7: PSEnvPub
public static void PSEnvPub(string[] args)
{
//
// Pubsub envelope publisher
//
// Author: metadings
//
// Prepare our context and publisher
using (var context = new ZContext())
using (var publisher = new ZSocket(context, ZSocketType.PUB))
{
publisher.Linger = TimeSpan.Zero;
publisher.Bind("tcp://*:5563");
while (true)
{
// Write two messages, each with an envelope and content
using (var message = new ZMessage())
{
message.Add(new ZFrame("A"));
message.Add(new ZFrame("We don't want to see this"));
publisher.Send(message);
}
using (var message = new ZMessage())
{
message.Add(new ZFrame("B"));
message.Add(new ZFrame("We would like to see this"));
publisher.Send(message);
}
Thread.Sleep(1000);
}
}
}
示例8: PSEnvSub
public static void PSEnvSub(string[] args)
{
//
// Pubsub envelope subscriber
//
// Author: metadings
//
// Prepare our context and subscriber
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:5563");
subscriber.Subscribe("B");
int subscribed = 0;
while (true)
{
using (ZMessage message = subscriber.ReceiveMessage())
{
subscribed++;
// Read envelope with address
string address = message[0].ReadString();
// Read message contents
string contents = message[1].ReadString();
Console.WriteLine("{0}. [{1}] {2}", subscribed, address, contents);
}
}
}
}
示例9: LBBroker_Client
// Basic request-reply client using REQ socket
static void LBBroker_Client(ZContext context, int i)
{
// Create a socket
using (var client = new ZSocket(context, ZSocketType.REQ))
{
// Set a printable identity
client.IdentityString = "CLIENT" + i;
// Connect
client.Connect("inproc://frontend");
using (var request = new ZMessage())
{
request.Add(new ZFrame("Hello"));
// Send request
client.Send(request);
}
// Receive reply
using (ZMessage reply = client.ReceiveMessage())
{
Console.WriteLine("CLIENT{0}: {1}", i, reply[0].ReadString());
}
}
}
示例10: TaskWorker
public TaskWorker(string workerid="*",string SenderIP = "127.0.0.1", int SenderPort = 5557, string sinkIP = "127.0.0.1", int sinkPort=5558)
{
//
// Task worker
// Connects PULL socket to tcp://localhost:5557
// Collects workloads from ventilator via that socket
// Connects PUSH socket to tcp://localhost:5558
// Sends results to sink via that socket
//
// Author: metadings
//
// Socket to receive messages on and
// Socket to send messages to
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var sink = new ZSocket(context, ZSocketType.PUSH))
{
receiver.Connect(String.Format ("tcp://{0}:{1}",SenderIP,SenderPort));
sink.Connect(string.Format("tcp://{0}:{1}",sinkIP,sinkPort ));
Console.WriteLine("Worker " + workerid + " ready.");
// Process tasks forever
while (true)
{
var replyBytes = new byte[4];
receiver.ReceiveBytes(replyBytes, 0, replyBytes.Length);
int workload = BitConverter.ToInt32(replyBytes, 0);
Console.WriteLine("{0}.", workload); // Show progress
Thread.Sleep(workload); // Do the work
sink.Send(new byte[0], 0, 0); // Send results to sink
}
}
}
示例11: PollSingle
unsafe internal static bool PollSingle(
ZSocket socket,
ZPollItem item, ZPoll pollEvents,
out ZError error, TimeSpan? timeout = null)
{
error = default(ZError);
bool result = false;
int timeoutMs = !timeout.HasValue ? -1 : (int)timeout.Value.TotalMilliseconds;
zmq_pollitem_posix_t* native = stackalloc zmq_pollitem_posix_t[1];
// fixed (zmq_pollitem_posix_t* native = managedArray) {
native->SocketPtr = socket.SocketPtr;
native->Events = (short)(item.Events & pollEvents);
native->ReadyEvents = (short)ZPoll.None;
while (!(result = (-1 != zmq.poll(native, 1, timeoutMs))))
{
error = ZError.GetLastErr();
if (error == ZError.EINTR)
{
error = default(ZError);
continue;
}
break;
}
item.ReadyEvents = (ZPoll)native->ReadyEvents;
//}
return result;
}
示例12: RTReq_Worker
static void RTReq_Worker(int i)
{
using (var context = new ZContext())
using (var worker = new ZSocket(context, ZSocketType.REQ))
{
worker.IdentityString = "PEER" + i; // Set a printable identity
worker.Connect("tcp://127.0.0.1:5671");
int total = 0;
while (true)
{
// Tell the broker we're ready for work
worker.Send(new ZFrame("Hi Boss"));
// Get workload from broker, until finished
using (ZFrame frame = worker.ReceiveFrame())
{
bool finished = (frame.ReadString() == "Fired!");
if (finished)
{
break;
}
}
total++;
// Do some random work
Thread.Sleep(1);
}
Console.WriteLine("Completed: PEER{0}, {1} tasks", i, total);
}
}
示例13: Espresso_Subscriber
static void Espresso_Subscriber(ZContext context)
{
// The subscriber thread requests messages starting with
// A and B, then reads and counts incoming messages.
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:6001");
subscriber.Subscribe("A");
subscriber.Subscribe("B");
ZError error;
int count = 0;
while (count < 5)
{
var bytes = new byte[10];
int bytesLength;
if (-1 == (bytesLength = subscriber.ReceiveBytes(bytes, 0, bytes.Length, ZSocketFlags.None, out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
++count;
}
Console.WriteLine("I: subscriber counted {0}", count);
}
}
示例14: Espresso_Subscriber
static void Espresso_Subscriber(ZContext context)
{
// The subscriber thread requests messages starting with
// A and B, then reads and counts incoming messages.
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect("tcp://127.0.0.1:6001");
subscriber.Subscribe("A");
subscriber.Subscribe("B");
ZError error;
ZFrame frame;
int count = 0;
while (count < 5)
{
if (null == (frame = subscriber.ReceiveFrame(out error)))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
++count;
}
Console.WriteLine("I: subscriber counted {0}", count);
}
}
示例15: Main
public static void Main(string[] args)
{
//
// Multithreaded Hello World server
//
// Author: metadings
//
// Socket to talk to clients and
// Socket to talk to workers
using (var ctx = new ZContext())
using (var clients = new ZSocket(ctx, ZSocketType.ROUTER))
using (var workers = new ZSocket(ctx, ZSocketType.DEALER))
{
clients.Bind("tcp://*:5555");
workers.Bind("inproc://workers");
// Launch pool of worker threads
for (int i = 0; i < 5; ++i)
{
new Thread(() => MTServer_Worker(ctx)).Start();
}
// Connect work threads to client threads via a queue proxy
ZContext.Proxy(clients, workers);
}
}