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 16, 2015
1 parent b97d0b4 commit b3318a9
Show file tree
Hide file tree
Showing 85 changed files with 1,689 additions and 3,601 deletions.
6 changes: 1 addition & 5 deletions examples/C#/.gitignore
@@ -1,10 +1,6 @@
packages/
packages/repositories.config
packages.config
bin/
obj/
Properties/
*.csproj
*.sln
*.suo
*.user
*.user
27 changes: 27 additions & 0 deletions examples/C#/AssemblyInfo.cs
@@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.

[assembly: AssemblyTitle("ZGuideExamples")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("metadings")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

38 changes: 38 additions & 0 deletions examples/C#/HWClient.cs
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using ZeroMQ;

namespace ZeroMQ.Test
{
static partial class Program
{
public static void HWClient(IDictionary<string, string> dict, string[] args)
{
using (var context = ZContext.Create())
using (var requester = ZSocket.Create(context, ZSocketType.REQ)) {

requester.Connect("tcp://127.0.0.1:5555");

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

string requestText = "Hello";

Console.Write("Sending {0}... ", requestText);
using (var request = ZFrame.Create(requestText))
{
requester.SendFrame(request);
}

using (ZFrame reply = requester.ReceiveFrame())
{
Console.WriteLine("Received: {0} {1}!", requestText, reply.ReadString());
}
}
}
}
}
}
36 changes: 36 additions & 0 deletions examples/C#/HWServer.cs
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using ZeroMQ;

namespace ZeroMQ.Test
{
static partial class Program
{
public static void HWServer(IDictionary<string, string> dict, string[] args)
{
using (var context = ZContext.Create())
using (var responder = ZSocket.Create(context, ZSocketType.REP)) {

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

while (true) {

using (ZFrame request = responder.ReceiveFrame())
{
Console.WriteLine("Received {0}", request);
Thread.Sleep(1);

using (ZFrame reply = ZFrame.Create("World"))
{
responder.SendFrame(reply);
}
}
}
}
}
}
}
45 changes: 45 additions & 0 deletions examples/C#/Identity.cs
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using ZeroMQ;

namespace ZeroMQ.Test
{
static partial class Program
{
public static void Identity(IDictionary<string, string> dict, string[] args)
{
using (var context = ZContext.Create())
using (var sink = ZSocket.Create(context, ZSocketType.ROUTER)) {

sink.Bind("inproc://example");

using (var anonymous = ZSocket.Create(context, ZSocketType.REQ)) {
anonymous.Connect("inproc://example");
anonymous.SendFrame(ZFrame.Create("ROUTER uses REQ's generated UUID"));
}
Identity_Dump(sink);

using (var identified = ZSocket.Create(context, ZSocketType.REQ)) {
identified.Identity = Encoding.UTF8.GetBytes("PEER2");
identified.Connect("inproc://example");
identified.SendFrame(ZFrame.Create("ROUTER uses REQ's socket identity"));
}
Identity_Dump(sink);
}
}

static void Identity_Dump(ZSocket sink)
{
using (ZMessage msg = sink.ReceiveMessage()) {
Console.WriteLine("---");
Console.WriteLine("[0] {0}", msg[0].ReadString());
Console.WriteLine("[1] {0}", msg[1].ReadString());
Console.WriteLine("[2] {0}", msg[2].ReadString());
}
}
}
}
69 changes: 69 additions & 0 deletions examples/C#/Interrupt.cs
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using ZeroMQ;

namespace ZeroMQ.Test
{
static partial class Program
{
public static void Interrupt(IDictionary<string, string> dict, string[] args)
{
using (var context = ZContext.Create())
using (var responder = ZSocket.Create(context, ZSocketType.REP)) {

var thread = new Thread(() => {
Console.CancelKeyPress += (sender, e) => {
// e.Cancel = false;
context.Terminate();
};
while (true) {
if (Console.KeyAvailable) {
ConsoleKeyInfo info = Console.ReadKey(true);
if (info.Modifiers == ConsoleModifiers.Control && info.Key == ConsoleKey.C) {
break;
}
if (info.Key == ConsoleKey.Escape) {
context.Terminate();
break;
}
}
Thread.Sleep(1);
}
});
thread.Start();
thread.Join(64);


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

ZError error;
ZFrame request;
while (true) {

if (null == (request = responder.ReceiveFrame(out error)))
{
if (error == ZError.ETERM) {
Console.WriteLine("Terminating, you have pressed ESC.");
break;
}
throw new ZException(error);
}

using (request) {
string respondText = "Hello";
Console.WriteLine("Received: {0}!", respondText, request.ReadString());

Console.Write("Sending {0}... ", respondText);
using (var response = ZFrame.Create(respondText)) {
responder.SendFrame(response);
}
}
}
}
}
}
}

0 comments on commit b3318a9

Please sign in to comment.