Skip to content

Commit

Permalink
Merge pull request booksbyus#656 from metadings/master
Browse files Browse the repository at this point in the history
Updating C# Examples
  • Loading branch information
hintjens authored Aug 11, 2016
2 parents fb9b7ac + 80cb955 commit dafe237
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/C#/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ Use with LPClient.
#### [Tripping](https://github.com/metadings/zguide/blob/master/examples/C%23/tripping.cs)

```
Usage: ./ZGuideExamples.exe Tripping
Usage: ./ZGuideExamples.exe [--verbose] Tripping
```

#### [MMIEcho](https://github.com/metadings/zguide/blob/master/examples/C%23/mmiecho.cs)
Expand Down
28 changes: 12 additions & 16 deletions examples/C#/mspoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,24 @@ public static void MSPoller(string[] args)
subscriber.Connect("tcp://127.0.0.1:5556");
subscriber.SetOption(ZSocketOption.SUBSCRIBE, "10001 ");

var poll = ZPollItem.CreateReceiver();
var sockets = new ZSocket[] { receiver, subscriber };
var polls = new ZPollItem[] { ZPollItem.CreateReceiver(), ZPollItem.CreateReceiver() };

// Process messages from both sockets
ZError error;
ZMessage msg;
ZMessage[] msg;
while (true)
{
if (receiver.PollIn(poll, out msg, out error, TimeSpan.FromMilliseconds(64)))
if (sockets.PollIn(polls, out msg, out error, TimeSpan.FromMilliseconds(64)))
{
// Process task
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}

if (subscriber.PollIn(poll, out msg, out error, TimeSpan.FromMilliseconds(64)))
{
// Process weather update
if (msg[0] != null)
{
// Process task
}
if (msg[1] != null)
{
// Process weather update
}
}
else
{
Expand Down
46 changes: 28 additions & 18 deletions examples/C#/msreader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,38 @@ public static void MSReader(string[] args)
ZFrame frame;
while (true)
{
if (null != (frame = receiver.ReceiveFrame(ZSocketFlags.DontWait, out error)))
while (true)
{
// Process task
}
else
{
if (error == ZError.ETERM)
return; // Interrupted
if (error != ZError.EAGAIN)
throw new ZException(error);
}
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
break;
}
}
else

while (true)
{
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);

break;
}
}

// No activity, so sleep for 1 msec
Expand Down
5 changes: 2 additions & 3 deletions examples/C#/tripping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ static void Tripping_ClientTask(ZContext ctx, ZSocket pipe, CancellationTokenSou

"Asynchronous round-trip test...".DumpString();
sw.Restart();
// sending 100000 requests => often ends in eagain exception in ZContext.Proxy!!
for (requests = 0; requests < 1000; requests++)
for (requests = 0; requests < 100000; requests++)
using (var outgoing = new ZFrame("hello"))
client.SendFrame(outgoing);

for (requests = 0; requests < 1000; requests++)
for (requests = 0; requests < 100000; requests++)
using (var reply = client.ReceiveFrame())
if (Verbose)
reply.ToString().DumpString();
Expand Down

0 comments on commit dafe237

Please sign in to comment.