diff --git a/integrations/Unity3D/Assets/Demo.cs b/integrations/Unity3D/Assets/Demo.cs index 148fdcf..4151889 100644 --- a/integrations/Unity3D/Assets/Demo.cs +++ b/integrations/Unity3D/Assets/Demo.cs @@ -36,14 +36,15 @@ public class Demo : MonoBehaviour //int bytes_written = zt.Send(connfd,buffer,0, out error); //print(bytes_written); - char[] buffer = new char[1024]; - buffer = "hello".ToCharArray(); + //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); } @@ -79,10 +80,15 @@ public class Demo : MonoBehaviour InputField addr = addr_go.GetComponents () [0]; InputField port = port_go.GetComponents () [0]; Debug.Log ("Connecting to: " + addr.text + ":" + port.text); - byte error = 0; - server_connection_socket = zt.Connect (0, addr.text, int.Parse (port.text), out error); - Debug.Log ("server_connection_socket = " + server_connection_socket); - Debug.Log ("Conenct(): " + error); + + Thread connectThread = new Thread(() => { + byte error = 0; + server_connection_socket = zt.Connect (0, addr.text, int.Parse (port.text), out error); + Debug.Log ("server_connection_socket = " + server_connection_socket); + Debug.Log ("Connect(): " + error); + }); + connectThread.IsBackground = true; + connectThread.Start(); } public void Disconnect() @@ -97,12 +103,19 @@ public class Demo : MonoBehaviour public void SendMessage() { + //zt_test_network (); GameObject go = GameObject.Find ("inputMessage"); InputField msg = go.GetComponents () [0]; - 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); + + 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); + }); + sendThread.IsBackground = true; + sendThread.Start(); + } void Start() @@ -116,6 +129,9 @@ public class Demo : MonoBehaviour go = GameObject.Find ("inputServerAddress"); input = go.GetComponents () [0]; input.text = "172.22.211.245"; + go = GameObject.Find ("inputServerPort"); + input = go.GetComponents () [0]; + input.text = "8888"; go = GameObject.Find ("inputMessage"); input = go.GetComponents () [0]; input.text = "Welcome to the machine"; @@ -131,20 +147,30 @@ public class Demo : MonoBehaviour // Terminate the ZeroTier service when the application quits void OnApplicationQuit() { + Debug.Log ("OnApplicationQuit()"); zt.Terminate (); } // Update is called once per frame void Update () { + /* + GameObject go = GameObject.Find ("_txtStatusIndicator"); + Text text = go.GetComponents () [0]; + text.text = zt.IsRunning () ? "ZeroTier Service: ONLINE" : "ZeroTier Service: OFFLINE"; + */ + // Rotate ZTCube when ZT is running /* if (zt.IsRunning ()) { - GameObject go = GameObject.Find ("ZTCube"); + + + go = GameObject.Find ("ZTCube"); Vector3 rotvec = new Vector3 (10f, 10f, 10f); go.transform.Rotate (rotvec, speed * Time.deltaTime); } */ + /* GameObject go = GameObject.Find("ZTCube"); Text text = go.GetComponents ()[0]; diff --git a/integrations/Unity3D/Assets/ZeroTier.cs b/integrations/Unity3D/Assets/ZeroTier.cs index 3707a27..acdd87a 100644 --- a/integrations/Unity3D/Assets/ZeroTier.cs +++ b/integrations/Unity3D/Assets/ZeroTier.cs @@ -112,9 +112,9 @@ public class ZeroTierNetworkInterface { // RX / TX [DllImport (DLL_PATH)] - unsafe private static extern int zt_recv(int sockfd, System.IntPtr buf, int len); + unsafe private static extern int zt_recv(int sockfd, string buf, int len); [DllImport (DLL_PATH)] - unsafe private static extern int zt_send(int sockfd, System.IntPtr buf, int len); + unsafe private static extern int zt_send(int sockfd, string buf, int len); // ZT Thread controls [DllImport (DLL_PATH)] @@ -242,6 +242,7 @@ public class ZeroTierNetworkInterface { // Sends data out over the network + /* public int Send(int fd, char[] buf, int len, out byte error) { int bytes_written = 0; @@ -249,14 +250,12 @@ public class ZeroTierNetworkInterface { GCHandle buf_handle = GCHandle.Alloc (buf, GCHandleType.Pinned); IntPtr pBufPtr = buf_handle.AddrOfPinnedObject (); - - //int len = Marshal.SizeOf (pBufPtr); if((bytes_written = zt_send (fd, pBufPtr, len)) < 0) { error = (byte)bytes_written; } return bytes_written; } - + */ // Structure used to house arrays meant to be sent to unmanaged memory and passed to the // ZeroTier service @@ -265,11 +264,26 @@ public class ZeroTierNetworkInterface { public IntPtr array; } - /* - // Sends data out over the network + // Write data to a ZeroTier socket public int Send(int fd, char[] buf, int len, out byte error) { - //char[] buffer = new char[1024]; + GCHandle buf_handle = GCHandle.Alloc (buf, GCHandleType.Pinned); + IntPtr pBufPtr = buf_handle.AddrOfPinnedObject (); + + error = 0; + int bytes_written; + string str = new string (buf); + if((bytes_written = zt_send (fd, str, len)) < 0) { + error = (byte)bytes_written; + } + return bytes_written; + } + + // Sends data out over the network + /* + public int Send(int fd, char[] bufx, int len, out byte error) + { + char[] buf = "this is another test".ToCharArray(); UnityArrayInput data = new UnityArrayInput (); data.array = Marshal.AllocHGlobal (Marshal.SizeOf (typeof(char))*buf.Length); //data.len = buf.Length; @@ -278,7 +292,7 @@ public class ZeroTierNetworkInterface { try { - Marshal.Copy(buf, 0, data.array, buf.Length); + //Marshal.Copy(buf, 0, data.array, buf.Length); Debug.Log(buf.Length); // ZT API call diff --git a/src/SDK.h b/src/SDK.h index 301bf65..cb9d0d9 100644 --- a/src/SDK.h +++ b/src/SDK.h @@ -80,8 +80,13 @@ ssize_t zt_recvmsg(RECVMSG_SIG); int len; }; - ssize_t zt_send(int fd, struct UnityArrayInput *buf, int len); - ssize_t zt_recv(int fd, struct UnityArrayInput *buf, int len); + ssize_t zt_recv(int fd, void *buf, int len); + + ssize_t zt_send(int fd, void *buf, int len); + + + //ssize_t zt_send(int fd, struct UnityArrayInput *buf, int len); + //ssize_t zt_recv(int fd, struct UnityArrayInput *buf, int len); #endif diff --git a/src/SDK_Sockets.c b/src/SDK_Sockets.c index 2ed02f6..22f7c48 100644 --- a/src/SDK_Sockets.c +++ b/src/SDK_Sockets.c @@ -299,7 +299,18 @@ const char *get_netpath() { // ------------------------------------------------------------------------------ #if defined(__UNITY_3D__) + + ssize_t zt_send(int fd, void *buf, int len) + { + return write(fd, buf, len); + } + ssize_t zt_recv(int fd, void *buf, int len) + { + return read(fd, buf, len); + } + + /* ssize_t zt_send(int fd, struct UnityArrayInput *buf, int len) { return write(fd, buf->array, len); @@ -309,6 +320,7 @@ const char *get_netpath() { { return read(fd, buf->array, len); } + */ #endif // ------------------------------------------------------------------------------ diff --git a/tests/server.c b/tests/server.c index 41096ba..4758478 100644 --- a/tests/server.c +++ b/tests/server.c @@ -8,79 +8,49 @@ int main(int argc , char *argv[]) { + if(argc < 2) { + printf("usage: server \n"); + return 1; + } + + int port = atoi(argv[1]); int socket_desc , client_sock , c , read_size; struct sockaddr_in server , client; char client_message[2000]; - //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); - if (socket_desc == -1) - { + if (socket_desc == -1) { printf("Could not create socket"); } - puts("Socket created"); - //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; - server.sin_port = htons( 8888 ); + server.sin_port = htons(port); - //Bind - if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) - { - //print the error message + printf("binding on port %d\n", port); + if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) { perror("bind failed. Error"); return 1; } - puts("bind done"); - - //Listen - listen(socket_desc , 3); - - //Accept and incoming connection - puts("Waiting for incoming connections..."); + printf("listening\n"); + listen(socket_desc , 3); + puts("waiting to accept\n"); c = sizeof(struct sockaddr_in); - - //accept connection from an incoming client + client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); if (client_sock < 0) { perror("accept failed"); return 1; } - puts("Connection accepted"); + puts("connection accepted\n reading...\n"); - //Receive a message from client - sleep(5); - int bytes_read = recv(client_sock , client_message , 2000 , 0); - -// printf("read (%d) bytes from client: %s\n", bytes_read, client_message); - -printf("Read (%d) bytes\n", bytes_read); -for(int i=0; i 0 ) - { - //Send the message back to client - //write(client_sock , client_message , strlen(client_message)); - printf("from client: %s\n", client_message); -} - */ - -/* - if(read_size == 0) - { - puts("Client disconnected"); - fflush(stdout); } - else if(read_size == -1) - { - perror("recv failed"); - } - */ return 0; }