aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.PPCConsole/ViewModels/RemoteDesktopViewVM.cs
blob: eb5d57447f22600b5c0864a9b9671925a7df9fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Tango.Core.Commands;
using Tango.Core.Threading;
using Tango.FSE.Common;
using Tango.RemoteDesktop.Frames;
using Tango.RemoteDesktop.Network;

namespace Tango.FSE.PPCConsole.ViewModels
{
    public class RemoteDesktopViewVM : FSEViewModel
    {
        private RasterFrame _currentFrame;

        private BitmapSource _source;
        public BitmapSource Source
        {
            get { return _source; }
            set { _source = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand StartCommand { get; set; }
        public RelayCommand StopCommand { get; set; }

        public RemoteDesktopViewVM()
        {
            StartCommand = new RelayCommand(StartRemoteDesktop, () => MachineProvider.IsConnected);
            StopCommand = new RelayCommand(StopRemoteDesktop);
        }

        public override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            MachineProvider.MachineConnected += (_, __) => InvalidateRelayCommands();
            MachineProvider.MachineDisconnected += (_, __) => InvalidateRelayCommands();
        }

        private void StartRemoteDesktop()
        {
            SequencerThread<StartRemoteDesktopSessionResponse> sequencer = null;
            sequencer = new SequencerThread<StartRemoteDesktopSessionResponse>((response) => 
            {
                if (response.Packet == null)
                {
                    sequencer.FrameRate = 1000 / response.FrameRate;
                    return; //Returned just to notice that there was no timeout..
                }

                try
                {
                    if (!response.Packet.IsPartial)
                    {
                        using (MemoryStream ms = new MemoryStream(response.Packet.Bitmap))
                        {
                            if (_currentFrame != null)
                            {
                                _currentFrame.Dispose();
                            }

                            _currentFrame = new RasterFrame(new Bitmap(ms));
                        }
                    }
                    else
                    {
                        using (MemoryStream ms = new MemoryStream(response.Packet.Bitmap))
                        {
                            var diffFrame = new RasterFrame(new Bitmap(ms), response.Packet.PartialRegion.Left, response.Packet.PartialRegion.Top);
                            diffFrame.Apply(_currentFrame);
                            diffFrame.Dispose();
                        }
                    }

                    Source = _currentFrame.ToBitmapSource();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error on remote desktop packet received: {ex.Message}");
                }
            });

            sequencer.Start();

            MachineProvider.MachineOperator.SendGenericContinuousRequest<StartRemoteDesktopSessionRequest, StartRemoteDesktopSessionResponse>(new StartRemoteDesktopSessionRequest() { }, new Transport.TransportContinuousRequestConfig()
            {
                ContinuousTimeout = TimeSpan.FromSeconds(30),
            }).Subscribe((response) =>
            {
                sequencer.Push(response);

            }, (ex) =>
            {
                Debug.WriteLine(ex);
            }, () => { });
        }

        private void StopRemoteDesktop()
        {

        }
    }
}