本文整理汇总了C#中ZSocket.ReceiveFrame方法的典型用法代码示例。如果您正苦于以下问题:C# ZSocket.ReceiveFrame方法的具体用法?C# ZSocket.ReceiveFrame怎么用?C# ZSocket.ReceiveFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZSocket
的用法示例。
在下文中一共展示了ZSocket.ReceiveFrame方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
示例2: 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());
}
}
}
}
示例3: 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();
}
}
}
示例4: 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);
}
}
示例5: Tripping_ClientTask
static void Tripping_ClientTask(ZContext ctx, ZSocket pipe, CancellationTokenSource cancellor, object[] args)
{
using (ZSocket client = new ZSocket(ctx, ZSocketType.DEALER))
{
client.Connect("tcp://localhost:5555");
"Setting up test...".DumpString();
Thread.Sleep(100);
int requests;
"Synchronous round-trip test...".DumpString();
var start = DateTime.Now;
Stopwatch sw = Stopwatch.StartNew();
for (requests = 0; requests < 10000; requests++)
{
using (var outgoing = new ZFrame("hello"))
{
client.Send(outgoing);
using (var reply = client.ReceiveFrame())
{
if (Verbose)
reply.ToString().DumpString();
}
}
}
sw.Stop();
" {0} calls - {1} ms => {2} calls / second".DumpString(requests, sw.ElapsedMilliseconds, requests * 1000 / sw.ElapsedMilliseconds);
"Asynchronous round-trip test...".DumpString();
sw.Restart();
// sending 100000 requests => often ends in eagain exception in ZContext.Proxy!!
for (requests = 0; requests < 1000; requests++)
using (var outgoing = new ZFrame("hello"))
client.SendFrame(outgoing);
for (requests = 0; requests < 1000; requests++)
using (var reply = client.ReceiveFrame())
if (Verbose)
reply.ToString().DumpString();
sw.Stop();
" {0} calls - {1} ms => {2} calls / second".DumpString(requests, sw.ElapsedMilliseconds, requests * 1000 / sw.ElapsedMilliseconds);
using (var outgoing = new ZFrame("done"))
pipe.SendFrame(outgoing);
}
}
示例6: TaskSink2
public static void TaskSink2(string[] args)
{
//
// Task sink - design 2
// Adds pub-sub flow to send kill signal to workers
//
// Author: metadings
//
// Socket to receive messages on and
// Socket for worker control
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var controller = new ZSocket(context, ZSocketType.PUB))
{
receiver.Bind("tcp://*:5558");
controller.Bind("tcp://*:5559");
// Wait for start of batch
receiver.ReceiveFrame();
// Start our clock now
var stopwatch = new Stopwatch();
stopwatch.Start();
// Process 100 confirmations
for (int i = 0; i < 100; ++i)
{
receiver.ReceiveFrame();
if ((i / 10) * 10 == i)
Console.Write(":");
else
Console.Write(".");
}
stopwatch.Stop();
Console.WriteLine("Total elapsed time: {0} ms", stopwatch.ElapsedMilliseconds);
// Send kill signal to workers
controller.Send(new ZFrame("KILL"));
}
}
示例7: MSReader
public static void MSReader(string[] args)
{
//
// Reading from multiple sockets
// This version uses a simple recv loop
//
// Author: metadings
//
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PULL))
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
// Connect to task ventilator
receiver.Connect("tcp://127.0.0.1:5557");
// Connect to weather server
subscriber.Connect("tcp://127.0.0.1:5556");
subscriber.SetOption(ZSocketOption.SUBSCRIBE, "10001 ");
// Process messages from both sockets
// We prioritize traffic from the task ventilator
ZError error;
ZFrame frame;
while (true)
{
if (null != (frame = receiver.ReceiveFrame(ZSocketFlags.DontWait, out error)))
{
// Process task
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
if (null != (frame = subscriber.ReceiveFrame(ZSocketFlags.DontWait, out error)))
{
// Process weather update
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
// No activity, so sleep for 1 msec
Thread.Sleep(1);
}
}
}
示例8: RRWorker
public static void RRWorker(string[] args)
{
//
// Hello World worker
// Connects REP socket to tcp://127.0.0.1:5560
// Expects "Hello" from client, replies with "World"
//
// Author: metadings
//
if (args == null || args.Length < 2)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} RRWorker [Name] [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Name Your Name");
Console.WriteLine(" Endpoint Where RRWorker should connect to.");
Console.WriteLine(" Default is tcp://127.0.0.1:5560");
Console.WriteLine();
if (args.Length < 1) {
args = new string[] { "World", "tcp://127.0.0.1:5560" };
} else {
args = new string[] { args[0], "tcp://127.0.0.1:5560" };
}
}
string name = args[0];
string endpoint = args[1];
// Socket to talk to clients
using (var context = new ZContext())
using (var responder = new ZSocket(context, ZSocketType.REP))
{
responder.Connect(endpoint);
while (true)
{
// Wait for next request from client
using (ZFrame request = responder.ReceiveFrame())
{
Console.Write("{0} ", request.ReadString());
// Do some 'work'
Thread.Sleep(1);
// Send reply back to client
Console.WriteLine("{0}... ", name);
responder.Send(new ZFrame(name));
}
}
}
}
示例9: TaskSink
public static void TaskSink(string[] args)
{
//
// Task sink
// Binds PULL socket to tcp://127.0.0.1:5558
// Collects results from workers via that socket
//
// Author: metadings
//
// Prepare our context and socket
using (var context = new ZContext())
using (var sink = new ZSocket(context, ZSocketType.PULL))
{
sink.Bind("tcp://*:5558");
// Wait for start of batch
sink.ReceiveFrame();
// Start our clock now
var stopwatch = new Stopwatch();
stopwatch.Start();
// Process 100 confirmations
for (int i = 0; i < 100; ++i)
{
sink.ReceiveFrame();
if ((i / 10) * 10 == i)
Console.Write(":");
else
Console.Write(".");
}
// Calculate and report duration of batch
stopwatch.Stop();
Console.WriteLine("Total elapsed time: {0} ms", stopwatch.ElapsedMilliseconds);
}
}
示例10: Main
public static void Main(string[] args)
{
//
// Synchronized subscriber
//
// Author: metadings
//
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
using (var syncclient = new ZSocket(context, ZSocketType.REQ))
{
// First, connect our subscriber socket
subscriber.Connect("tcp://127.0.0.1:5561");
subscriber.SubscribeAll();
// 0MQ is so fast, we need to wait a while…
Thread.Sleep(1000);
// Second, synchronize with publisher
syncclient.Connect("tcp://127.0.0.1:5562");
// - send a synchronization request
syncclient.Send(new ZFrame());
// - wait for synchronization reply
syncclient.ReceiveFrame();
// Third, get our updates and report how many we got
int i = 0;
while (true)
{
using (ZFrame frame = subscriber.ReceiveFrame())
{
string text = frame.ReadString();
if (text == "END")
{
break;
}
frame.Position = 0;
Console.WriteLine("Receiving {0}…", frame.ReadInt32());
++i;
}
}
Console.WriteLine("Received {0} updates.", i);
Console.ReadLine();
}
}
示例11: SuiSnail_Subscriber
static void SuiSnail_Subscriber(ZContext context, ZSocket backend, CancellationTokenSource cancellor, object[] args)
{
// This is our subscriber. It connects to the publisher and subscribes
// to everything. It sleeps for a short time between messages to
// simulate doing too much work. If a message is more than one second
// late, it croaks.
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
// Subscribe to everything
subscriber.SubscribeAll();
subscriber.Connect("tcp://127.0.0.1:5556");
ZFrame incoming;
ZError error;
var rnd = new Random();
while (!cancellor.IsCancellationRequested)
{
// Get and process messages
if (null != (incoming = subscriber.ReceiveFrame(out error)))
{
string terms = incoming.ReadString();
Console.WriteLine(terms);
var clock = DateTime.Parse(terms);
// Suicide snail logic
if (DateTime.UtcNow - clock > SuiSnail_MAX_ALLOWED_DELAY)
{
Console.WriteLine("E: subscriber cannot keep up, aborting");
break;
}
// Work for 1 msec plus some random additional time
Thread.Sleep(1 + rnd.Next(200));
}
else
{
if (error == ZError.ETERM)
break; // Interrupted
throw new ZException(error);
}
}
backend.Send(new ZFrame("gone and died"));
}
}
示例12: Peering2_WorkerTask
static void Peering2_WorkerTask(ZContext context, int i, string name)
{
// The worker task plugs into the load-balancer using a REQ socket
using (var worker = new ZSocket(context, ZSocketType.REQ))
{
// Set printable identity
worker.IdentityString = name;
// Connect
worker.Connect("tcp://127.0.0.1:" + Peering2_GetPort(name) + 2);
// Tell broker we're ready for work
worker.Send(new ZFrame("READY"));
// Process messages as they arrive
ZError error;
while (true)
{
// Receive
ZFrame incoming = worker.ReceiveFrame(out error);
if (incoming == null)
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
using (incoming)
{
Console.WriteLine("Worker {0}: {1}", name, incoming.ReadString());
// Do some heavy work
Thread.Sleep(1);
// Send
using (var outgoing = new ZFrame("OK"))
{
worker.Send(outgoing);
}
}
}
}
}
示例13: Espresso_Listener
static void Espresso_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");
ZError error;
ZFrame frame;
while (true)
{
if (null != (frame = listener.ReceiveFrame(out error)))
{
using (frame)
{
byte first = frame.ReadAsByte();
var rest = new byte[9];
frame.Read(rest, 0, rest.Length);
Console.WriteLine("{0} {1}", (char)first, rest.ToHexString());
if (first == 0x01)
{
// Subscribe
}
else if (first == 0x00)
{
// Unsubscribe
context.Shutdown();
}
}
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
}
}
}
示例14: HWClient
public static void HWClient(string[] args)
{
//
// Hello World client
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} HWClient [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Endpoint Where HWClient should connect to.");
Console.WriteLine(" Default is tcp://127.0.0.1:5555");
Console.WriteLine();
args = new string[] { "tcp://127.0.0.1:5555" };
}
string endpoint = args[0];
// Create
using (var context = new ZContext())
using (var requester = new ZSocket(context, ZSocketType.REQ))
{
// Connect
requester.Connect(endpoint);
for (int n = 0; n < 10; ++n)
{
string requestText = "Hello";
Console.Write("Sending {0}...", requestText);
// Send
requester.Send(new ZFrame(requestText));
// Receive
using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine(" Received: {0} {1}!", requestText, reply.ReadString());
}
}
}
}
示例15: HWServer
public static void HWServer(string[] args)
{
//
// Hello World server
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} HWServer [Name]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Name Your name. Default: World");
Console.WriteLine();
args = new string[] { "World" };
}
string name = args[0];
// Create
using (var context = new ZContext())
using (var responder = new ZSocket(context, ZSocketType.REP))
{
// Bind
responder.Bind("tcp://*:5555");
while (true)
{
// Receive
using (ZFrame request = responder.ReceiveFrame())
{
Console.WriteLine("Received {0}", request.ReadString());
// Do some work
Thread.Sleep(1);
// Send
responder.Send(new ZFrame(name));
}
}
}
}