本文整理汇总了C#中ZSocket.Bind方法的典型用法代码示例。如果您正苦于以下问题:C# ZSocket.Bind方法的具体用法?C# ZSocket.Bind怎么用?C# ZSocket.Bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZSocket
的用法示例。
在下文中一共展示了ZSocket.Bind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
}
示例2: 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);
}
}
}
示例3: 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);
}
}
示例4: 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);
}
}
}
示例5: 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);
}
}
}
示例6: RRBroker
public static void RRBroker(string[] args)
{
//
// Simple request-reply broker
//
// Author: metadings
//
// Prepare our context and sockets
using (var ctx = new ZContext())
using (var frontend = new ZSocket(ctx, ZSocketType.ROUTER))
using (var backend = new ZSocket(ctx, ZSocketType.DEALER))
{
frontend.Bind("tcp://*:5559");
backend.Bind("tcp://*:5560");
// Initialize poll set
var poll = ZPollItem.CreateReceiver();
// Switch messages between sockets
ZError error;
ZMessage message;
while (true)
{
if (frontend.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
// Process all parts of the message
Console_WriteZMessage("frontend", 2, message);
backend.Send(message);
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
if (backend.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
// Process all parts of the message
Console_WriteZMessage(" backend", 2, message);
frontend.Send(message);
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
}
}
}
示例7: RTReq
public static void RTReq(string[] args)
{
//
// ROUTER-to-REQ example
//
// While this example runs in a single process, that is only to make
// it easier to start and stop the example. Each thread has its own
// context and conceptually acts as a separate process.
//
// Author: metadings
//
using (var context = new ZContext())
using (var broker = new ZSocket(context, ZSocketType.ROUTER))
{
broker.Bind("tcp://*:5671");
for (int i = 0; i < RTReq_Workers; ++i)
{
int j = i; new Thread(() => RTReq_Worker(j)).Start();
}
var stopwatch = new Stopwatch();
stopwatch.Start();
// Run for five seconds and then tell workers to end
int workers_fired = 0;
while (true)
{
// Next message gives us least recently used worker
using (ZMessage identity = broker.ReceiveMessage())
{
broker.SendMore(identity[0]);
broker.SendMore(new ZFrame());
// Encourage workers until it's time to fire them
if (stopwatch.Elapsed < TimeSpan.FromSeconds(5))
{
broker.Send(new ZFrame("Work harder!"));
}
else
{
broker.Send(new ZFrame("Fired!"));
if (++workers_fired == RTReq_Workers)
{
break;
}
}
}
}
}
}
示例8: BaseNetMqServer
protected BaseNetMqServer(int responsePort = -1, int publishPort = 0, string host = "*")
{
Host = host;
ServerToken = GenerateKey("Server");
_backendSocket = new ZSocket(NetMqManager.Instance.Context, ZSocketType.ROUTER);
_backendSocket.Bind(ServerBackendAddr);
InitWorkers();
_responseSocket = new ZSocket(NetMqManager.Instance.Context, ZSocketType.ROUTER);
if (responsePort == -1)
{
while (true)
{
var rand = new Random();
ResponsePort = rand.Next(50000, 60000);
try
{
_responseSocket.Bind(string.Format("tcp://{0}:{1}", host, ResponsePort));
break;
}
catch (Exception)
{
continue;
}
}
}
else
{
ResponsePort = responsePort;
_responseSocket.Bind(string.Format("tcp://{0}:{1}", host, ResponsePort));
}
//Coroutine2.Start(MainLoop());
//MainLoop();
_mainLoppThread = new Thread(MainLoop);
_mainLoppThread.Start();
}
示例9: PathoPub
public static void PathoPub(string[] args)
{
//
// Pathological publisher
// Sends out 1,000 topics and then one random update per second
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} PathoPub [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Endpoint Where PathoPub should connect to.");
Console.WriteLine(" Default is null, Binding on tcp://*:5556");
Console.WriteLine();
args = new string[] { null };
}
using (var context = new ZContext())
using (var publisher = new ZSocket(context, ZSocketType.PUB))
{
if (args[0] != null)
{
publisher.Connect(args[0]);
}
else
{
publisher.Bind("tcp://*:5556");
}
// Ensure subscriber connection has time to complete
Thread.Sleep(100);
// Send out all 1,000 topic messages
for (int topic = 0; topic < 1000; ++topic)
{
publisher.SendMore(new ZFrame(string.Format("{0:D3}", topic)));
publisher.Send(new ZFrame("Save Roger"));
}
// Send one random update per second
var rnd = new Random();
while (true)
{
Thread.Sleep(10);
publisher.SendMore(new ZFrame(string.Format("{0:D3}", rnd.Next(1000))));
publisher.Send(new ZFrame("Off with his head!"));
}
}
}
示例10: Producer
public Producer(ITransportSettings transportSettings, IDictionary<string, IList<string>> queueMappings)
{
_transportSettings = transportSettings;
_queueMappings = queueMappings;
if (_transportSettings.ClientSettings.ContainsKey("PublisherHost"))
{
_publishContext = new ZContext();
_publisher = new ZSocket(_publishContext, ZSocketType.PUB);
_publisher.Linger = TimeSpan.FromMilliseconds(1);
_publisher.Bind(_transportSettings.ClientSettings["PublisherHost"].ToString());
}
}
示例11: WUProxy
public static void WUProxy(string[] args)
{
//
// Weather proxy device
//
// Author: metadings
//
using (var context = new ZContext())
using (var frontend = new ZSocket(context, ZSocketType.XSUB))
using (var backend = new ZSocket(context, ZSocketType.XPUB))
{
// Frontend is where the weather server sits
string localhost = "tcp://127.0.0.1:5556";
Console.WriteLine("I: Connecting to {0}", localhost);
frontend.Connect(localhost);
// Backend is our public endpoint for subscribers
foreach (IPAddress address in WUProxy_GetPublicIPs())
{
var tcpAddress = string.Format("tcp://{0}:8100", address);
Console.WriteLine("I: Binding on {0}", tcpAddress);
backend.Bind(tcpAddress);
var epgmAddress = string.Format("epgm://{0};239.192.1.1:8100", address);
Console.WriteLine("I: Binding on {0}", epgmAddress);
backend.Bind(epgmAddress);
}
using (var subscription = ZFrame.Create(1))
{
subscription.Write(new byte[] { 0x1 }, 0, 1);
backend.Send(subscription);
}
// Run the proxy until the user interrupts us
ZContext.Proxy(frontend, backend);
}
}
示例12: FLServer1
public static void FLServer1(string[] args)
{
//
// Freelance server - Model 1
// Trivial echo service
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} FLServer1 [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Endpoint Where FLServer1 should bind on.");
Console.WriteLine(" Default is tcp://127.0.0.1:7780");
Console.WriteLine();
args = new string[] { "tcp://127.0.0.1:7780" };
}
using (var context = new ZContext())
using (var server = new ZSocket(context, ZSocketType.REP))
{
server.Bind(args[0]);
Console.WriteLine("I: echo service is ready at {0}", args[0]);
ZMessage message;
ZError error;
while (true)
{
if (null != (message = server.ReceiveMessage(out error)))
{
using (message)
{
server.Send(message);
}
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
}
}
}
示例13: TaskVent
public TaskVent(int SenderPort=5557,string sinkIP="127.0.0.1",int sinkPort=5558)
{
//
// Task ventilator
// Binds PUSH socket to tcp://localhost:5557
// Sends batch of tasks to workers via that socket
//
// Author: metadings
//
// Socket to send messages on and
// Socket to send start of batch message on
using (var context = new ZContext())
using (var sender = new ZSocket(context, ZSocketType.PUSH))
using (var sink = new ZSocket(context, ZSocketType.PUSH))
{
sender.Bind("tcp://*:"+SenderPort.ToString());
sink.Connect(String.Format("tcp://{0}:{1}",sinkIP,sinkPort));
Console.WriteLine("Press ENTER when the workers are ready…");
Console.ReadKey(true);
Console.WriteLine("Sending tasks to workers…");
// The first message is "0" and signals start of batch
sink.Send(new byte[] { 0x00 }, 0, 1);
// Initialize random number generator
var rnd = new Random();
// Send 100 tasks
int i = 0;
long total_msec = 0; // Total expected cost in msecs
for (; i < 100; ++i)
{
// Random workload from 1 to 100msecs
int workload = 1000;
total_msec += workload;
byte[] action = BitConverter.GetBytes(workload);
Console.WriteLine("*{0}* ", workload);
sender.Send(action, 0, action.Length);
}
Console.WriteLine("Total expected cost: {0} ms", total_msec);
}
}
示例14: Tripping_BrokerTask
// .split broker task
// Here is the broker task. It uses the {{zmq_proxy}} function to switch
// messages between frontend and backend:
static void Tripping_BrokerTask(ZContext ctx)
{
using (var frontend = new ZSocket(ctx, ZSocketType.DEALER))
using (var backend = new ZSocket(ctx, ZSocketType.DEALER))
{
frontend.Bind("tcp://*:5555");
backend.Bind("tcp://*:5556");
ZError error;
if (!ZContext.Proxy(frontend, backend, out error))
{
if (Equals(error, ZError.ETERM))
return; // Interrupted
throw new ZException(error);
}
}
}
示例15: TaskVent
public static void TaskVent(string[] args)
{
//
// Task ventilator
// Binds PUSH socket to tcp://127.0.0.1:5557
// Sends batch of tasks to workers via that socket
//
// Author: metadings
//
// Socket to send messages on and
// Socket to send start of batch message on
using (var context = new ZContext())
using (var sender = new ZSocket(context, ZSocketType.PUSH))
using (var sink = new ZSocket(context, ZSocketType.PUSH))
{
sender.Bind("tcp://*:5557");
sink.Connect("tcp://127.0.0.1:5558");
Console.WriteLine("Press ENTER when the workers are ready...");
Console.ReadKey(true);
Console.WriteLine("Sending tasks to workers...");
// The first message is "0" and signals start of batch
sink.Send(new byte[] { 0x00 }, 0, 1);
// Initialize random number generator
var rnd = new Random();
// Send 100 tasks
int i = 0;
long total_msec = 0; // Total expected cost in msecs
for (; i < 100; ++i)
{
// Random workload from 1 to 100msecs
int workload = rnd.Next(100) + 1;
total_msec += workload;
byte[] action = BitConverter.GetBytes(workload);
Console.WriteLine("{0}", workload);
sender.Send(action, 0, action.Length);
}
Console.WriteLine("Total expected cost: {0} ms", total_msec);
}
}