Skip to content

Commit

Permalink
Feature/master examples (#51)
Browse files Browse the repository at this point in the history
* Initial examples/nngcat/

- C# version of https://nanomsg.github.io/nng/man/v1.1.0/nngcat.1

* Mention examples/ in README
  • Loading branch information
jake-ruyi authored Feb 26, 2019
1 parent 2fa797c commit 1bf8f07
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var factory = nng.NngLoadContext.Init(ctx);
// Use factory...
```

See [`tests/`](https://github.com/subor/nng.NETCore/tree/master/tests) for basic examples.
See [`tests/`](https://github.com/subor/nng.NETCore/tree/master/tests) and [`examples/`](https://github.com/subor/nng.NETCore/tree/master/examples) for usage examples.

## Build & Run

Expand Down
85 changes: 85 additions & 0 deletions examples/nngcat/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/// <summary>
/// C# version of nngcat.
/// https://nanomsg.github.io/nng/man/v1.1.0/nngcat.1
/// </summary>
/// <example>
/// <code>
/// dotnet run -- --rep --listen tcp://localhost:8080 --data "42" &
/// dotnet run -- --req --dial tcp://localhost:8080 --data "what"
/// </code>
/// </example>
using Microsoft.Extensions.CommandLineUtils;
using nng;
using System;
using System.IO;
using System.Text;

namespace nngcat
{
class Program
{
static void Main(string[] args)
{
var app = new CommandLineApplication();
app.Name = "nngcat";
app.HelpOption("-? | -h | --help");
var repOpt = app.Option("--rep", "Rep protocol", CommandOptionType.NoValue);
var reqOpt = app.Option("--req", "Req protocol", CommandOptionType.NoValue);
var listenOpt = app.Option("--bind | --listen <URL>", "Connect to the peer at the address specified by URL.", CommandOptionType.SingleValue);
var dialOpt = app.Option("--connect | --dial <URL>", "Bind to, and accept connections from peers, at the address specified by URL.", CommandOptionType.SingleValue);
// Receive options
var quotedOpt = app.Option("-Q | --quoted", "Currently always enabled", CommandOptionType.NoValue);
// Transmit options
var dataOpt = app.Option("-D | --data <DATA>", "Use DATA for the body of outgoing messages.", CommandOptionType.SingleValue);

app.OnExecute(() => {
var path = Path.GetDirectoryName(typeof(Program).Assembly.Location);
var ctx = new nng.NngLoadContext(path);
var factory = nng.NngLoadContext.Init(ctx);

if (repOpt.HasValue())
{
using (var socket = factory.ReplierOpen().listenOrDial(listenOpt, dialOpt))
{
var request = socket.RecvMsg().Unwrap();
var str = Encoding.ASCII.GetString(request.AsSpan());
Console.WriteLine(str);
var reply = factory.CreateMessage();
var replyBytes = Encoding.ASCII.GetBytes(dataOpt.Value());
reply.Append(replyBytes);
socket.SendMsg(reply);
}
} else if (reqOpt.HasValue())
{
using (var socket = factory.RequesterOpen().listenOrDial(listenOpt, dialOpt))
{
var request = factory.CreateMessage();
var requestBytes = Encoding.ASCII.GetBytes(dataOpt.Value());
request.Append(requestBytes);
socket.SendMsg(request).Unwrap();
var reply = socket.RecvMsg().Unwrap();
var str = Encoding.ASCII.GetString(reply.AsSpan());
Console.WriteLine('"' + str + '"');
}
}
return 0;
});

app.Execute(args);
}
}

static class SocketExt
{
public static T listenOrDial<T>(this NngResult<T> socket, CommandOption listenOpt, CommandOption dialOpt)
where T: ISocket
{
if (listenOpt.HasValue())
return socket.ThenListen(listenOpt.Value()).Unwrap();
else if (dialOpt.HasValue())
return socket.ThenDial(dialOpt.Value()).Unwrap();
else
throw new ArgumentException("Must --listen or --dial");
}
}
}
13 changes: 13 additions & 0 deletions examples/nngcat/nngcat.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<PackageReference Include="Subor.nng.NETCore" Version="1.1.1" />
</ItemGroup>

</Project>

0 comments on commit 1bf8f07

Please sign in to comment.