Restructure packaging directories. Move C# bindings, minor compilation fix.

This commit is contained in:
Joseph Henry
2021-02-01 17:59:21 -08:00
parent 3544eab41d
commit 38ea47212d
16 changed files with 106 additions and 71 deletions

View File

@@ -1,22 +1,28 @@
ZeroTier Sockets for C# .NET (Work In Progress)
ZeroTier Sockets for C# .NET
=====
This library is a re-implementation of the .NET socket class ([System.Net.Sockets.Socket](https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket)) built atop ZeroTier's SDK using P/INVOKE and is designed to be a direct drop-in replacement. The library consists of three main objects: `ZeroTier.Node`, `ZeroTier.Event`, and `ZeroTier.Socket`. No code change is required in your application beyond a small snippet of startup code, renaming `Socket` to `ZeroTier.Socket` (where applicable) and handling a smattering of events.
A replacement for the [System.Net.Sockets.Socket](https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket) class built atop ZeroTier's SDK using P/INVOKE. It is designed to be a direct drop-in replacement. The library consists of three main objects: `ZeroTier.Node`, `ZeroTier.Event`, and `ZeroTier.Socket`. No code change is required in your application beyond a small snippet of startup code, renaming `Socket` to `ZeroTier.Socket` (where applicable) and handling a smattering of events.
# Overview
tl;dr:
Add `ZeroTier.Sockets` to your project:
```powershell
Install-Package ZeroTier.Sockets
```
See [example.cs](./example.cs) for complete client/server app implementation.
```csharp
using System.Net.Sockets;
using ZeroTier;
void myCallback(ZeroTier.Event e)
void OnZeroTierEvent(ZeroTier.Event e)
{
Console.WriteLine("{0} ({1})", e.EventCode, e.EventName);
}
...
ZeroTier.Node node = new ZeroTier.Node("path", myCallback, 9991);
ZeroTier.Node node = new ZeroTier.Node("path", OnZeroTierEvent, 9991);
node.Start();
node.Join(0xc287ac0b42a6fb4c);
@@ -31,15 +37,28 @@ sock.Connect(remoteEndPoint);
node.Stop();
```
See [example.cs](./example.cs) for a complete client/server implementation.
## Building and running the example
# Building example without NuGet package (Advanced)
From top-level repo directory, build `libzt.dll/so/dylib`:
```bash
make host_pinvoke_release
```
Copy `libzt.dll/so/dylib` into this project directory:
```
make debug|release
cp ../../lib/release/${YOUR_HOST_TUPLE}-pinvoke/libzt.* .
```
Where `${YOUR_HOST_TUPLE}` is something like: `linux-x86_64`, `macOS-x86_64`, etc.
Build language binding layer, `ZeroTier.Sockets.dll`:
```bash
cd examples/csharp
${CSHARP_COMPILER} -target:library -out:ZeroTier.Sockets.dll ../../src/bindings/csharp/*.cs
${CSHARP_COMPILER} -reference:ZeroTier.Sockets.dll example.cs
./example.exe
```
## Development notes
The SWIG interface file `zt.i` is only present for historical reference purposes. SWIG generates a ton of unnecessary boilerplate code which is hard to completely prevent using hints. You can generate a new wrapper for yourself using `swig -c++ -csharp -dllimport "./libzt.so" zt.i` but I would not recommend doing so unless you know what you're in for.
Where `${CSHARP_COMPILER}` may be `csc` or `mono-csc` depending on your platform.