public void Update() { switch (frameReceiverStatus) { case FrameReceiverStatus.ToStart: indicator.color = Color.red; RequestAndGetData(); break; case FrameReceiverStatus.ToDefer: indicator.color = Color.red; frameReceiverStatus = FrameReceiverStatus.Defered; Invoke("RequestAndGetData", timeToDeferFailedConnections); break; case FrameReceiverStatus.Defered: break; case FrameReceiverStatus.WaitingForConnection: break; case FrameReceiverStatus.Looping: indicator.color = Color.green; frameMaterial.mainTexture = textureToLoad; break; default: break; } if (qTextureData.Count > 0) { textureToLoad.LoadImage(qTextureData.Dequeue()); textureToLoad.Apply(); VideoTick(); } RenderTick(); var renderDeltaTime = GetRenderDeltaTime(); if (renderFPS != null) { renderFPS.text = string.Format("Render: {0:0.0} ms ({1:0.} fps)", renderDeltaTime, 1000.0f / renderDeltaTime); } var videoDeltaTime = GetVideoDeltaTime(); if (videoFPS != null) { videoFPS.text = string.Format("Video: {0:0.0} ms ({1:0.} fps)", videoDeltaTime, 1000.0f / videoDeltaTime); } }
/// <summary> /// Connects to the server and requests data. /// </summary> private void ConnectListener() { if (frameReceiverStatus == FrameReceiverStatus.WaitingForConnection || frameReceiverStatus == FrameReceiverStatus.ToDefer || frameReceiverStatus == FrameReceiverStatus.Looping) { Debug.Log(TAG + ": ConnectListener() not supported at current status"); return; } frameReceiverStatus = FrameReceiverStatus.WaitingForConnection; Debug.Log(TAG + ": Connecting to " + serverIP); HostName networkHost = new HostName(serverIP); networkConnection = new StreamSocket(); IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, connectionPort.ToString()); AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler); outstandingAction.Completed = aach; }
/// <summary> /// When a connection to the server is established and we can start reading the data, this will be called. /// </summary> /// <param name="asyncInfo">Info about the connection.</param> /// <param name="status">Status of the connection</param> private void RcvNetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status) { try { // Status completed is successful. if (status == AsyncStatus.Completed) { DataReader networkDataReader; DataReaderLoadOperation drlo; // Since we are connected, we can read the data being sent to us. using (networkDataReader = new DataReader(networkConnection.InputStream)) { // read four bytes to get the size. frameReceiverStatus = FrameReceiverStatus.Looping; while (true) { drlo = networkDataReader.LoadAsync(4); while (drlo.Status == AsyncStatus.Started) { // just waiting. } int dataSize = networkDataReader.ReadInt32(); if (dataSize < 0) { Debug.Log(TAG + ": Super bad super big datasize"); } // Need to allocate a new buffer with the dataSize. byte[] mostRecentDataBuffer = new byte[dataSize]; // Read the data. drlo = networkDataReader.LoadAsync((uint)dataSize); while (drlo.Status == AsyncStatus.Started) { // just waiting. } networkDataReader.ReadBytes(mostRecentDataBuffer); Debug.Log(TAG + ": Image received with " + dataSize.ToString() + " bytes"); qTextureData.Enqueue(mostRecentDataBuffer); if (qTextureData.Count > 1) { qTextureData.Dequeue(); } } } networkConnection.Dispose(); frameReceiverStatus = FrameReceiverStatus.ToStart; } else { Debug.Log(TAG + ": Failed to establish connection for rcv. Error Code:\n" + asyncInfo.ErrorCode); // In the failure case we'll requeue the data and wait before trying again. networkConnection.Dispose(); frameReceiverStatus = FrameReceiverStatus.ToDefer; } } catch (Exception e) { networkConnection.Dispose(); frameReceiverStatus = FrameReceiverStatus.ToDefer; Debug.Log(TAG + ": error caught, network disposed, waiting for new connection\n" + e); } }