diff --git a/docs/zt_llapi_unity.md b/docs/zt_llapi_unity.md new file mode 100644 index 0000000..533025b --- /dev/null +++ b/docs/zt_llapi_unity.md @@ -0,0 +1,57 @@ +ZeroTier Unity LLAPI +==== + +We've tried to replicate the behavior of the Unity3D LLAPI to make using ZeroTier as easy as possible. Everything is handled via a `ZeroTierNetworkInterface` object. You can use it as follows: + +## Creating a host and receiving data + +``` +public class MyObject +{ + private ZeroTierNetworkInterface zt; + + void Start() + { + zt = new ZeroTierNetworkInterface("/Users/Bob/UnityGame/nc_8c493f5bef1747a6"); + zt.AddHost(); + } + + void Update() + { + NetworkEventType ne = zt.Receive(); + + switch(ne) + { + case NetworkEventType.ConnectEvent: + Debug.Log("Client connected!"); + break; + case NetworkEventType.DataEvent: + Debug.Log("Received data from client!"); + } + } + +} +``` + +## Connecting to a server and sending a message + +public class MyObject +{ + private ZeroTierNetworkInterface zt; + + void Start() + { + zt = new ZeroTierNetworkInterface("/Users/Bob/UnityGame/nc_8c493f5bef1747a6"); + + byte error; + int conn_id = zt.Connect(0, "192.168.0.50", "8080", out error); + + if(conn_id) { + zt.Send(conn_id, "Welcome to the machine!", 24, error); + } + else { + Debug.Log("Unable to connect to host"); + } + + } +} \ No newline at end of file diff --git a/integrations/Unity3D/Assets/Demo.cs b/integrations/Unity3D/Assets/Demo.cs index ef2a9e6..8d23b9d 100644 --- a/integrations/Unity3D/Assets/Demo.cs +++ b/integrations/Unity3D/Assets/Demo.cs @@ -15,48 +15,6 @@ public class Demo : MonoBehaviour int server_connection_socket; // The "connection id" - private void zt_sample_network_test_thread() - { - print("test_network"); - - byte error; - // Prepare sample data buffer - /* - byte[] buffer = new byte[1024]; - Stream stream = new MemoryStream(buffer); - BinaryFormatter f = new BinaryFormatter(); - f.Serialize ( stream , "Welcome to the machine! (from Unity3D)" ); - int error; - */ - - // Connect to server - int connfd = zt.Connect (0, "172.22.211.245", 8888, out error); - print(connfd); - - // Send sample data to server - //int bytes_written = zt.Send(connfd,buffer,0, out error); - //print(bytes_written); - - //char[] buffer = new char[1024]; - //buffer = "hello".ToCharArray(); - //print (buffer); - //Stream stream = new MemoryStream(buffer); - //BinaryFormatter formatter = new BinaryFormatter(); - //formatter.Serialize(stream, "HelloServer"); - //int bufferSize = 1024; - - Debug.Log ("Sending..."); - int bytes_written = zt.Send(connfd, "hello".ToCharArray(),4, out error); - print(bytes_written); - } - - public void zt_test_network() - { - Thread networkTestThread = new Thread(() => { zt_sample_network_test_thread();}); - networkTestThread.IsBackground = true; - networkTestThread.Start(); - } - // Demo button methods public void Join() { diff --git a/integrations/Unity3D/Assets/ZeroTier.cs b/integrations/Unity3D/Assets/ZeroTier.cs index 4d618a0..db1c95b 100644 --- a/integrations/Unity3D/Assets/ZeroTier.cs +++ b/integrations/Unity3D/Assets/ZeroTier.cs @@ -27,16 +27,16 @@ using UnityEngine; using UnityEngine.UI; +using UnityEngine.Networking; + using System; using System.Collections; using System.Runtime.InteropServices; using System.Threading; -using UnityEngine.Networking; - using System.Net.Sockets; using System.Net; - using System.IO; +using C5; public class ZeroTierNetworkInterface { @@ -64,9 +64,6 @@ public class ZeroTierNetworkInterface { const string DLL_PATH = "ZeroTierSDK_Unity3D_ANDROID"; #endif - // ZeroTier background thread - private Thread ztThread; - // Interop structures [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)] public struct sockaddr { @@ -77,6 +74,11 @@ public class ZeroTierNetworkInterface { public string sa_data; } + // ZeroTier background thread + private Thread ztThread; + + private ArrayList connections = new ArrayList (); + // Virtual network interace config private int MaxPacketSize; private string rpc_path = "/does/this/work"; @@ -88,6 +90,7 @@ public class ZeroTierNetworkInterface { Debug.Log("Native ZT Plugin: " + str); } +#region // ZeroTier service / debug initialization [DllImport (DLL_PATH)] public static extern void SetDebugFunction( IntPtr fp ); @@ -129,6 +132,7 @@ public class ZeroTierNetworkInterface { private static extern bool zt_join_network(string nwid); [DllImport (DLL_PATH)] private static extern void zt_leave_network(string nwid); +#endregion // Thread which starts the ZeroTier service // The ZeroTier service may spin off a SOCKS5 proxy server @@ -295,6 +299,10 @@ public class ZeroTierNetworkInterface { */ public NetworkEventType Receive(out int hostId, out int connectionId, out int channelId, byte[] buffer, int bufferSize, out int receivedSize, out byte error) { + for (int i = 0; i < connections.Count; i++) { + + } + int res; res = zt_recv (connectionId, buffer, bufferSize);