Skip to content

Commit

Permalink
Updating C# Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
metadings committed Jan 23, 2015
1 parent 24b520b commit 91af9a2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 58 deletions.
50 changes: 21 additions & 29 deletions examples/C#/asyncsrv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ static void AsyncSrv_Client(ZContext context, int i)
// Connect
client.Connect("tcp://localhost:5570");

var poll = ZPollItem.CreateReceiver();

int requests = 0;
ZError error;
ZMessage incoming;
var poll = ZPollItem.CreateReceiver();

int requests = 0;
while (true)
{
// Tick once per second, pulling in arriving messages
Expand All @@ -53,10 +52,7 @@ static void AsyncSrv_Client(ZContext context, int i)
continue;
}
if (error == ZError.ETERM)
{
return;
}

return; // Interrupted
throw new ZException(error);
}
using (incoming)
Expand All @@ -74,23 +70,22 @@ static void AsyncSrv_Client(ZContext context, int i)
if (!client.Send(outgoing, out error))
{
if (error == ZError.ETERM)
{
return;
}
return; // Interrupted
throw new ZException(error);
}
}
}
}
}

// 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.
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 = ZSocket.Create(context, ZSocketType.ROUTER))
using (var backend = ZSocket.Create(context, ZSocketType.DEALER))
{
Expand All @@ -105,41 +100,38 @@ static void AsyncSrv_ServerTask(ZContext context)
int j = i; new Thread(() => AsyncSrv_ServerWorker(context, j)).Start();
}

ZError error;

// Connect backend to frontend via a proxy
ZError error;
if (!ZContext.Proxy(frontend, backend, out error))
{
if (error == ZError.ETERM)
return;

return; // Interrupted
throw new ZException(error);
}
}
}

// Each worker task works on one request at a time and sends a random number
// of replies back, with random delays between replies:
static void AsyncSrv_ServerWorker(ZContext context, int i)
{
// Each worker task works on one request at a time and sends a random number
// of replies back, with random delays between replies:

using (var worker = ZSocket.Create(context, ZSocketType.DEALER))
{
worker.Connect("inproc://backend");

ZError error;
ZMessage request;
var rnd = new Random();

while (true)
{
ZMessage request;
if (null == (request = worker.ReceiveMessage(out error)))
{
if (error == ZError.ETERM)
return;

return; // Interrupted
throw new ZException(error);
}

using (request)
{
// The DEALER socket gives us the reply envelope and message
Expand All @@ -161,8 +153,7 @@ static void AsyncSrv_ServerWorker(ZContext context, int i)
if (!worker.Send(response, out error))
{
if (error == ZError.ETERM)
return;

return; // Interrupted
throw new ZException(error);
}
}
Expand All @@ -172,10 +163,11 @@ static void AsyncSrv_ServerWorker(ZContext context, int i)
}
}

// The main thread simply starts several clients and a server, and then
// waits for the server to finish.
public static void AsyncSrv(IDictionary<string, string> dict, string[] args)
{
// The main thread simply starts several clients and a server, and then
// waits for the server to finish.

using (var context = ZContext.Create())
{
for (int i = 0; i < 5; ++i)
Expand Down
14 changes: 9 additions & 5 deletions examples/C#/hwclient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ public static void HWClient(IDictionary<string, string> dict, string[] args)
// Authors: Pieter Hintjens, Uli Riehm
//

// Create
using (var context = ZContext.Create())
using (var requester = ZSocket.Create(context, ZSocketType.REQ)) {

using (var requester = ZSocket.Create(context, ZSocketType.REQ))
{
// Connect
requester.Connect("tcp://127.0.0.1:5555");

for (var n = 0; n < 10; ++n) {

for (int n = 0; n < 10; ++n)
{
string requestText = "Hello";

Console.Write("Sending {0}... ", requestText);

// Send
using (var request = new ZFrame(requestText))
{
requester.Send(request);
}

// Receive
using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine("Received: {0} {1}!", requestText, reply.ReadString());
Expand Down
7 changes: 6 additions & 1 deletion examples/C#/hwserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ public static void HWServer(IDictionary<string, string> dict, string[] args)
// Authors: Pieter Hintjens, Uli Riehm
//

// Create
using (var context = ZContext.Create())
using (var responder = ZSocket.Create(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);

using (ZFrame reply = new ZFrame("World"))
// Send
using (var reply = new ZFrame("World"))
{
responder.Send(reply);
}
Expand Down
21 changes: 15 additions & 6 deletions examples/C#/interrupt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public static void Interrupt(IDictionary<string, string> dict, string[] args)
using (var context = ZContext.Create())
using (var responder = ZSocket.Create(context, ZSocketType.REP))
{
Console.CancelKeyPress += (sender, e) =>
{
// e.Cancel = false;
context.Terminate();
};

var thread = new Thread(() =>
{
Console.CancelKeyPress += (sender, e) =>
{
// e.Cancel = false;
context.Terminate();
};
while (true)
{
if (Console.KeyAvailable)
Expand All @@ -50,6 +50,7 @@ public static void Interrupt(IDictionary<string, string> dict, string[] args)
thread.Start();
thread.Join(64);


responder.Bind("tcp://*:5555");

ZError error;
Expand All @@ -74,7 +75,15 @@ public static void Interrupt(IDictionary<string, string> dict, string[] args)
Console.Write("Sending {0}... ", respondText);
using (var response = new ZFrame(respondText))
{
responder.Send(response);
if (!responder.Send(response, out error))
{
if (error == ZError.ETERM)
{
Console.WriteLine("Terminating, you have pressed ESC.");
break;
}
throw new ZException(error);
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/C#/ppworker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ public static void PPWorker(IDictionary<string, string> dict, string[] args)
ZMessage incoming;
int cycles = 0;
var poll = ZPollItem.CreateReceiver();
var rnd = new Random();

while (true)
{
var rnd = new Random();

if (worker.PollIn(poll, out incoming, out error, Worker.PPP_TICK))
{
// Get message
Expand Down
9 changes: 7 additions & 2 deletions examples/C#/rrbroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public static void RRBroker(IDictionary<string, string> dict, string[] args)
{
if (frontend.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
// Process all parts of the message
backend.Send(message);
using (message)
{
// Process all parts of the message
Console_WriteZMessage(message, "frontend");
backend.Send(message);
}
}
else
{
Expand All @@ -50,6 +54,7 @@ public static void RRBroker(IDictionary<string, string> dict, string[] args)
if (backend.PollIn(poll, out message, out error, TimeSpan.FromMilliseconds(64)))
{
// Process all parts of the message
Console_WriteZMessage(message, " backend");
frontend.Send(message);
}
else
Expand Down
9 changes: 3 additions & 6 deletions examples/C#/rrclient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ public static void RRClient(IDictionary<string, string> dict, string[] args)
{
requester.Connect("tcp://127.0.0.1:5559");

for (var n = 0; n < 10; ++n)
for (int n = 0; n < 10; ++n)
{
string requestText = "Hello";

Console.Write("Sending {0}... ", requestText);
using (var request = new ZFrame(requestText))
using (var request = new ZFrame("Hello"))
{
requester.Send(request);
}

using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine("Received: {0} {1}!", requestText, reply.ReadString());
Console.WriteLine("Hello {0}!", reply.ReadString());
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions examples/C#/rrworker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public static void RRWorker(IDictionary<string, string> dict, string[] args)
// Authors: Pieter Hintjens, Uli Riehm
//

if (args == null || args.Length == 0)
{
args = new string[] { "World" };
}
string name = args[0];

// Socket to talk to clients
using (var context = ZContext.Create())
using (var responder = ZSocket.Create(context, ZSocketType.REP))
Expand All @@ -31,16 +37,15 @@ public static void RRWorker(IDictionary<string, string> dict, string[] args)
// Wait for next request from client
using (ZFrame request = responder.ReceiveFrame())
{
Console.Write("Received {0}, ", request.ReadString());
Console.Write("{0} ", request.ReadString());
}

// Do some 'work'
Thread.Sleep(1);

// Send reply back to client
string replyText = "World";
Console.WriteLine("Sending {0}... ", replyText);
using (var reply = new ZFrame(replyText))
Console.WriteLine("{0}... ", name);
using (var reply = new ZFrame(name))
{
responder.Send(reply);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/C#/wuserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public static void WUServer(IDictionary<string, string> dict, string[] args)
publisher.Bind("tcp://*:5556");
// publisher.Bind("epgm://eth0,224.0.0.0:5556");

// Initialize random number generator
var rnd = new Random();

while (true)
{
// Initialize random number generator
var rnd = new Random();

// Get values that will fool the boss
int zipcode = rnd.Next(99999);
int temperature = rnd.Next(-55, +45);
Expand Down

0 comments on commit 91af9a2

Please sign in to comment.