2021-02-01 17:59:21 -08:00
ZeroTier Sockets for C# .NET
2021-01-04 21:03:57 -08:00
=====
2021-02-01 17:59:21 -08:00
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.
2021-01-04 21:03:57 -08:00
2021-02-01 17:59:21 -08:00
# Overview
2021-01-04 21:03:57 -08:00
2021-02-01 17:59:21 -08:00
Add `ZeroTier.Sockets` to your project:
```powershell
Install-Package ZeroTier.Sockets
2021-01-04 21:03:57 -08:00
```
2021-02-01 17:59:21 -08:00
See [example.cs ](./example.cs ) for complete client/server app implementation.
```csharp
2021-01-04 21:03:57 -08:00
using System.Net.Sockets;
using ZeroTier;
2021-02-01 17:59:21 -08:00
void OnZeroTierEvent(ZeroTier.Event e)
2021-01-04 21:03:57 -08:00
{
Console.WriteLine("{0} ({1})", e.EventCode, e.EventName);
}
...
2021-02-01 17:59:21 -08:00
ZeroTier.Node node = new ZeroTier.Node("path", OnZeroTierEvent, 9991);
2021-01-04 21:03:57 -08:00
node.Start();
node.Join(0xc287ac0b42a6fb4c);
...
ZeroTier.Socket sock = new ZeroTier.Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(remoteEndPoint);
...
node.Stop();
```
2021-02-01 17:59:21 -08:00
# Building example without NuGet package (Advanced)
2021-01-04 21:03:57 -08:00
2021-02-01 17:59:21 -08:00
From top-level repo directory, build `libzt.dll/so/dylib` :
```bash
make host_pinvoke_release
2021-01-04 21:03:57 -08:00
```
2021-02-01 17:59:21 -08:00
Copy `libzt.dll/so/dylib` into this project directory:
```
cp ../../lib/release/${YOUR_HOST_TUPLE}-pinvoke/libzt.* .
2021-01-04 21:03:57 -08:00
```
2021-02-01 17:59:21 -08:00
Where `${YOUR_HOST_TUPLE}` is something like: `linux-x86_64` , `macOS-x86_64` , etc.
2021-01-04 21:03:57 -08:00
2021-02-01 17:59:21 -08:00
Build language binding layer, `ZeroTier.Sockets.dll` :
2021-01-04 21:03:57 -08:00
2021-02-01 17:59:21 -08:00
```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
```
Where `${CSHARP_COMPILER}` may be `csc` or `mono-csc` depending on your platform.