Giter VIP home page Giter VIP logo

sharpnng's Introduction

SharpNng Build Status Build Status NuGet

SharpNng is a lightweight low-level managed wrapper around NNG a Lightweight Messaging Library.

NOTE: This project is archived as I had no use in the end about it. The native version of NNG used by SharpNng is 1.5.2

Features

  • Strict mapping with the C API
  • Pure DllImport library via using static nng;
  • Compatible with netstandard2.0 and netstandard2.1+
  • Fast interop with Span friendly API.

Usage

  • Install the SharpNng NuGet Package to your project.
using static nng;
 // port of https://nanomsg.org/gettingstarted/nng/reqrep.html
string ipcName = $"ipc:///tmp/SharpNng_{Guid.NewGuid():N}.ipc";

var sync = new EventWaitHandle(false, EventResetMode.ManualReset);

void Node0()
{
    nng_socket sock = default;

    int result = nng_rep0_open(ref sock);
    nng_assert(result);
    try
    {
        nng_listener listener = default;
        result = nng_listen(sock, ipcName, ref listener, 0);
        nng_assert(result);

        IntPtr buf;
        size_t sz = default;

        TestContext.Out.WriteLine("Server: Listening");

        sync.Set();

        unsafe
        {
            result = nng_recv(sock, new IntPtr(&buf), ref sz, NNG_FLAG_ALLOC);
            nng_assert(result);
        }

        Assert.AreEqual(4, sz.Value.ToInt64());

        nng_free(buf, sz);
    }
    finally
    {
        result = nng_close(sock);
    }
};

void Node1()
{
    TestContext.Out.WriteLine("Client: Started");

    nng_socket sock = default;

    int result = nng_req0_open(ref sock);
    nng_assert(result);

    try
    {
        nng_dialer dialer = default;
        result = nng_dial(sock, ipcName, ref dialer, 0);
        nng_assert(result);

        TestContext.Out.WriteLine("Client: Connected");

        unsafe
        {
            int value = 0x6afedead;
            result = nng_send(sock, new IntPtr(&value), 4, 0);
            nng_assert(result);
        }
    }
    finally
    {
        result = nng_close(sock);
    }
};

// Start the server
var thread = new Thread(Node0)
{
    IsBackground = true
};
thread.Start();

// Wait for the server to start
sync.WaitOne(1000);

// Run the client
Node1();

Platforms

SharpNng is supported on the following platforms:

  • win-x64, win-x86, win-arm64, win-arm
  • linux-x64, linux-arm64, linux-arm
  • osx-x64, osx-arm64

Note that the Linux version might probably only work on debian derivatives for now...

How to Build?

You need to install the .NET 6 SDK. Then from the root folder:

$ dotnet build src -c Release

In order to rebuild the native binaries, you need to run the build scripts from ext

License

This software is released under the BSD-Clause 2 license.

Author

Alexandre Mutel aka xoofx.

sharpnng's People

Contributors

xoofx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sharpnng's Issues

Question: Problems implementing a pub-sub protocol

Hi, I'm not sure if this project is maintained but here goes.

Im tasked with implementing the client side of a pub-sub type protocol using NNG to interface with CPP applications through a C# .NET6 app. I chose SharpNng to help me with that. I can do REQ et REP type communication with no major issues, using your exemples as a base.

I tried implementing the pub-sub according to the getting-started guides I could find :
https://nanomsg.org/gettingstarted/nng/pubsub.html

I came up with the following :

internal async Task SubscribeAsync(string host, int port, string subject, DataReceivedCallback callBackFunc, CancellationToken token)
        {
            var url = $"tcp://{host}:{port}";
            int result = 0;

            Trace.WriteLine($"Sub: Starting sub socket for: {url}");

            nng_socket sock1 = default;
            try
            {
                result = nng_sub0_open(ref sock1);
                nng_assert(result);

                unsafe
                {
                    result = nng_socket_set(sock1, "NNG_OPT_SUB_SUBSCRIBE", new IntPtr(&subject), subject.Length);
                    nng_assert(result);
                }

                nng_dialer dialer = default;
                result = nng_dial(sock1, url, ref dialer, 0);
                nng_assert(result);

                Trace.WriteLine($"Sub: Dialed-in starting monitorin loop");

                while (!token.IsCancellationRequested)
                {
                    var response = this.Receive(sock1);

                    Trace.WriteLine($"Sub, response received: {response}");

                    if (callBackFunc != null)
                        callBackFunc.Invoke(response);
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine($"Sub: Exception: {e}");
            }
            finally
            {
                result = nng_close(sock1);
            }

            return;
        }

I keep getting a NNG_ENOTSUP((9) when trying to set the NNG_OPT_SUB_SUBSCRIBE option.

I tried several things:

  • using socket_set_str instead of socket_set
  • using the dialer_set methods
  • setting the option before open, after open, etc
  • manually adding a \0 character to my string

I've now ran out of ideas, any pointers that could help me would be appreciated. This is my first time playing with NNG so it might be something obvious I missed, but the docs did not help me so far.

Thanks,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.