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";

View File

@@ -83,7 +83,7 @@ int zt_socket(SOCKET_SIG);
int zt_connect(CONNECT_SIG);
int zt_bind(BIND_SIG);
#if defined(__linux__)
int zt_accept(ACCEPT_SIG);
int zt_accept4(ACCEPT_SIG);
#endif
int zt_accept(ACCEPT_SIG);
int zt_listen(LISTEN_SIG);

View File

@@ -53,6 +53,9 @@ extern "C" {
#define SERVICE_CONNECT_ATTEMPTS 30
ssize_t sock_fd_write(int sock, int fd);
ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
static int rpc_count;
static pthread_mutex_t lock;
@@ -111,10 +114,6 @@ int load_symbols_rpc()
int rpc_join(char * sockname)
{
FILE *f = fopen("/Users/Joseph/utest2/_log.txt","a");
fprintf(f, sockname);
if(sockname == NULL) {
printf("Warning, rpc netpath is NULL\n");
}
@@ -277,39 +276,65 @@ ssize_t sock_fd_write(int sock, int fd)
*/
ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
{
FILE *file = fopen("/Users/Joseph/code/__log","a");
ssize_t size;
if (fd) {
fprintf(file, "A");
struct msghdr msg;
struct iovec iov;
union {
struct cmsghdr cmsghdr;
char control[CMSG_SPACE(sizeof (int))];
} cmsgu;
fprintf(file, "B");
struct cmsghdr *cmsg;
iov.iov_base = buf;
iov.iov_len = bufsize;
msg.msg_name = NULL;
msg.msg_namelen = 0;
fprintf(file, "C");
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsgu.control;
msg.msg_controllen = sizeof(cmsgu.control);
size = recvmsg (sock, &msg, 0);
fprintf(file, "D");
if (size < 0)
return -1;
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
fprintf(file, "E");
if (cmsg->cmsg_level != SOL_SOCKET) {
fprintf(file, "F");
fprintf (stderr, "invalid cmsg_level %d\n",cmsg->cmsg_level);
return -1;
}
if (cmsg->cmsg_type != SCM_RIGHTS) {
fprintf(file, "G");
fprintf (stderr, "invalid cmsg_type %d\n",cmsg->cmsg_type);
return -1;
}
*fd = *((int *) CMSG_DATA(cmsg));
} else *fd = -1;
} else {
fprintf(file, "H");
*fd = -1;}
} else {
fprintf(file, "I");
size = read (sock, buf, bufsize);
if (size < 0) {
fprintf(stderr, "sock_fd_read(): read: Error\n");

View File

@@ -312,7 +312,14 @@ const char *get_netpath() {
ssize_t zt_recv(int fd, void *buf, int len)
{
return read(fd, buf, len);
dwr("zt_recv(%d): \n", fd);
int bytes_read = read(fd, buf, len);
if(bytes_read >= 0)
{
dwr("zt_recv(): MSG(%d): %s\n", bytes_read, buf);
}
return bytes_read;
}
int zt_set_nonblock(int fd)
@@ -470,11 +477,15 @@ const char *get_netpath() {
int zt_accept(ACCEPT_SIG)
{
//dwr(MSG_DEBUG,"zt_accept(%d):\n", sockfd);
dwr(MSG_DEBUG,"zt_accept(%d):\n", sockfd);
// FIXME: Find a better solution for this before production
#if !defined(__UNITY_3D__)
if(addr)
addr->sa_family = AF_INET;
#endif
int new_fd = get_new_fd(sockfd);
dwr(MSG_DEBUG,"newfd = %d\n", new_fd);
if(new_fd > 0) {
errno = ERR_OK;
return new_fd;

View File

@@ -13,7 +13,7 @@ int main(int argc , char *argv[])
return 1;
}
int sock, port = atoi(argv[1]);
int sock, port = atoi(argv[2]);
struct sockaddr_in server;
char message[1000] , server_reply[2000];
@@ -40,11 +40,15 @@ int main(int argc , char *argv[])
scanf("%s" , message);
//Send some data
if(send(sock , message , strlen(message) , 0) < 0)
if(send(sock , "welcome to the machine!" ,24 , 0) < 0)
{
puts("Send failed");
return 1;
}
else
{
printf("len = %d\n", strlen(message));
}
//Receive a reply from the server
if(recv(sock , server_reply , 2000 , 0) < 0)