Unity3D .NET API and documentation update
This commit is contained in:
@@ -62,9 +62,8 @@
|
||||
<Compile Include="Assets\OBJ-IO\Plugins\Mesh\OBJ\OBJMaterial.cs" />
|
||||
<Compile Include="Assets\OBJ-IO\Plugins\Utils\Int32Converter.cs" />
|
||||
<Compile Include="Assets\WorldMain.cs" />
|
||||
<Compile Include="Assets\ZeroTierNetworkInterface.cs" />
|
||||
<Compile Include="Assets\ZTSDK.cs" />
|
||||
<Compile Include="Assets\ZeroTierSockets_Demo.cs" />
|
||||
<Compile Include="Assets\ZeroTierUtils.cs" />
|
||||
<None Include="Assets\OBJ-IO\Readme.txt" />
|
||||
<Reference Include="UnityEngine.Networking">
|
||||
<HintPath>/Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/Networking/UnityEngine.Networking.dll</HintPath>
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>BuildMachineOSBuild</key>
|
||||
<string>15F34</string>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>ZeroTierSDK_Unity3D_OSX</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>zerotier.ZeroTierSDK-Unity3D-OSX</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>ZeroTierSDK_Unity3D_OSX</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>BNDL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleSupportedPlatforms</key>
|
||||
<array>
|
||||
<string>MacOSX</string>
|
||||
</array>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>DTCompiler</key>
|
||||
<string>com.apple.compilers.llvm.clang.1_0</string>
|
||||
<key>DTPlatformBuild</key>
|
||||
<string>7D1014</string>
|
||||
<key>DTPlatformVersion</key>
|
||||
<string>GM</string>
|
||||
<key>DTSDKBuild</key>
|
||||
<string>15E60</string>
|
||||
<key>DTSDKName</key>
|
||||
<string>macosx10.11</string>
|
||||
<key>DTXcode</key>
|
||||
<string>0731</string>
|
||||
<key>DTXcodeBuild</key>
|
||||
<string>7D1014</string>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string>Copyright © 2016 ZeroTier Inc. All rights reserved.</string>
|
||||
</dict>
|
||||
</plist>
|
||||
Binary file not shown.
@@ -0,0 +1,105 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>files</key>
|
||||
<dict/>
|
||||
<key>files2</key>
|
||||
<dict/>
|
||||
<key>rules</key>
|
||||
<dict>
|
||||
<key>^Resources/</key>
|
||||
<true/>
|
||||
<key>^Resources/.*\.lproj/</key>
|
||||
<dict>
|
||||
<key>optional</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>1000</real>
|
||||
</dict>
|
||||
<key>^Resources/.*\.lproj/locversion.plist$</key>
|
||||
<dict>
|
||||
<key>omit</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>1100</real>
|
||||
</dict>
|
||||
<key>^version.plist$</key>
|
||||
<true/>
|
||||
</dict>
|
||||
<key>rules2</key>
|
||||
<dict>
|
||||
<key>.*\.dSYM($|/)</key>
|
||||
<dict>
|
||||
<key>weight</key>
|
||||
<real>11</real>
|
||||
</dict>
|
||||
<key>^(.*/)?\.DS_Store$</key>
|
||||
<dict>
|
||||
<key>omit</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>2000</real>
|
||||
</dict>
|
||||
<key>^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/</key>
|
||||
<dict>
|
||||
<key>nested</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>10</real>
|
||||
</dict>
|
||||
<key>^.*</key>
|
||||
<true/>
|
||||
<key>^Info\.plist$</key>
|
||||
<dict>
|
||||
<key>omit</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>20</real>
|
||||
</dict>
|
||||
<key>^PkgInfo$</key>
|
||||
<dict>
|
||||
<key>omit</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>20</real>
|
||||
</dict>
|
||||
<key>^Resources/</key>
|
||||
<dict>
|
||||
<key>weight</key>
|
||||
<real>20</real>
|
||||
</dict>
|
||||
<key>^Resources/.*\.lproj/</key>
|
||||
<dict>
|
||||
<key>optional</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>1000</real>
|
||||
</dict>
|
||||
<key>^Resources/.*\.lproj/locversion.plist$</key>
|
||||
<dict>
|
||||
<key>omit</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>1100</real>
|
||||
</dict>
|
||||
<key>^[^/]+$</key>
|
||||
<dict>
|
||||
<key>nested</key>
|
||||
<true/>
|
||||
<key>weight</key>
|
||||
<real>10</real>
|
||||
</dict>
|
||||
<key>^embedded\.provisionprofile$</key>
|
||||
<dict>
|
||||
<key>weight</key>
|
||||
<real>20</real>
|
||||
</dict>
|
||||
<key>^version\.plist$</key>
|
||||
<dict>
|
||||
<key>weight</key>
|
||||
<real>20</real>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -37,6 +37,7 @@ using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
// TODO:
|
||||
/*
|
||||
@@ -49,7 +50,7 @@ using System.Collections.Generic;
|
||||
* */
|
||||
|
||||
// Provides a bare-bones interface to ZeroTier-administered sockets
|
||||
public class ZeroTierNetworkInterface {
|
||||
public class ZTSDK {
|
||||
|
||||
// ZeroTier background thread
|
||||
protected Thread ztThread;
|
||||
@@ -91,47 +92,45 @@ public class ZeroTierNetworkInterface {
|
||||
private static extern int unity_start_service(string path);
|
||||
[DllImport (DLL_PATH)]
|
||||
private static extern int unity_start_service_and_rpc(string path, string nwid);
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern bool zts_is_running();
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern void zts_stop_service();
|
||||
|
||||
// Connection calls
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern int zt_socket(int family, int type, int protocol);
|
||||
protected static extern int zts_socket(int family, int type, int protocol);
|
||||
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_bind(int sockfd, System.IntPtr addr, int addrlen);
|
||||
unsafe protected static extern int zts_bind(int sockfd, System.IntPtr addr, int addrlen);
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_connect(int sockfd, System.IntPtr addr, int addrlen);
|
||||
unsafe protected static extern int zts_connect(int sockfd, System.IntPtr addr, int addrlen);
|
||||
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern int zt_accept(int sockfd);
|
||||
protected static extern int zts_accept(int sockfd);
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern int zt_listen(int sockfd, int backlog);
|
||||
protected static extern int zts_listen(int sockfd, int backlog);
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern int zt_close(int sockfd);
|
||||
protected static extern int zts_close(int sockfd);
|
||||
|
||||
// RX / TX
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_recv(int sockfd, [In, Out] IntPtr buf, int len);
|
||||
unsafe protected static extern int zts_recv(int sockfd, [In, Out] IntPtr buf, int len);
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_send(int sockfd, IntPtr buf, int len);
|
||||
unsafe protected static extern int zts_send(int sockfd, IntPtr buf, int len);
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_set_nonblock(int sockfd);
|
||||
unsafe protected static extern int zts_set_nonblock(int sockfd);
|
||||
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_sendto(int fd, IntPtr buf, int len, int flags, System.IntPtr addr, int addrlen);
|
||||
unsafe protected static extern int zts_sendto(int fd, IntPtr buf, int len, int flags, System.IntPtr addr, int addrlen);
|
||||
[DllImport (DLL_PATH)]
|
||||
unsafe protected static extern int zt_recvfrom(int fd, [In, Out] IntPtr buf, int len, int flags, System.IntPtr addr, int addrlen);
|
||||
|
||||
// ZT Thread controls
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern bool zt_is_running();
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern void zt_terminate();
|
||||
unsafe protected static extern int zts_recvfrom(int fd, [In, Out] IntPtr buf, int len, int flags, System.IntPtr addr, int addrlen);
|
||||
|
||||
// ZT Network controls
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern bool zt_join_network(string nwid);
|
||||
protected static extern void zts_join_network(string nwid);
|
||||
[DllImport (DLL_PATH)]
|
||||
protected static extern void zt_leave_network(string nwid);
|
||||
protected static extern void zts_leave_network(string nwid);
|
||||
#endregion
|
||||
|
||||
// Interop structures
|
||||
@@ -208,19 +207,21 @@ public class ZeroTierNetworkInterface {
|
||||
}
|
||||
|
||||
// Initialize the ZeroTier service with a given path
|
||||
public ZeroTierNetworkInterface(string path, string nwid) {
|
||||
public ZTSDK(string path, string nwid) {
|
||||
Debug.Log("ZTSDK(): " + nwid);
|
||||
|
||||
this.path = path;
|
||||
this.nwid = nwid;
|
||||
Init();
|
||||
}
|
||||
|
||||
public ZeroTierNetworkInterface (string path) {
|
||||
public ZTSDK (string path) {
|
||||
this.path = path;
|
||||
Init();
|
||||
}
|
||||
|
||||
// Initialize the ZeroTier service
|
||||
public ZeroTierNetworkInterface() {
|
||||
public ZTSDK() {
|
||||
Init();
|
||||
}
|
||||
|
||||
@@ -229,7 +230,7 @@ public class ZeroTierNetworkInterface {
|
||||
public bool JoinNetwork(string nwid)
|
||||
{
|
||||
if(!joined_to_network) {
|
||||
zt_join_network(nwid);
|
||||
zts_join_network(nwid);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -242,7 +243,7 @@ public class ZeroTierNetworkInterface {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
zt_leave_network(nwid);
|
||||
zts_leave_network(nwid);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -251,28 +252,28 @@ public class ZeroTierNetworkInterface {
|
||||
// Creates a new ZeroTier-administered socket
|
||||
public int Socket(int family, int type, int protocol)
|
||||
{
|
||||
return zt_socket (family, type, protocol);
|
||||
return zts_socket (family, type, protocol);
|
||||
}
|
||||
|
||||
// Binds to a specific address
|
||||
public int Bind(int fd, string addr, int port)
|
||||
{
|
||||
GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
|
||||
int addrlen = Marshal.SizeOf (pSockAddr);
|
||||
return zt_bind (fd, pSockAddr, addrlen);
|
||||
return zts_bind (fd, pSockAddr, addrlen);
|
||||
}
|
||||
|
||||
// Listens for an incoming connection request
|
||||
public int Listen(int fd, int backlog)
|
||||
{
|
||||
return zt_listen(fd, backlog);
|
||||
return zts_listen(fd, backlog);
|
||||
}
|
||||
|
||||
// Accepts an incoming connection
|
||||
public int Accept(int fd)
|
||||
{
|
||||
return zt_accept (fd);
|
||||
return zts_accept (fd);
|
||||
}
|
||||
|
||||
// Closes a connection
|
||||
@@ -284,17 +285,17 @@ public class ZeroTierNetworkInterface {
|
||||
// Connects to a remote host
|
||||
public int Connect(int fd, string addr, int port)
|
||||
{
|
||||
GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
|
||||
int addrlen = Marshal.SizeOf (pSockAddr);
|
||||
return zt_connect (fd, pSockAddr, addrlen);
|
||||
return zts_connect (fd, pSockAddr, addrlen);
|
||||
}
|
||||
|
||||
public int Read(int fd, ref char[] buf, int len)
|
||||
{
|
||||
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
|
||||
IntPtr ptr = handle.AddrOfPinnedObject();
|
||||
int bytes_read = zt_recv (fd, ptr, len*2);
|
||||
int bytes_read = zts_recv (fd, ptr, len*2);
|
||||
string str = Marshal.PtrToStringAuto(ptr);
|
||||
//Marshal.Copy (ptr, buf, 0, bytes_read);
|
||||
buf = Marshal.PtrToStringAnsi(ptr).ToCharArray();
|
||||
@@ -308,7 +309,7 @@ public class ZeroTierNetworkInterface {
|
||||
//error = 0;
|
||||
int bytes_written;
|
||||
// FIXME: Sending a length of 2X the buffer size seems to fix the object pinning issue
|
||||
if((bytes_written = zt_send(fd, ptr, len*2)) < 0) {
|
||||
if((bytes_written = zts_send(fd, ptr, len*2)) < 0) {
|
||||
//error = (byte)bytes_written;
|
||||
}
|
||||
return bytes_written;
|
||||
@@ -322,11 +323,11 @@ public class ZeroTierNetworkInterface {
|
||||
int bytes_written;
|
||||
|
||||
// Form address structure
|
||||
GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
|
||||
int addrlen = Marshal.SizeOf (pSockAddr);
|
||||
|
||||
if((bytes_written = zt_sendto(fd, ptr, len*2, flags, pSockAddr, addrlen)) < 0) {
|
||||
if((bytes_written = zts_sendto(fd, ptr, len*2, flags, pSockAddr, addrlen)) < 0) {
|
||||
//error = (byte)bytes_written;
|
||||
}
|
||||
return bytes_written;
|
||||
@@ -339,11 +340,11 @@ public class ZeroTierNetworkInterface {
|
||||
IntPtr ptr = handle.AddrOfPinnedObject();
|
||||
|
||||
// Form address structure
|
||||
GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
|
||||
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
|
||||
int addrlen = Marshal.SizeOf (pSockAddr);
|
||||
|
||||
int bytes_read = zt_recvfrom(fd, ptr, len*2, flags, pSockAddr, addrlen);
|
||||
int bytes_read = zts_recvfrom(fd, ptr, len*2, flags, pSockAddr, addrlen);
|
||||
string str = Marshal.PtrToStringAuto(ptr);
|
||||
//Marshal.Copy (ptr, buf, 0, bytes_read);
|
||||
buf = Marshal.PtrToStringAnsi(ptr).ToCharArray();
|
||||
@@ -354,13 +355,63 @@ public class ZeroTierNetworkInterface {
|
||||
// Returns whether the ZeroTier service is currently running
|
||||
public bool IsRunning()
|
||||
{
|
||||
return zt_is_running ();
|
||||
return zts_is_running ();
|
||||
}
|
||||
|
||||
// Terminates the ZeroTier service
|
||||
public void Terminate()
|
||||
{
|
||||
zt_terminate ();
|
||||
zts_stop_service ();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
// --- Utilities ---
|
||||
|
||||
|
||||
// Handles IPv4 and IPv6 notation.
|
||||
public static IPEndPoint CreateIPEndPoint(string endPoint)
|
||||
{
|
||||
string[] ep = endPoint.Split(':');
|
||||
if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
|
||||
IPAddress ip;
|
||||
if (ep.Length > 2) {
|
||||
if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip)) {
|
||||
throw new FormatException("Invalid ip-adress");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!IPAddress.TryParse(ep[0], out ip)) {
|
||||
throw new FormatException("Invalid ip-adress");
|
||||
}
|
||||
}
|
||||
int port;
|
||||
if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port)) {
|
||||
throw new FormatException("Invalid port");
|
||||
}
|
||||
return new IPEndPoint(ip, port);
|
||||
}
|
||||
|
||||
// Generates an unmanaged sockaddr structure from a string-formatted endpoint
|
||||
public static GCHandle Generate_unmananged_sockaddr(string endpoint_str)
|
||||
{
|
||||
IPEndPoint ipEndPoint;
|
||||
ipEndPoint = CreateIPEndPoint (endpoint_str);
|
||||
SocketAddress socketAddress = ipEndPoint.Serialize ();
|
||||
|
||||
// use an array of bytes instead of the sockaddr structure
|
||||
byte[] sockAddrStructureBytes = new byte[socketAddress.Size];
|
||||
GCHandle sockAddrHandle = GCHandle.Alloc (sockAddrStructureBytes, GCHandleType.Pinned);
|
||||
for (int i = 0; i < socketAddress.Size; ++i) {
|
||||
sockAddrStructureBytes [i] = socketAddress [i];
|
||||
}
|
||||
return sockAddrHandle;
|
||||
}
|
||||
|
||||
public static GCHandle Generate_unmanaged_buffer(byte[] buf)
|
||||
{
|
||||
// use an array of bytes instead of the sockaddr structure
|
||||
GCHandle sockAddrHandle = GCHandle.Alloc (buf, GCHandleType.Pinned);
|
||||
return sockAddrHandle;
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
||||
{
|
||||
public float speed = 300f;
|
||||
|
||||
private ZeroTierNetworkInterface zt;
|
||||
private ZTSDK zt;
|
||||
string nwid = "";
|
||||
|
||||
int sock; // The "connection id"
|
||||
@@ -222,7 +222,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
||||
GameObject go;
|
||||
go = GameObject.Find ("inputNetworkID");
|
||||
input = go.GetComponents<InputField> () [0];
|
||||
input.text = "XXXXXXXXXXXXXXXX";
|
||||
input.text = "8056c2e21c000001";
|
||||
go = GameObject.Find ("inputServerAddress");
|
||||
input = go.GetComponents<InputField> () [0];
|
||||
input.text = "10.9.9.203";
|
||||
@@ -234,7 +234,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
||||
input.text = "Welcome to the machine";
|
||||
|
||||
// Create new instance of ZeroTier in separate thread
|
||||
zt = new ZeroTierNetworkInterface ("/Users/ztest", "XXXXXXXXXXXXXXXX");
|
||||
zt = new ZTSDK (".", "8056c2e21c000001");
|
||||
}
|
||||
|
||||
// Terminate the ZeroTier service when the application quits
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Net;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public class ZeroTierUtils {
|
||||
|
||||
// Handles IPv4 and IPv6 notation.
|
||||
public static IPEndPoint CreateIPEndPoint(string endPoint)
|
||||
{
|
||||
string[] ep = endPoint.Split(':');
|
||||
if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
|
||||
IPAddress ip;
|
||||
if (ep.Length > 2) {
|
||||
if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip)) {
|
||||
throw new FormatException("Invalid ip-adress");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!IPAddress.TryParse(ep[0], out ip)) {
|
||||
throw new FormatException("Invalid ip-adress");
|
||||
}
|
||||
}
|
||||
int port;
|
||||
if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port)) {
|
||||
throw new FormatException("Invalid port");
|
||||
}
|
||||
return new IPEndPoint(ip, port);
|
||||
}
|
||||
|
||||
// Generates an unmanaged sockaddr structure from a string-formatted endpoint
|
||||
public static GCHandle Generate_unmananged_sockaddr(string endpoint_str)
|
||||
{
|
||||
IPEndPoint ipEndPoint;
|
||||
ipEndPoint = ZeroTierUtils.CreateIPEndPoint (endpoint_str);
|
||||
SocketAddress socketAddress = ipEndPoint.Serialize ();
|
||||
|
||||
// use an array of bytes instead of the sockaddr structure
|
||||
byte[] sockAddrStructureBytes = new byte[socketAddress.Size];
|
||||
GCHandle sockAddrHandle = GCHandle.Alloc (sockAddrStructureBytes, GCHandleType.Pinned);
|
||||
for (int i = 0; i < socketAddress.Size; ++i) {
|
||||
sockAddrStructureBytes [i] = socketAddress [i];
|
||||
}
|
||||
return sockAddrHandle;
|
||||
}
|
||||
|
||||
public static GCHandle Generate_unmanaged_buffer(byte[] buf)
|
||||
{
|
||||
// use an array of bytes instead of the sockaddr structure
|
||||
GCHandle sockAddrHandle = GCHandle.Alloc (buf, GCHandleType.Pinned);
|
||||
return sockAddrHandle;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user