Пример #1
0
        public async Task ProcessReceivedPacket(ReceivedPacketInfo receivedPacket)
        {
            Debug.WriteLine("Process Received Packet");
            long           recvFrameID  = receivedPacket.frameID;
            string         recvEngineID = receivedPacket.engineID;
            SentPacketInfo sentPacket   = null;
            bool           isSuccess;

            // increase appropriate amount of tokens
            int increaseCount = 0;

            for (long frameID = _prevRecvFrameID + 1; frameID < recvFrameID; frameID++)
            {
                sentPacket = null;
                if (Const.IS_EXPERIMENT)
                {
                    // Do not remove since we need to measure latency even for the late response
                    isSuccess = _sentPackets.TryGetValue(frameID, out sentPacket);
                }
                else
                {
                    isSuccess = _sentPackets.TryRemove(frameID, out sentPacket);
                }
                if (isSuccess == true)
                {
                    increaseCount++;
                }
            }
            Debug.WriteLine("Increasing Tokens by " + increaseCount);
            IncreaseTokens(increaseCount);

            // deal with the current response
            isSuccess = _sentPackets.TryGetValue(recvFrameID, out sentPacket);
            if (isSuccess == true)
            {
                // do not increase token if have already received duplicated ack
                if (recvFrameID > _prevRecvFrameID)
                {
                    IncreaseTokens(1);
                }

                if (Const.IS_EXPERIMENT)
                {
                    string log = recvFrameID + "\t" + recvEngineID + "\t" +
                                 sentPacket.generatedTime + "\t" + sentPacket.compressedTime + "\t" +
                                 receivedPacket.msgRecvTime + "\t" + receivedPacket.guidanceDoneTime + "\t" +
                                 receivedPacket.status;
                    await _myLogger.WriteString(log + "\n");
                }
            }
            if (recvFrameID > _prevRecvFrameID)
            {
                _prevRecvFrameID = recvFrameID;
            }
        }
        private void StartResultReceivingThread()
        {
            IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
                async(workItem) =>
            {
                while (true)
                {
                    // receive current time at server
                    string recvMsg = await receiveMsgAsync(_resultReceivingSocketReader);
                    UnityEngine.Debug.Log(recvMsg);

                    JsonObject obj  = JsonValue.Parse(recvMsg).GetObject();
                    string status   = null;
                    string result   = null;
                    long frameID    = -1;
                    string engineID = null;

                    if (obj.ContainsKey("status"))
                    {
                        status   = obj.GetNamedString("status");
                        result   = obj.GetNamedString("result");
                        frameID  = (long)obj.GetNamedNumber("frame_id");
                        engineID = obj.GetNamedString("engine_id");
                    }
                    else
                    {
                        UnityEngine.Debug.Log("the return message has no status field");
                        return;
                    }

                    ReceivedPacketInfo receivedPacketInfo = new ReceivedPacketInfo(frameID, engineID, status);
                    receivedPacketInfo.setMsgRecvTime(GetTimeMillis());
                    if (!status.Equals("success"))
                    {
                        receivedPacketInfo.setGuidanceDoneTime(GetTimeMillis());
                        _tokenController.ProcessReceivedPacket(receivedPacketInfo);
                        continue;
                    }

                    if (result != null)
                    {
                        /* parsing result */
                        JsonObject resultJSON = JsonValue.Parse(result).GetObject();

                        // speech guidance
                        if (resultJSON.ContainsKey("speech"))
                        {
                            _speechFeedback = resultJSON.GetNamedString("speech");
                            //UnityEngine.Debug.Log("speech guidance found");

                            textToSpeechManager.SpeakText(_speechFeedback);
                        }

                        // hologram guidance
                        if (resultJSON.ContainsKey("holo_object"))
                        {
                            _guidanceObject = resultJSON.GetNamedString("holo_object");
                            if (!_guidanceObject.Equals("none"))
                            {
                                JsonObject holoPosInfo = resultJSON.GetNamedObject("holo_location");

                                UnityEngine.Debug.Log("Hologram guidance: " + holoPosInfo);
                                float x     = (float)holoPosInfo.GetNamedNumber("x");
                                float y     = (float)holoPosInfo.GetNamedNumber("y");
                                float depth = (float)holoPosInfo.GetNamedNumber("depth");

                                SentPacketInfo p = _tokenController.GetSentPacket(frameID);
                                _guidancePos     = Pixel2WorldPos(x, y, depth, p.projectionMatrix, p.cameraToWorldMatrix);
                            }

                            _guidancePosReady = true;
                        }

                        receivedPacketInfo.setGuidanceDoneTime(GetTimeMillis());
                        _tokenController.ProcessReceivedPacket(receivedPacketInfo);
                    }
                }
            });

            // A reference to the work item is cached so that we can trigger a
            // cancellation when the user presses the Cancel button.
            _resultReceivingWorkItem = asyncAction;
        }