Updated protocol-specific tests
This commit is contained in:
@@ -12,14 +12,16 @@ Our implementation currently intends to be the bare minimum required to get your
|
|||||||
|
|
||||||
- `Join(nwid)`: Joins a ZeroTier virtual network
|
- `Join(nwid)`: Joins a ZeroTier virtual network
|
||||||
- `Leave(nwid)`: Leaves 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
|
- `Bind(fd, addr, port)`: Binds to that socket on the address and port given
|
||||||
- `Listen(fd, backlog)`: Puts a socket into a listening state
|
- `Listen(fd, backlog)`: Puts a socket into a listening state
|
||||||
- `Accept(fd)`: Accepts an incoming connection
|
- `Accept(fd)`: Accepts an incoming connection
|
||||||
- `Connect(fd, addr, port)`: Connects to an endpoint associated with the given `fd`
|
- `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`
|
- `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`
|
- `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
|
## Adding ZeroTier to your Unity app
|
||||||
|
|||||||
@@ -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`
|
- `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`
|
- `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`
|
- `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
|
- `CLose(fd)`: Closes a connection to an endpoint
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|||||||
Binary file not shown.
@@ -46,7 +46,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
|||||||
private ZeroTierNetworkInterface zt;
|
private ZeroTierNetworkInterface zt;
|
||||||
string nwid = "";
|
string nwid = "";
|
||||||
|
|
||||||
int connection_socket; // The "connection id"
|
int sock; // The "connection id"
|
||||||
int host_socket;
|
int host_socket;
|
||||||
|
|
||||||
// Demo button methods
|
// Demo button methods
|
||||||
@@ -83,7 +83,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
|||||||
int port_num;
|
int port_num;
|
||||||
int.TryParse(port.text,out port_num);
|
int.TryParse(port.text,out port_num);
|
||||||
zt.Connect (sockfd, addr.text,port_num);
|
zt.Connect (sockfd, addr.text,port_num);
|
||||||
Debug.Log ("connection_socket = " + connection_socket);
|
Debug.Log ("sock = " + sock);
|
||||||
});
|
});
|
||||||
connectThread.IsBackground = true;
|
connectThread.IsBackground = true;
|
||||||
connectThread.Start();
|
connectThread.Start();
|
||||||
@@ -97,20 +97,28 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
|||||||
InputField port = port_go.GetComponents<InputField> () [0];
|
InputField port = port_go.GetComponents<InputField> () [0];
|
||||||
Debug.Log ("Binding to: " + addr.text + ":" + port.text);
|
Debug.Log ("Binding to: " + addr.text + ":" + port.text);
|
||||||
|
|
||||||
|
// Get protocol type from GUI
|
||||||
|
GameObject go = GameObject.Find ("dropdownProtocol");
|
||||||
|
Dropdown dd = go.GetComponents<Dropdown> () [0];
|
||||||
|
Debug.Log("Protocol selected: " + dd.captionText.text);
|
||||||
|
SocketType type = dd.captionText.text == "UDP" ? SocketType.Dgram : SocketType.Stream;
|
||||||
|
|
||||||
Thread connectThread = new Thread(() => {
|
Thread connectThread = new Thread(() => {
|
||||||
|
|
||||||
|
// TCP
|
||||||
|
if(type == SocketType.Stream) {
|
||||||
// Socket()
|
// Socket()
|
||||||
connection_socket = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified);
|
sock = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified);
|
||||||
Debug.Log ("sockfd = " + connection_socket);
|
Debug.Log ("sock = " + sock);
|
||||||
|
|
||||||
// Bind()
|
// Bind()
|
||||||
int port_num;
|
int port_num;
|
||||||
int.TryParse(port.text,out port_num);
|
int.TryParse(port.text,out port_num);
|
||||||
int bind_res = zt.Bind(connection_socket, "0.0.0.0", port_num);
|
int bind_res = zt.Bind(sock, "0.0.0.0", port_num);
|
||||||
Debug.Log ("bind_res = " + bind_res);
|
Debug.Log ("bind_res = " + bind_res);
|
||||||
|
|
||||||
// Listen()
|
// Listen()
|
||||||
int listen_res = zt.Listen(connection_socket, 1);
|
int listen_res = zt.Listen(sock, 1);
|
||||||
Debug.Log ("listen_res = " + listen_res);
|
Debug.Log ("listen_res = " + listen_res);
|
||||||
|
|
||||||
// Accept() loop
|
// Accept() loop
|
||||||
@@ -118,22 +126,44 @@ public class ZeroTierSockets_Demo : MonoBehaviour
|
|||||||
int accept_res = -1;
|
int accept_res = -1;
|
||||||
while(accept_res < 0)
|
while(accept_res < 0)
|
||||||
{
|
{
|
||||||
//yield return new WaitForSeconds(1);
|
accept_res = zt.Accept(sock);
|
||||||
accept_res = zt.Accept(connection_socket);
|
|
||||||
Debug.Log ("accept_res = " + accept_res);
|
Debug.Log ("accept_res = " + accept_res);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RX message
|
||||||
char[] msg = new char[1024];
|
char[] msg = new char[1024];
|
||||||
int bytes_read = 0;
|
int bytes_read = 0;
|
||||||
while(bytes_read >= 0)
|
while(bytes_read >= 0)
|
||||||
{
|
{
|
||||||
//Debug.Log("reading from socket");
|
|
||||||
bytes_read = zt.Read(accept_res, ref msg, 80);
|
bytes_read = zt.Read(accept_res, ref msg, 80);
|
||||||
|
|
||||||
string msgstr = new string(msg);
|
string msgstr = new string(msg);
|
||||||
Debug.Log("MSG (" + bytes_read + "):" + msgstr);
|
Debug.Log("MSG (" + bytes_read + "):" + msgstr);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UDP
|
||||||
|
else if(type == SocketType.Dgram)
|
||||||
|
{
|
||||||
|
// Socket()
|
||||||
|
sock = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Dgram, (int)ProtocolType.Unspecified);
|
||||||
|
Debug.Log ("sock = " + sock);
|
||||||
|
|
||||||
|
// 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;
|
connectThread.IsBackground = true;
|
||||||
connectThread.Start();
|
connectThread.Start();
|
||||||
@@ -146,25 +176,44 @@ public class ZeroTierSockets_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 ("Disconnecting from: " + addr.text + ":" + port.text);
|
Debug.Log ("Disconnecting from: " + addr.text + ":" + port.text);
|
||||||
Debug.Log ("Disconnect(): " + zt.Close (connection_socket));
|
Debug.Log ("Disconnect(): " + zt.Close (sock));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage()
|
public void SendMessage()
|
||||||
{
|
{
|
||||||
//zt_test_network ();
|
// Get info from GUI
|
||||||
/*
|
GameObject go = GameObject.Find ("dropdownProtocol");
|
||||||
GameObject go = GameObject.Find ("inputMessage");
|
Dropdown dd = go.GetComponents<Dropdown> () [0];
|
||||||
InputField msg = go.GetComponents<InputField> () [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<InputField> () [0];
|
||||||
|
|
||||||
|
// Get port number from UI
|
||||||
|
GameObject port_go = GameObject.Find ("inputServerPort");
|
||||||
|
InputField port = port_go.GetComponents<InputField> () [0];
|
||||||
|
int port_num;
|
||||||
|
int.TryParse(port.text,out port_num);
|
||||||
|
|
||||||
|
int bytes_written = 0;
|
||||||
|
|
||||||
Thread sendThread = new Thread(() => {
|
Thread sendThread = new Thread(() => {
|
||||||
Debug.Log ("Sending Message: " + msg.text);
|
// TCP
|
||||||
byte error = 0;
|
if(type == SocketType.Stream) {
|
||||||
zt.Send (server_connection_socket, msg.text.ToCharArray (), msg.text.ToCharArray ().Length, out error);
|
bytes_written = zt.Write (sock, msg.text.ToCharArray(), msg.text.Length);
|
||||||
Debug.Log ("Send(): " + error);
|
}
|
||||||
|
|
||||||
|
// 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.IsBackground = true;
|
||||||
sendThread.Start();
|
sendThread.Start();
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
|
|||||||
Reference in New Issue
Block a user