Exemplo n.º 1
0
        private void ReceivingMain()
        {
            log.Info("Started the receive thread.");
            Volatile.Write(ref this.receivingState, RUNNING);
            var msg = new Msg();

            msg.InitPool(this.maxFrameSize);
            using (var pullSocket = new PullSocket(this.incomingConnection))
            {
                while (Volatile.Read(ref this.receivingState) == RUNNING)
                {
                    try
                    {
                        if (pullSocket.TryReceive(ref msg, new TimeSpan(0, 0, 5)))
                        {
                            var body = msg.Data;
                            if (this.encoder.Decode(body, out var envelope))
                            {
                                this.subject.OnNext(envelope);
                            }
                        }
                        msg.InitPool(this.maxFrameSize);
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex, ex.Message);
                    }
                }
            }

            Volatile.Write(ref this.receivingState, STOPPED);
            log.Info("Receiving thread has stopped.");
        }
    protected override void UpdateExtendedControls()
    {
        if (isRegistered == false)
        {
            return;
        }

        if (_lastCenterEyePosition == Vector3.zero)
        {
            Vector3    position    = Vector3.zero;
            Quaternion orientation = Quaternion.identity;
            GetTransform((byte)AirVRHeadTrackerKey.Transform, ref position, ref orientation);

            if (position != Vector3.zero)
            {
                _lastCenterEyePosition = position;
            }
        }

        Debug.Assert(_zmqPredictedMotion != null);
        while (_zmqPredictedMotion.TryReceive(ref _msgRecv, System.TimeSpan.Zero))
        {
            if (_msgRecv.Size <= 0)
            {
                continue;
            }

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(_msgRecv.Data, 0, 8);
                for (int i = 0; i < 9; i++)
                {
                    Array.Reverse(_msgRecv.Data, 8 + i * 4, 4);
                }
            }

            _lastTimeStamp   = BitConverter.ToDouble(_msgRecv.Data, 0);
            _lastOrientation = new Quaternion(BitConverter.ToSingle(_msgRecv.Data, 8),
                                              BitConverter.ToSingle(_msgRecv.Data, 8 + 4),
                                              BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 2),
                                              BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 3));

            _predictionTime      = BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 4);
            _originalOrientation = new Quaternion(BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 5),
                                                  BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 6),
                                                  BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 7),
                                                  BitConverter.ToSingle(_msgRecv.Data, 8 + 4 * 8));
        }

        if (eventHandler != null)
        {
            eventHandler.OnReceivedPredictionServerData(_lastTimeStamp, _lastOrientation, _predictionTime, _originalOrientation);
        }

        OverrideControlTransform((byte)AirVRHeadTrackerKey.Transform, _lastTimeStamp, _lastOrientation * _lastCenterEyePosition, _lastOrientation);
    }
 private void Main()
 {
     Volatile.Write(ref this.currentState, RUNNING);
     log.Info("Incoming Connection Started...");
     using (this.pullSocket = new PullSocket(this.connectionString))
     {
         Msg msg = new Msg();
         msg.InitPool(this.maxFrameSize);
         while (Volatile.Read(ref this.currentState) == RUNNING)
         {
             try
             {
                 if (pullSocket.TryReceive(ref msg, new TimeSpan(0, 0, 5)))
                 {
                     if (msg.Data.Length >= MessageSettings.HEADER_SIZE &&
                         this.encoder.Decode(msg.Data, out MessageEnvelope <TPayloadType, TBody> envelope))
                     {
                         if (this.incomingMessageBuilder.Add(envelope, out Message <TPayloadType, TBody> message))
                         {
                             this.incomingMessageProcessor.Handle(message);
                         }
                     }
                     else
                     {
                         log.Warn($"Invalid message received");
                     }
                 }
             }
             catch (Exception ex)
             {
                 log.Error(ex, ex.Message);
             }
         }
     }
     log.Info("Incoming connection stopped.");
     Volatile.Write(ref this.currentState, STOPPED);
 }
    public void Update()
    {
        if (_owner.bypassPrediction)
        {
            leftProjection = rightProjection = Rect.MinMaxRect(-1, -1, 1, 1);
            return;
        }

        if (_zmqPredictedMotion == null)
        {
            return;
        }

        _prevExternalInputActualPress     = externalInputActualPress;
        _prevExternalInputPredictivePress = externalInputPredictivePress;

        while (_zmqPredictedMotion.TryReceive(ref _msgRecv, TimeSpan.Zero))
        {
            if (_msgRecv.Size <= 0)
            {
                continue;
            }

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(_msgRecv.Data, 0, 8);
                for (int i = 0; i < 49; i++)
                {
                    Array.Reverse(_msgRecv.Data, 8 + i * 4, 4);
                }
            }

            int pos = 0;
            timestamp      = getLong(_msgRecv.Data, ref pos);
            predictionTime = getFloat(_msgRecv.Data, ref pos);

            var inputLeftEyePosition  = getPosition(_msgRecv.Data, ref pos);
            var inputRightEyePosition = getPosition(_msgRecv.Data, ref pos);
            var inputHeadOrientation  = getRotation(_msgRecv.Data, ref pos);
            var inputProjection       = getProjection(_msgRecv.Data, ref pos);
            var inputRightHandPose    = new Pose(getPosition(_msgRecv.Data, ref pos), getRotation(_msgRecv.Data, ref pos));

            var leftEyePosition  = getPosition(_msgRecv.Data, ref pos);
            var rightEyePosition = getPosition(_msgRecv.Data, ref pos);
            var headOrientation  = getRotation(_msgRecv.Data, ref pos);

            leftEye  = new Pose(leftEyePosition, headOrientation);
            rightEye = new Pose(rightEyePosition, headOrientation);

            leftProjection  = getProjection(_msgRecv.Data, ref pos);
            rightProjection = getProjection(_msgRecv.Data, ref pos);

            foveationInnerRadius  = getFloat(_msgRecv.Data, ref pos);
            foveationMiddleRadius = getFloat(_msgRecv.Data, ref pos);

            rightHand = new Pose(getPosition(_msgRecv.Data, ref pos), getRotation(_msgRecv.Data, ref pos));

            externalInputId = getUshort(_msgRecv.Data, ref pos);

            var actualPress    = getBool(_msgRecv.Data, ref pos);
            var predictedPress = getBool(_msgRecv.Data, ref pos);

            if (_prevExternalInputActualPress != actualPress)
            {
                externalInputActualPress = actualPress;
            }
            if (_prevExternalInputPredictivePress != predictedPress)
            {
                externalInputPredictivePress = predictedPress;
            }

            _motionDataProvider.Put(timestamp, predictionTime,
                                    new Pose(inputLeftEyePosition, inputHeadOrientation), new Pose(inputRightEyePosition, inputHeadOrientation),
                                    MPPProjection.FromRect(inputProjection), inputRightHandPose,
                                    leftEye, rightEye,
                                    MPPProjection.FromRect(leftProjection), MPPProjection.FromRect(rightProjection),
                                    foveationInnerRadius, foveationMiddleRadius, rightHand);
        }
    }