diff --git a/docs/unity3d_ios_zt_sdk.md b/docs/unity3d_ios_zt_sdk.md index 246378e..2bd4940 100644 --- a/docs/unity3d_ios_zt_sdk.md +++ b/docs/unity3d_ios_zt_sdk.md @@ -12,14 +12,16 @@ Our implementation currently intends to be the bare minimum required to get your - `Join(nwid)`: Joins a ZeroTier virtual network - `Leave(nwid)`: Leaves a ZeroTier virtual network -- `Socket(family, type, protocol)`: Creates a ZeroTier-administered socket +- `Socket(family, type, protocol)`: Creates a ZeroTier-administered socket (returns an `fd`) - `Bind(fd, addr, port)`: Binds to that socket on the address and port given - `Listen(fd, backlog)`: Puts a socket into a listening state - `Accept(fd)`: Accepts an incoming connection - `Connect(fd, addr, port)`: Connects to an endpoint associated with the given `fd` - `Write(fd, buf, len)`: Sends data to the endpoint associated with the given `fd` - `Read(fd, buf, len)`: Receives data from an endpoint associated with the given `fd` -- `CLose(fd)`: Closes a connection with an endpoint +- `SendTo(fd, buf, len, flags, addr, port)`: Sends data to a given address +- `RecvFrom(fd, ref buf, len, flags, addr, port)`: Receives data +- `CLose(fd)`: Closes a connection to an endpoint *** ## Adding ZeroTier to your Unity app diff --git a/docs/unity3d_osx_zt_sdk.md b/docs/unity3d_osx_zt_sdk.md index e71839c..abbdfed 100644 --- a/docs/unity3d_osx_zt_sdk.md +++ b/docs/unity3d_osx_zt_sdk.md @@ -19,6 +19,8 @@ Our implementation currently intends to be the bare minimum required to get your - `Connect(fd, addr, port)`: Connects to an endpoint associated with the given `fd` - `Write(fd, buf, len)`: Sends data to the endpoint associated with the given `fd` - `Read(fd, buf, len)`: Receives data from an endpoint associated with the given `fd` +- `SendTo(fd, buf, len, flags, addr, port)`: Sends data to a given address +- `RecvFrom(fd, ref buf, len, flags, addr, port)`: Receives data - `CLose(fd)`: Closes a connection to an endpoint *** diff --git a/integrations/Apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.xcworkspace/xcuserdata/Joseph.xcuserdatad/UserInterfaceState.xcuserstate b/integrations/Apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.xcworkspace/xcuserdata/Joseph.xcuserdatad/UserInterfaceState.xcuserstate index aa59c5b..cb1d0ed 100644 Binary files a/integrations/Apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.xcworkspace/xcuserdata/Joseph.xcuserdatad/UserInterfaceState.xcuserstate and b/integrations/Apple/ZeroTierSDK_Apple/ZeroTierSDK_Apple.xcodeproj/project.xcworkspace/xcuserdata/Joseph.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/integrations/Unity3D/Assets/ZeroTierSockets_Demo.cs b/integrations/Unity3D/Assets/ZeroTierSockets_Demo.cs index 8c81b1a..2baf79b 100644 --- a/integrations/Unity3D/Assets/ZeroTierSockets_Demo.cs +++ b/integrations/Unity3D/Assets/ZeroTierSockets_Demo.cs @@ -46,7 +46,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour private ZeroTierNetworkInterface zt; string nwid = ""; - int connection_socket; // The "connection id" + int sock; // The "connection id" int host_socket; // Demo button methods @@ -83,7 +83,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour int port_num; int.TryParse(port.text,out port_num); zt.Connect (sockfd, addr.text,port_num); - Debug.Log ("connection_socket = " + connection_socket); + Debug.Log ("sock = " + sock); }); connectThread.IsBackground = true; connectThread.Start(); @@ -97,42 +97,72 @@ public class ZeroTierSockets_Demo : MonoBehaviour InputField port = port_go.GetComponents () [0]; Debug.Log ("Binding to: " + addr.text + ":" + port.text); + // Get protocol type from GUI + GameObject go = GameObject.Find ("dropdownProtocol"); + Dropdown dd = go.GetComponents () [0]; + Debug.Log("Protocol selected: " + dd.captionText.text); + SocketType type = dd.captionText.text == "UDP" ? SocketType.Dgram : SocketType.Stream; + Thread connectThread = new Thread(() => { - // Socket() - connection_socket = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified); - Debug.Log ("sockfd = " + connection_socket); + // TCP + if(type == SocketType.Stream) { + // Socket() + sock = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified); + Debug.Log ("sock = " + sock); - // Bind() - int port_num; - int.TryParse(port.text,out port_num); - int bind_res = zt.Bind(connection_socket, "0.0.0.0", port_num); - Debug.Log ("bind_res = " + bind_res); + // Bind() + int port_num; + int.TryParse(port.text,out port_num); + int bind_res = zt.Bind(sock, "0.0.0.0", port_num); + Debug.Log ("bind_res = " + bind_res); - // Listen() - int listen_res = zt.Listen(connection_socket, 1); - Debug.Log ("listen_res = " + listen_res); + // Listen() + int listen_res = zt.Listen(sock, 1); + Debug.Log ("listen_res = " + listen_res); - // Accept() loop - Debug.Log("entering accept() loop"); - int accept_res = -1; - while(accept_res < 0) - { - //yield return new WaitForSeconds(1); - accept_res = zt.Accept(connection_socket); - Debug.Log ("accept_res = " + accept_res); + // Accept() loop + Debug.Log("entering accept() loop"); + int accept_res = -1; + while(accept_res < 0) + { + accept_res = zt.Accept(sock); + Debug.Log ("accept_res = " + accept_res); + } + // RX message + char[] msg = new char[1024]; + int bytes_read = 0; + while(bytes_read >= 0) + { + bytes_read = zt.Read(accept_res, ref msg, 80); + string msgstr = new string(msg); + Debug.Log("MSG (" + bytes_read + "):" + msgstr); + } } - char[] msg = new char[1024]; - int bytes_read = 0; - while(bytes_read >= 0) + // UDP + else if(type == SocketType.Dgram) { - //Debug.Log("reading from socket"); - bytes_read = zt.Read(accept_res, ref msg, 80); + // Socket() + sock = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Dgram, (int)ProtocolType.Unspecified); + Debug.Log ("sock = " + sock); - string msgstr = new string(msg); - Debug.Log("MSG (" + bytes_read + "):" + msgstr); + // Bind() + int port_num; + int.TryParse(port.text,out port_num); + int bind_res = zt.Bind(sock, "0.0.0.0", port_num); + Debug.Log ("bind_res = " + bind_res); + + // RX message + int bytes_read = 0, flags = 0, msg_len = 1024; + char[] msg = new char[msg_len]; + while(bytes_read >= 0) + { + bytes_read = zt.RecvFrom(sock, ref msg, msg_len, flags, "0.0.0.0", port_num); + string msgstr = new string(msg); + Debug.Log("MSG (" + bytes_read + "):" + msgstr); + } } }); connectThread.IsBackground = true; @@ -146,25 +176,44 @@ public class ZeroTierSockets_Demo : MonoBehaviour InputField addr = addr_go.GetComponents () [0]; InputField port = port_go.GetComponents () [0]; Debug.Log ("Disconnecting from: " + addr.text + ":" + port.text); - Debug.Log ("Disconnect(): " + zt.Close (connection_socket)); + Debug.Log ("Disconnect(): " + zt.Close (sock)); } public void SendMessage() { - //zt_test_network (); - /* - GameObject go = GameObject.Find ("inputMessage"); - InputField msg = go.GetComponents () [0]; + // Get info from GUI + GameObject go = GameObject.Find ("dropdownProtocol"); + Dropdown dd = go.GetComponents () [0]; + Debug.Log("Protocol selected: " + dd.captionText.text); + SocketType type = dd.captionText.text == "UDP" ? SocketType.Dgram : SocketType.Stream; + + GameObject input = GameObject.Find ("inputMessage"); + InputField msg = input.GetComponents () [0]; + + // Get port number from UI + GameObject port_go = GameObject.Find ("inputServerPort"); + InputField port = port_go.GetComponents () [0]; + int port_num; + int.TryParse(port.text,out port_num); + + int bytes_written = 0; Thread sendThread = new Thread(() => { - Debug.Log ("Sending Message: " + msg.text); - byte error = 0; - zt.Send (server_connection_socket, msg.text.ToCharArray (), msg.text.ToCharArray ().Length, out error); - Debug.Log ("Send(): " + error); + // TCP + if(type == SocketType.Stream) { + bytes_written = zt.Write (sock, msg.text.ToCharArray(), msg.text.Length); + } + + // UDP + else if(type == SocketType.Dgram) { + int flags = 0; + string addr = "0.0.0.0"; + bytes_written = zt.SendTo(sock, msg.text.ToCharArray(), msg.text.Length, flags, addr, port_num); + } + Debug.Log ("bytes_written = " + bytes_written); }); sendThread.IsBackground = true; sendThread.Start(); - */ } void Start()