Skip to content

Commit

Permalink
Updating C# Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
metadings committed Feb 9, 2015
1 parent 7b757da commit bec72b8
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/C#/asyncsrv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void AsyncSrv_Client(ZContext context, int i)
{
if (error == ZError.EAGAIN)
{
error = ZError.None;
Thread.Sleep(1);
continue;
}
if (error == ZError.ETERM)
Expand Down
4 changes: 1 addition & 3 deletions examples/C#/eagain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ public static void EAgain(IDictionary<string, string> dict, string[] args)
Console.WriteLine(new ZException(error));
break;

/* Usually, when reaching EAGAIN you would do
error = ZError.None; // = default(ZError); // = null;
/* Usually when reaching EAGAIN, I would do
Thread.Sleep(1);
continue; /**/
}
if (error == ZError.ETERM)
Expand Down
6 changes: 2 additions & 4 deletions examples/C#/interrupt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,15 @@ public static void Interrupt(IDictionary<string, string> dict, string[] args)
{
context.Shutdown();
}
} /**/
}

if (null == (request = responder.ReceiveFrame(ZSocketFlags.DontWait, out error)))
{
if (error == ZError.EAGAIN)
{
error = ZError.None;
Thread.Sleep(1);

continue;
} /**/
}
if (error == ZError.ETERM)
break; // Interrupted
throw new ZException(error);
Expand Down
20 changes: 9 additions & 11 deletions examples/C#/mtrelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public static void MTRelay(IDictionary<string, string> dict, string[] args)
//

// Bind inproc socket before starting step2
using (var context = new ZContext())
using (var receiver = new ZSocket(context, ZSocketType.PAIR))
using (var ctx = new ZContext())
using (var receiver = new ZSocket(ctx, ZSocketType.PAIR))
{
receiver.Bind("inproc://step3");

var thread = new Thread(() => MTRelay_step2(context));
thread.Start();
new Thread(() => MTRelay_step2(ctx)).Start();

// Wait for signal
receiver.ReceiveFrame();
Expand All @@ -34,22 +33,21 @@ public static void MTRelay(IDictionary<string, string> dict, string[] args)
}
}

static void MTRelay_step2(ZContext context)
static void MTRelay_step2(ZContext ctx)
{
// Bind inproc socket before starting step1
using (var receiver = new ZSocket(context, ZSocketType.PAIR))
using (var receiver = new ZSocket(ctx, ZSocketType.PAIR))
{
receiver.Bind("inproc://step2");

var thread = new Thread(() => MTRelay_step1(context));
thread.Start();
new Thread(() => MTRelay_step1(ctx)).Start();

// Wait for signal and pass it on
receiver.ReceiveFrame();
}

// Connect to step3 and tell it we're ready
using (var xmitter = new ZSocket(context, ZSocketType.PAIR))
using (var xmitter = new ZSocket(ctx, ZSocketType.PAIR))
{
xmitter.Connect("inproc://step3");

Expand All @@ -58,10 +56,10 @@ static void MTRelay_step2(ZContext context)
}
}

static void MTRelay_step1(ZContext context)
static void MTRelay_step1(ZContext ctx)
{
// Connect to step2 and tell it we're ready
using (var xmitter = new ZSocket(context, ZSocketType.PAIR))
using (var xmitter = new ZSocket(ctx, ZSocketType.PAIR))
{
xmitter.Connect("inproc://step2");

Expand Down
4 changes: 2 additions & 2 deletions examples/C#/mtserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public static void MTServer(IDictionary<string, string> dict, string[] args)
}
}

static void MTServer_Worker(ZContext context)
static void MTServer_Worker(ZContext ctx)
{
// Socket to talk to dispatcher
using (var server = new ZSocket(context, ZSocketType.REP))
using (var server = new ZSocket(ctx, ZSocketType.REP))
{
server.Connect("inproc://workers");

Expand Down
2 changes: 0 additions & 2 deletions examples/C#/peering1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public static void Peering1(IDictionary<string, string> dict, string[] args)
{
if (error == ZError.EAGAIN)
{
error = ZError.None;

using (var output = new ZMessage())
{
output.Add(new ZFrame(self));
Expand Down
6 changes: 3 additions & 3 deletions examples/C#/rrbroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static void RRBroker(IDictionary<string, string> dict, string[] args)
//

// Prepare our context and sockets
using (var context = new ZContext())
using (var frontend = new ZSocket(context, ZSocketType.ROUTER))
using (var backend = new ZSocket(context, ZSocketType.DEALER))
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");
Expand Down
15 changes: 7 additions & 8 deletions examples/C#/rtdealer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public static void RTDealer(IDictionary<string, string> dict, string[] args)

for (int i = 0; i < RTDealer_Workers; ++i)
{
int j = i;
new Thread(() => RTDealer_Worker(j)).Start();
int j = i; new Thread(() => RTDealer_Worker(j)).Start();
}

var stopwatch = new Stopwatch();
Expand Down Expand Up @@ -84,14 +83,14 @@ static void RTDealer_Worker(int i)
worker.Send(new ZFrame("Hi Boss"));

// Get workload from broker, until finished
bool finished;
using (ZMessage msg = worker.ReceiveMessage())
{
finished = (msg[2].ReadString() == "Fired!");
}
if (finished)
{
break;
bool finished = (msg[2].ReadString() == "Fired!");

if (finished)
{
break;
}
}

total++;
Expand Down
12 changes: 6 additions & 6 deletions examples/C#/rtreq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ static void RTReq_Worker(int i)
worker.Send(new ZFrame("Hi Boss"));

// Get workload from broker, until finished
bool finished;
using (ZFrame frame = worker.ReceiveFrame())
{
finished = (frame.ReadString() == "Fired!");
}
if (finished)
{
break;
bool finished = (frame.ReadString() == "Fired!");
if (finished)
{
break;
}
}

total++;

// Do some random work
Expand Down
2 changes: 0 additions & 2 deletions examples/C#/suisnail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ static void SuiSnail_Publisher(ZContext context, ZSocket backend, CancellationTo
{
if (error == ZError.EAGAIN)
{
error = ZError.None;
Thread.Sleep(1); // wait 1 ms

continue;
}
if (error == ZError.ETERM)
Expand Down

0 comments on commit bec72b8

Please sign in to comment.