本文整理汇总了C#中ZeroMQ.ZContext类的典型用法代码示例。如果您正苦于以下问题:C# ZContext类的具体用法?C# ZContext怎么用?C# ZContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZContext类属于ZeroMQ命名空间,在下文中一共展示了ZContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AsyncSrv_ServerTask
static void AsyncSrv_ServerTask(ZContext context)
{
// This is our server task.
// It uses the multithreaded server model to deal requests out to a pool
// of workers and route replies back to clients. One worker can handle
// one request at a time but one client can talk to multiple workers at
// once.
using (var frontend = new ZSocket(context, ZSocketType.ROUTER))
using (var backend = new ZSocket(context, ZSocketType.DEALER))
{
// Frontend socket talks to clients over TCP
frontend.Bind("tcp://*:5570");
// Backend socket talks to workers over inproc
backend.Bind("inproc://backend");
// Launch pool of worker threads, precise number is not critical
for (int i = 0; i < 5; ++i)
{
int j = i; new Thread(() => AsyncSrv_ServerWorker(context, j)).Start();
}
// Connect backend to frontend via a proxy
ZError error;
if (!ZContext.Proxy(frontend, backend, out error))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
}
}
示例2: 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();
}
}
}
示例3: 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());
}
}
}
示例4: FLClient1_TryRequest
static ZFrame FLClient1_TryRequest(ZContext context, string endpoint, ZFrame request)
{
Console.WriteLine("I: trying echo service at {0}...", endpoint);
using (var client = new ZSocket(context, ZSocketType.REQ))
{
client.Connect(endpoint);
// Send request, wait safely for reply
using (var message = ZFrame.CopyFrom(request))
{
client.Send(message);
}
var poll = ZPollItem.CreateReceiver();
ZError error;
ZMessage incoming;
if (client.PollIn(poll, out incoming, out error, FLClient1_REQUEST_TIMEOUT))
{
return incoming[0];
}
}
return null;
}
示例5: Tripping
// Round-trip demonstrator
// While this example runs in a single process, that is just to make
// it easier to start and stop the example. The client task signals to
// main when it's ready.
public static void Tripping(string[] args)
{
bool verbose = (args.Any(e => e.ToLower().Equals("-v")
|| e.ToLower().Equals("--verbose")));
Console.WriteLine("Verbose: {0}", verbose);
CancellationTokenSource cancellor = new CancellationTokenSource();
Console.CancelKeyPress += (s, ea) =>
{
ea.Cancel = true;
cancellor.Cancel();
};
using (ZContext ctx = new ZContext())
{
using (var client = new ZActor(ctx, Tripping_ClientTask))
{
(new Thread(() => Tripping_WorkerTask(ctx))).Start();
(new Thread(() => Tripping_BrokerTask(ctx))).Start();
client.Start();
using (var signal = client.Frontend.ReceiveFrame())
if (verbose)
signal.ToString().DumpString();
}
}
}
示例6: TaskWork
public static void TaskWork(string[] args)
{
//
// Task worker
// Connects PULL socket to tcp://127.0.0.1:5557
// Collects workloads from ventilator via that socket
// Connects PUSH socket to tcp://127.0.0.1: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("tcp://127.0.0.1:5557");
sink.Connect("tcp://127.0.0.1:5558");
// 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
}
}
}
示例7: 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);
}
}
}
}
示例8: ZActor
public ZActor(ZContext context, ZAction action, params object[] args)
: this(context, default(string), action, args)
{
var rnd0 = new byte[8];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetNonZeroBytes(rnd0);
this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
}
示例9: 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);
}
}
}
示例10: 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);
}
}
}
示例11: 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);
}
}
示例12: 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);
}
}
}
示例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;
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);
}
}
示例14: 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);
}
}
示例15: 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
}
}
}