6 #if !UNITY_EDITOR && UNITY_WSA 7 using System.Collections.Generic;
8 using Windows.Networking.Sockets;
9 using Windows.Storage.Streams;
10 using Windows.Networking;
11 using Windows.Foundation;
21 [Tooltip(
"The IPv4 Address of the machine running the Unity editor. Copy and paste this value from RemoteMeshTarget.")]
24 [Tooltip(
"The connection port on the machine to use.")]
25 public int ConnectionPort = 11000;
27 #if !UNITY_EDITOR && UNITY_WSA 28 private StreamSocket networkConnection;
36 private bool Sending =
false;
41 private byte[] nextDataBufferToSend;
46 private Queue<byte[]> dataQueue =
new Queue<byte[]>();
51 private float deferTime = 0.0f;
56 private float timeToDeferFailedConnections = 10.0f;
65 DeferUpdates(deferTime);
70 if (!Sending && dataQueue.Count > 0)
72 byte[] nextPacket = dataQueue.Dequeue();
73 SendDataOverNetwork(nextPacket);
81 void DeferUpdates(
float timeout)
84 Invoke(
"EnableUpdates", timeout);
99 public void SendData(byte[] dataBufferToSend)
101 dataQueue.Enqueue(dataBufferToSend);
108 private void SendDataOverNetwork(byte[] dataBufferToSend)
113 Debug.Log(
"one at a time please");
121 nextDataBufferToSend = dataBufferToSend;
124 HostName networkHost =
new HostName(ServerIP.Trim());
125 networkConnection =
new StreamSocket();
129 IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
130 AsyncActionCompletedHandler aach =
new AsyncActionCompletedHandler(NetworkConnectedHandler);
131 outstandingAction.Completed = aach;
140 public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
143 if (status == AsyncStatus.Completed)
145 DataWriter networkDataWriter;
148 using(networkDataWriter =
new DataWriter(networkConnection.OutputStream))
151 networkDataWriter.WriteInt32(nextDataBufferToSend.Length);
154 networkDataWriter.WriteBytes(nextDataBufferToSend);
157 DataWriterStoreOperation dswo = networkDataWriter.StoreAsync();
158 dswo.Completed =
new AsyncOperationCompletedHandler<uint>(DataSentHandler);
163 Debug.Log(
"Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
165 networkConnection.Dispose();
168 dataQueue.Enqueue(nextDataBufferToSend);
172 deferTime = timeToDeferFailedConnections;
182 public void DataSentHandler(IAsyncOperation<uint> operation, AsyncStatus status)
185 if (status == AsyncStatus.Error)
188 dataQueue.Enqueue(nextDataBufferToSend);
189 deferTime = timeToDeferFailedConnections;
198 networkConnection.Dispose();