introduced workaround for interop memory barrier

This commit is contained in:
Joseph Henry
2016-06-16 23:52:14 -07:00
parent b99990d5eb
commit fd81a88009
5 changed files with 99 additions and 72 deletions

View File

@@ -36,14 +36,15 @@ public class Demo : MonoBehaviour
//int bytes_written = zt.Send(connfd,buffer,0, out error); //int bytes_written = zt.Send(connfd,buffer,0, out error);
//print(bytes_written); //print(bytes_written);
char[] buffer = new char[1024]; //char[] buffer = new char[1024];
buffer = "hello".ToCharArray(); //buffer = "hello".ToCharArray();
//print (buffer); //print (buffer);
//Stream stream = new MemoryStream(buffer); //Stream stream = new MemoryStream(buffer);
//BinaryFormatter formatter = new BinaryFormatter(); //BinaryFormatter formatter = new BinaryFormatter();
//formatter.Serialize(stream, "HelloServer"); //formatter.Serialize(stream, "HelloServer");
//int bufferSize = 1024; //int bufferSize = 1024;
Debug.Log ("Sending...");
int bytes_written = zt.Send(connfd, "hello".ToCharArray(),4, out error); int bytes_written = zt.Send(connfd, "hello".ToCharArray(),4, out error);
print(bytes_written); print(bytes_written);
} }
@@ -79,10 +80,15 @@ public class Demo : MonoBehaviour
InputField addr = addr_go.GetComponents<InputField> () [0]; InputField addr = addr_go.GetComponents<InputField> () [0];
InputField port = port_go.GetComponents<InputField> () [0]; InputField port = port_go.GetComponents<InputField> () [0];
Debug.Log ("Connecting to: " + addr.text + ":" + port.text); 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); Thread connectThread = new Thread(() => {
Debug.Log ("server_connection_socket = " + server_connection_socket); byte error = 0;
Debug.Log ("Conenct(): " + error); 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() public void Disconnect()
@@ -97,12 +103,19 @@ public class Demo : MonoBehaviour
public void SendMessage() public void SendMessage()
{ {
//zt_test_network ();
GameObject go = GameObject.Find ("inputMessage"); GameObject go = GameObject.Find ("inputMessage");
InputField msg = go.GetComponents<InputField> () [0]; InputField msg = go.GetComponents<InputField> () [0];
Debug.Log ("Sending Message: " + msg.text);
byte error = 0; Thread sendThread = new Thread(() => {
zt.Send (server_connection_socket, msg.text.ToCharArray (), msg.text.ToCharArray ().Length, out error); Debug.Log ("Sending Message: " + msg.text);
Debug.Log ("Send(): " + error); 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() void Start()
@@ -116,6 +129,9 @@ public class Demo : MonoBehaviour
go = GameObject.Find ("inputServerAddress"); go = GameObject.Find ("inputServerAddress");
input = go.GetComponents<InputField> () [0]; input = go.GetComponents<InputField> () [0];
input.text = "172.22.211.245"; input.text = "172.22.211.245";
go = GameObject.Find ("inputServerPort");
input = go.GetComponents<InputField> () [0];
input.text = "8888";
go = GameObject.Find ("inputMessage"); go = GameObject.Find ("inputMessage");
input = go.GetComponents<InputField> () [0]; input = go.GetComponents<InputField> () [0];
input.text = "Welcome to the machine"; input.text = "Welcome to the machine";
@@ -131,20 +147,30 @@ public class Demo : MonoBehaviour
// Terminate the ZeroTier service when the application quits // Terminate the ZeroTier service when the application quits
void OnApplicationQuit() { void OnApplicationQuit() {
Debug.Log ("OnApplicationQuit()");
zt.Terminate (); zt.Terminate ();
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
/*
GameObject go = GameObject.Find ("_txtStatusIndicator");
Text text = go.GetComponents<Text> () [0];
text.text = zt.IsRunning () ? "ZeroTier Service: ONLINE" : "ZeroTier Service: OFFLINE";
*/
// Rotate ZTCube when ZT is running // Rotate ZTCube when ZT is running
/* /*
if (zt.IsRunning ()) { if (zt.IsRunning ()) {
GameObject go = GameObject.Find ("ZTCube");
go = GameObject.Find ("ZTCube");
Vector3 rotvec = new Vector3 (10f, 10f, 10f); Vector3 rotvec = new Vector3 (10f, 10f, 10f);
go.transform.Rotate (rotvec, speed * Time.deltaTime); go.transform.Rotate (rotvec, speed * Time.deltaTime);
} }
*/ */
/* /*
GameObject go = GameObject.Find("ZTCube"); GameObject go = GameObject.Find("ZTCube");
Text text = go.GetComponents<Text> ()[0]; Text text = go.GetComponents<Text> ()[0];

View File

@@ -112,9 +112,9 @@ public class ZeroTierNetworkInterface {
// RX / TX // RX / TX
[DllImport (DLL_PATH)] [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)] [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 // ZT Thread controls
[DllImport (DLL_PATH)] [DllImport (DLL_PATH)]
@@ -242,6 +242,7 @@ public class ZeroTierNetworkInterface {
// Sends data out over the network // Sends data out over the network
/*
public int Send(int fd, char[] buf, int len, out byte error) public int Send(int fd, char[] buf, int len, out byte error)
{ {
int bytes_written = 0; int bytes_written = 0;
@@ -249,14 +250,12 @@ public class ZeroTierNetworkInterface {
GCHandle buf_handle = GCHandle.Alloc (buf, GCHandleType.Pinned); GCHandle buf_handle = GCHandle.Alloc (buf, GCHandleType.Pinned);
IntPtr pBufPtr = buf_handle.AddrOfPinnedObject (); IntPtr pBufPtr = buf_handle.AddrOfPinnedObject ();
//int len = Marshal.SizeOf (pBufPtr);
if((bytes_written = zt_send (fd, pBufPtr, len)) < 0) { if((bytes_written = zt_send (fd, pBufPtr, len)) < 0) {
error = (byte)bytes_written; error = (byte)bytes_written;
} }
return bytes_written; return bytes_written;
} }
*/
// Structure used to house arrays meant to be sent to unmanaged memory and passed to the // Structure used to house arrays meant to be sent to unmanaged memory and passed to the
// ZeroTier service // ZeroTier service
@@ -265,11 +264,26 @@ public class ZeroTierNetworkInterface {
public IntPtr array; public IntPtr array;
} }
/* // Write data to a ZeroTier socket
// Sends data out over the network
public int Send(int fd, char[] buf, int len, out byte error) 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 (); UnityArrayInput data = new UnityArrayInput ();
data.array = Marshal.AllocHGlobal (Marshal.SizeOf (typeof(char))*buf.Length); data.array = Marshal.AllocHGlobal (Marshal.SizeOf (typeof(char))*buf.Length);
//data.len = buf.Length; //data.len = buf.Length;
@@ -278,7 +292,7 @@ public class ZeroTierNetworkInterface {
try try
{ {
Marshal.Copy(buf, 0, data.array, buf.Length); //Marshal.Copy(buf, 0, data.array, buf.Length);
Debug.Log(buf.Length); Debug.Log(buf.Length);
// ZT API call // ZT API call

View File

@@ -80,8 +80,13 @@ ssize_t zt_recvmsg(RECVMSG_SIG);
int len; int len;
}; };
ssize_t zt_send(int fd, struct UnityArrayInput *buf, int len); ssize_t zt_recv(int fd, void *buf, int len);
ssize_t zt_recv(int fd, struct UnityArrayInput *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 #endif

View File

@@ -300,6 +300,17 @@ const char *get_netpath() {
#if defined(__UNITY_3D__) #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) ssize_t zt_send(int fd, struct UnityArrayInput *buf, int len)
{ {
return write(fd, buf->array, len); return write(fd, buf->array, len);
@@ -309,6 +320,7 @@ const char *get_netpath() {
{ {
return read(fd, buf->array, len); return read(fd, buf->array, len);
} }
*/
#endif #endif
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------

View File

@@ -8,79 +8,49 @@
int main(int argc , char *argv[]) int main(int argc , char *argv[])
{ {
if(argc < 2) {
printf("usage: server <port>\n");
return 1;
}
int port = atoi(argv[1]);
int socket_desc , client_sock , c , read_size; int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client; struct sockaddr_in server , client;
char client_message[2000]; char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0); socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1) if (socket_desc == -1) {
{
printf("Could not create socket"); printf("Could not create socket");
} }
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY; server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 ); server.sin_port = htons(port);
//Bind printf("binding on port %d\n", port);
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) {
{
//print the error message
perror("bind failed. Error"); perror("bind failed. Error");
return 1; return 1;
} }
puts("bind done"); printf("listening\n");
//Listen
listen(socket_desc , 3); listen(socket_desc , 3);
puts("waiting to accept\n");
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in); c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0) if (client_sock < 0)
{ {
perror("accept failed"); perror("accept failed");
return 1; return 1;
} }
puts("Connection accepted"); puts("connection accepted\n reading...\n");
//Receive a message from client // RX
sleep(5); int bytes_read = recv(client_sock , client_message , 2000 , 0);
int bytes_read = recv(client_sock , client_message , 2000 , 0); printf("Read (%d) bytes\n", bytes_read);
for(int i=0; i<bytes_read; i++)
// printf("read (%d) bytes from client: %s\n", bytes_read, client_message); {
printf("Read (%d) bytes\n", bytes_read);
for(int i=0; i<bytes_read; i++)
{
printf("%c", client_message[i]); printf("%c", client_message[i]);
}
/*
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 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; return 0;
} }