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.

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.