Fully operational prototype plugin. Fixed RX issues

This commit is contained in:
Joseph Henry
2016-06-20 16:21:28 -07:00
parent a2a939f6f6
commit a7aa5c28c0
9 changed files with 72 additions and 43 deletions

View File

@@ -30,6 +30,8 @@ using System.Collections;
using UnityEngine.UI;
using System;
// Handles the switching of APIs
public class WorldMain : MonoBehaviour {
void Start() {
}
@@ -40,6 +42,4 @@ public class WorldMain : MonoBehaviour {
Dropdown dd = go.GetComponents<Dropdown> () [0];
Debug.Log("API selected: " + dd.captionText.text);
}
}

View File

@@ -113,7 +113,7 @@ public class ZeroTierNetworkInterface {
// RX / TX
[DllImport (DLL_PATH)]
unsafe protected static extern int zt_recv(int sockfd, IntPtr buf, int len);
unsafe protected static extern int zt_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);
[DllImport (DLL_PATH)]
@@ -243,36 +243,14 @@ public class ZeroTierNetworkInterface {
return zt_connect (fd, pSockAddr, addrlen);
}
/*
unsafe {
byte *ptr = (byte *)buffer.ToPointer();
int offset = 0;
for (int i=0; i<height; i++)
{
for (int j=0; j<width; j++)
{
float b = (float)ptr[offset+0] / 255.0f;
float g = (float)ptr[offset+1] / 255.0f;
float r = (float)ptr[offset+2] / 255.0f;
float a = (float)ptr[offset+3] / 255.0f;
offset += 4;
UnityEngine.Color color = new UnityEngine.Color(r, g, b, a);
texture.SetPixel(j, height-i, color);
}
}
}
*/
public int Read(int fd, char[] buf, int len)
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);
Marshal.Copy (ptr, buf, 0, bytes_read); // FIXME: Copies back into managed memory, should maybe avoid copying
string str = Marshal.PtrToStringAuto(ptr);
//Marshal.Copy (ptr, buf, 0, bytes_read);
buf = Marshal.PtrToStringAnsi(ptr).ToCharArray();
return bytes_read;
}

View File

@@ -100,8 +100,8 @@ public class ZeroTierSockets_Demo : MonoBehaviour
Thread connectThread = new Thread(() => {
// Socket()
int sockfd = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified);
Debug.Log ("sockfd = " + sockfd);
connection_socket = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified);
Debug.Log ("sockfd = " + connection_socket);
// Bind()
int port_num;
@@ -123,6 +123,17 @@ public class ZeroTierSockets_Demo : MonoBehaviour
Debug.Log ("accept_res = " + accept_res);
}
char[] msg = new char[1024];
int bytes_read = 0;
while(bytes_read >= 0)
{
//Debug.Log("reading from socket");
bytes_read = zt.Read(accept_res, ref msg, 80);
string msgstr = new string(msg);
Debug.Log("MSG (" + bytes_read + "):" + msgstr);
}
});
connectThread.IsBackground = true;
connectThread.Start();
@@ -169,7 +180,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
input.text = "172.22.211.245";
go = GameObject.Find ("inputServerPort");
input = go.GetComponents<InputField> () [0];
input.text = "8887";
input.text = "5555";
go = GameObject.Find ("inputMessage");
input = go.GetComponents<InputField> () [0];
input.text = "Welcome to the machine";