コード例 #1
0
        public ConnectionTracker(Lifetime lifetime, ILogger logger, UnityHost host, UnityEditorProtocol editorProtocol,
                                 IThreading locks, UnitySolutionTracker unitySolutionTracker)
        {
            State = new Property <UnityEditorState>(lifetime, "UnityEditorPlugin::ConnectionState", UnityEditorState.Disconnected);

            if (locks.Dispatcher.IsAsyncBehaviorProhibited)
            {
                return;
            }

            unitySolutionTracker.IsUnityProject.AdviseOnce(lifetime, args =>
            {
                if (!args)
                {
                    return;
                }

                //check connection between backend and unity editor
                locks.QueueRecurring(lifetime, "PeriodicallyCheck", TimeSpan.FromSeconds(1), () =>
                {
                    var model = editorProtocol.UnityModel.Value;
                    if (model == null)
                    {
                        State.SetValue(UnityEditorState.Disconnected);
                    }
                    else
                    {
                        if (!model.IsBound)
                        {
                            State.SetValue(UnityEditorState.Disconnected);
                        }

                        var rdTask = model.GetUnityEditorState.Start(Unit.Instance);
                        rdTask?.Result.Advise(lifetime, result =>
                        {
                            State.SetValue(result.Result);
                            logger.Trace($"Inside Result. Sending connection state. State: {State.Value}");
                            host.PerformModelAction(m => m.EditorState.Value = Wrap(State.Value));
                        });

                        var waitTask = Task.Delay(TimeSpan.FromSeconds(2));
                        waitTask.ContinueWith(_ =>
                        {
                            if (rdTask != null && !rdTask.AsTask().IsCompleted)
                            {
                                logger.Trace($"There were no response from Unity in one second. Set connection state to Disconnected.");
                                State.SetValue(UnityEditorState.Disconnected);
                            }
                        }, locks.Tasks.GuardedMainThreadScheduler);
                    }

                    logger.Trace($"Sending connection state. State: {State.Value}");
                    host.PerformModelAction(m => m.EditorState.Value = Wrap(State.Value));
                });
            });
        }
コード例 #2
0
        public ConnectionTracker(Lifetime lifetime, ILogger logger, UnityHost host, UnityEditorProtocol editorProtocol,
                                 IThreading locks, UnitySolutionTracker unitySolutionTracker)
        {
            State = new Property <UnityEditorState>(lifetime, "UnityEditorPlugin::ConnectionState", UnityEditorState.Disconnected);

            if (locks.Dispatcher.IsAsyncBehaviorProhibited)
            {
                return;
            }

            unitySolutionTracker.IsUnityProject.AdviseOnce(lifetime, args =>
            {
                if (!args)
                {
                    return;
                }

                //check connection between backend and unity editor
                locks.QueueRecurring(lifetime, "PeriodicallyCheck", TimeSpan.FromSeconds(1), () =>
                {
                    var model = editorProtocol.UnityModel.Value;
                    if (model == null)
                    {
                        State.SetValue(UnityEditorState.Disconnected);
                    }
                    else
                    {
                        try
                        {
                            var rdTask = model.GetUnityEditorState.Start(Unit.Instance);
                            rdTask?.Result.Advise(lifetime, result =>
                            {
                                State.SetValue(result.Result);
                                logger.Trace($"myIsConnected = {State.Value}");
                            });
                        }
                        catch (Exception e)
                        {
                            e.Data.Add("UnityModel", editorProtocol.UnityModel.Value);
                            throw;
                        }
                    }

                    logger.Trace($"Sending connection state. State: {State.Value}");
                    host.PerformModelAction(m => m.EditorState.Value = Wrap(State.Value));
                });
            });
        }