-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cs
73 lines (62 loc) · 2.14 KB
/
Test.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using UnityEngine;
using UnityEngine.Networking;
public class Test : MonoBehaviour {
private int localSocketId;
void Start () {
NetworkTransport.Init();
StartServer();
StartClient();
}
void StartServer () {
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.Unreliable);
HostTopology topology = new HostTopology(config, 10);
localSocketId = NetworkTransport.AddHost(topology, 0);
byte err;
NetworkTransport.StartBroadcastDiscovery(localSocketId, 8888, 1000, 1, 1, StringToBytes("Hello World"), 1024, 1000, out err);
}
void StartClient () {
ConnectionConfig config = new ConnectionConfig();
config.AddChannel(QosType.Unreliable);
HostTopology topology = new HostTopology(config, 10);
localSocketId = NetworkTransport.AddHost(topology, 8888);
byte err;
NetworkTransport.SetBroadcastCredentials(localSocketId, 1000, 1, 1, out err);
}
void Update () {
int connectionId;
int channelId;
int receivedSize;
byte[] msgInBuffer = new byte[1024];
byte error;
NetworkEventType networkEvent = NetworkEventType.DataEvent;
do
{
networkEvent = NetworkTransport.ReceiveFromHost(localSocketId, out connectionId, out channelId, msgInBuffer, 1024, out receivedSize, out error);
if (networkEvent == NetworkEventType.BroadcastEvent)
{
NetworkTransport.GetBroadcastConnectionMessage(localSocketId, msgInBuffer, 1024, out receivedSize, out error);
string senderAddr;
int senderPort;
NetworkTransport.GetBroadcastConnectionInfo(localSocketId, out senderAddr, out senderPort, out error);
Debug.Log("Got broadcast.");
//OnReceivedBroadcast(senderAddr, BytesToString(msgInBuffer));
}
} while (networkEvent != NetworkEventType.Nothing);
}
void OnGUI () {
GUILayout.Box("" +localSocketId);
}
static byte[] StringToBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string BytesToString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
}