Re-work of thread model

This commit is contained in:
Joseph Henry
2019-02-06 22:00:39 -08:00
parent 292fcdda2c
commit 2fdcf025e1
138 changed files with 7567 additions and 15053 deletions

View File

@@ -1,25 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleWindowsCSharpApp", "ExampleWindowsCSharpApp\ExampleWindowsCSharpApp.csproj", "{74F548E3-64FD-41FF-9416-0AE1FC24E8BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{74F548E3-64FD-41FF-9416-0AE1FC24E8BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74F548E3-64FD-41FF-9416-0AE1FC24E8BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74F548E3-64FD-41FF-9416-0AE1FC24E8BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74F548E3-64FD-41FF-9416-0AE1FC24E8BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BC236939-C661-4FEB-8935-4A61C2ECEC19}
EndGlobalSection
EndGlobal

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

View File

@@ -1,54 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{74F548E3-64FD-41FF-9416-0AE1FC24E8BE}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ExampleWindowsCSharpApp</RootNamespace>
<AssemblyName>ExampleWindowsCSharpApp</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="libzt.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -1,118 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using ZeroTier;
namespace ExampleWindowsCSharpApp
{
class Program
{
static void Main(string[] args)
{
ulong nwid = 0xe4da7455b2b9ee6a;
Console.Write("waiting for libzt to come online...\n");
libzt.zts_startjoin("config_path", nwid);
Console.Write("I am " + libzt.zts_get_node_id().ToString("X") +"\n");
Console.Write("ZT ready\n");
int fd;
int err = 0;
bool clientMode = false;
if ((fd = libzt.zts_socket(2, 1, 0)) < 0)
{
Console.Write("error creating socket\n");
}
// CLIENT
if (clientMode == true)
{
string remoteAddrStr = "172.28.221.116";
Int16 remotePort = 2323;
// Convert managed address object to pointer for unmanaged code
libzt.SockAddrIn addr = new libzt.SockAddrIn();
addr.Family = (byte)libzt.SockAddrFamily.Inet;
addr.Port = IPAddress.HostToNetworkOrder((Int16)remotePort);
addr.Addr = (uint)IPAddress.Parse(remoteAddrStr).Address;
IntPtr addrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(libzt.SockAddrIn)));
Marshal.StructureToPtr(addr, addrPtr, false);
int addrlen = Marshal.SizeOf(addr);
if ((err = libzt.zts_connect(fd, addrPtr, addrlen)) < 0)
{
Console.Write("error connecting to remote server\n");
}
// Send message
string msgStr = "Hello from C#!";
byte[] msgBuf = Encoding.ASCII.GetBytes(msgStr);
int buflen = System.Text.ASCIIEncoding.ASCII.GetByteCount(msgStr);
if ((err = libzt.zts_write(fd, msgBuf, buflen)) < 0)
{
Console.Write("error writing to remote server\n");
}
libzt.zts_close(fd);
Marshal.FreeHGlobal(addrPtr);
}
else // SERVER
{
string localAddrStr = "0.0.0.0";
Int16 bindPort = 5050;
libzt.SockAddrIn addr = new libzt.SockAddrIn();
addr.Family = (byte)libzt.SockAddrFamily.Inet;
addr.Port = IPAddress.HostToNetworkOrder((Int16)bindPort);
addr.Addr = (uint)IPAddress.Parse(localAddrStr).Address;
IntPtr addrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(libzt.SockAddrIn)));
Marshal.StructureToPtr(addr, addrPtr, false);
int addrlen = Marshal.SizeOf(addr);
if ((err = libzt.zts_bind(fd, addrPtr, addrlen)) < 0)
{
Console.Write("error binding to local port\n");
}
if ((err = libzt.zts_listen(fd, 1)) < 0)
{
Console.Write("error putting socket in listening state\n");
}
int accfd;
Console.Write("waiting to accept connection...\n");
if ((accfd = libzt.zts_accept(fd, IntPtr.Zero, IntPtr.Zero)) < 0)
{
Console.Write("error accepting incoming connection\n");
}
Console.Write("accepted connection!\n");
// Read message
byte[] msgBuf = new byte[32];
int buflen = 32;
Console.Write("reading from client...\n");
if ((err = libzt.zts_read(accfd, msgBuf, buflen)) < 0)
{
Console.Write("error reading from remote client\n");
}
Console.Write("read " + err + " bytes from client\n");
string msgStr = System.Text.Encoding.UTF8.GetString(msgBuf, 0, msgBuf.Length);
Console.Write("msg from client = " + msgStr + "\n");
libzt.zts_close(fd);
libzt.zts_close(accfd);
Marshal.FreeHGlobal(addrPtr);
}
Console.Write("fd=" + fd);
Console.Write("wrote=" + err);
libzt.zts_stop();
}
}
}

View File

@@ -1,36 +0,0 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ExampleWindowsCSharpApp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ExampleWindowsCSharpApp")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("74f548e3-64fd-41ff-9416-0ae1fc24e8be")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -1,184 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ZeroTier
{
static class libzt
{
public enum SockAddrFamily
{
Inet = 2,
Inet6 = 10
}
[StructLayout(LayoutKind.Sequential)]
public struct SockAddr
{
public ushort Family;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
public byte[] Data;
};
[StructLayout(LayoutKind.Sequential)]
public struct SockAddrIn
{
byte len; // unique to lwIP
public byte Family;
public Int16 Port;
public uint Addr;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] Zero;
}
[StructLayout(LayoutKind.Sequential)]
public struct SockAddrIn6
{
public ushort Family;
public ushort Port;
public uint FlowInfo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] Addr;
public uint ScopeId;
};
/****************************************************************************/
/* ZeroTier Service Controls */
/****************************************************************************/
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_start(string path);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_startjoin(string path, ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void zts_stop();
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void zts_join(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void zts_leave(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void zts_get_homepath(string homePath, int len);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int64 zts_get_node_id();
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_running();
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_has_address(ulong nwid);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_get_address(ulong nwid, IntPtr addr, int address_family);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void zts_get_6plane_addr(IntPtr addr, string nwid, string nodeId);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void zts_get_rfc4193_addr(IntPtr addr, string nwid, string nodeId);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern long zts_get_peer_count();
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_get_peer_address(string peer, ulong nodeId);
/****************************************************************************/
/* Socket-like API */
/****************************************************************************/
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_socket(int socket_family, int socket_type, int protocol);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_connect(int fd, IntPtr addr, int addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_bind(int fd, IntPtr addr, int addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_listen(int fd, int backlog);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_accept(int fd, IntPtr addr, IntPtr addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_setsockopt(int fd, int level, int optname, IntPtr optval, int optlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_getsockopt(int fd, int level, int optname, IntPtr optval, IntPtr optlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_getsockname(int fd, IntPtr addr, IntPtr addrlen);
/*
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_getpeername(int fd, System.IntPtr addr, IntPtr addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_gethostname(string name, int len);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_sethostname(string name, int len);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern IntPtr *zts_gethostbyname(string name);
*/
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_close(int fd);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_select(int nfds, IntPtr readfds, IntPtr writefds, IntPtr exceptfds, IntPtr timeout);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_fcntl(int fd, int cmd, int flags);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_ioctl(int fd, ulong request, IntPtr argp);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_send(int fd, byte[] buf, int len, int flags);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_sendto(int fd, byte[] buf, int len, int flags, IntPtr addr, int addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_sendmsg(int fd, byte[] msg, int flags);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_recv(int fd, byte[] buf, int len, int flags);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_recvfrom(int fd, byte[] buf, int len, int flags, IntPtr addr, IntPtr addrlen);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_recvmsg(int fd, byte[] msg, int flags);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_read(int fd, byte[] buf, int len);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_write(int fd, byte[] buf, int len);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_shutdown(int fd, int how);
/*
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_add_dns_nameserver(System.IntPtr addr);
[DllImport("libzt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int zts_del_dns_nameserver(System.IntPtr addr);
*/
}
}