protected virtual void OnMessage(IAvaloniaRemoteTransportConnection transport, object obj)
        {
            lock (_lock)
            {
                if (obj is FrameReceivedMessage lastFrame)
                {
                    lock (_lock)
                    {
                        _lastReceivedFrame = lastFrame.SequenceId;
                    }
                    Dispatcher.UIThread.Post(RenderIfNeeded);
                }
                if (obj is ClientSupportedPixelFormatsMessage supportedFormats)
                {
                    lock (_lock)
                        _supportedFormats = supportedFormats.Formats;
                    Dispatcher.UIThread.Post(RenderIfNeeded);
                }
                if (obj is MeasureViewportMessage measure)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        var m = Measure(new Size(measure.Width, measure.Height));
                        _transport.Send(new MeasureViewportMessage
                        {
                            Width  = m.Width,
                            Height = m.Height
                        });
                    });
                }
                if (obj is ClientViewportAllocatedMessage allocated)
                {
                    lock (_lock)
                    {
                        if (_pendingAllocation == null)
                        {
                            Dispatcher.UIThread.Post(() =>
                            {
                                ClientViewportAllocatedMessage allocation;
                                lock (_lock)
                                {
                                    allocation         = _pendingAllocation;
                                    _pendingAllocation = null;
                                }
                                _dpi       = new Vector(allocation.DpiX, allocation.DpiY);
                                ClientSize = new Size(allocation.Width, allocation.Height);
                                RenderIfNeeded();
                            });
                        }

                        _pendingAllocation = allocated;
                    }
                }
            }
        }
Example #2
0
 private void OnMessage(object msg)
 {
     if (msg is FrameMessage frame)
     {
         _connection.Send(new FrameReceivedMessage
         {
             SequenceId = frame.SequenceId
         });
         _lastFrame = frame;
         InvalidateVisual();
     }
 }
 public FrameMessage ConsumeFrame(long lastKnownFrameId)
 {
     lock (_lock)
     {
         if (_lastFrame != null && lastKnownFrameId > _lastConsumedFrame)
         {
             _transport.Send(new FrameReceivedMessage
             {
                 SequenceId = _lastFrame.SequenceId
             });
             _lastConsumedFrame = _lastFrame.SequenceId;
         }
         return(_lastFrame);
     }
 }
        async void Worker()
        {
            while (true)
            {
                SendOperation wi = null;
                lock (_lock)
                {
                    if (_sendQueue.Count != 0)
                    {
                        wi = _sendQueue.Dequeue();
                    }
                }
                if (wi == null)
                {
                    var signal = new TaskCompletionSource <int>();
                    lock (_lock)
                        _signal = signal;
                    await signal.Task.ConfigureAwait(false);

                    continue;
                }
                try
                {
                    await _conn.Send(wi.Message).ConfigureAwait(false);

                    wi.Tcs.TrySetResult(0);
                }
                catch (Exception e)
                {
                    wi.Tcs.TrySetException(e);
                }
            }
        }
Example #5
0
        public HtmlWebSocketTransport(IAvaloniaRemoteTransportConnection signalTransport, Uri listenUri)
        {
            if (listenUri.Scheme != "http")
            {
                throw new ArgumentException("listenUri");
            }

            var resourcePrefix = "Avalonia.DesignerSupport.Remote.HtmlTransport.webapp.build.";

            _resources = typeof(HtmlWebSocketTransport).Assembly.GetManifestResourceNames()
                         .Where(r => r.StartsWith(resourcePrefix) && r.EndsWith(".gz")).ToDictionary(
                r => r.Substring(resourcePrefix.Length).Substring(0, r.Length - resourcePrefix.Length - 3),
                r =>
            {
                using (var s =
                           new GZipStream(typeof(HtmlWebSocketTransport).Assembly.GetManifestResourceStream(r),
                                          CompressionMode.Decompress))
                {
                    var ms = new MemoryStream();
                    s.CopyTo(ms);
                    return(ms.ToArray());
                }
            });

            _signalTransport = signalTransport;
            var address = IPAddress.Parse(listenUri.Host);

            _simpleServer = new SimpleWebSocketHttpServer(address, listenUri.Port);
            _simpleServer.Listen();
            Task.Run(AcceptWorker);
            Task.Run(SocketWorker);
            _signalTransport.Send(new HtmlTransportStartedMessage {
                Uri = "http://" + address + ":" + listenUri.Port + "/"
            });
        }
Example #6
0
 public void UpdateXaml(string xaml, string sourceAssembly)
 {
     _xaml = new UpdateXamlMessage {
         AssemblyPath = sourceAssembly, Xaml = xaml
     };
     _conn?.Send(_xaml);
 }
 public PreviewerSession(IAvaloniaRemoteTransportConnection transport, Action onDispose)
 {
     _transport              = transport;
     _onDispose              = onDispose;
     _transport.OnException += OnTransportException;
     _transport.OnMessage   += TransportOnOnMessage;
     _transport.Send(new ClientSupportedPixelFormatsMessage
     {
         Formats = new[] { PixelFormat.Rgba8888 }
     }).Wait();
     _transport.Send(new ClientViewportAllocatedMessage
     {
         DpiX   = 96,
         DpiY   = 96,
         Width  = 1,
         Height = 1
     }).Wait();
 }
Example #8
0
 public void Resize(Size clientSize)
 {
     _transport.Send(new RequestViewportResizeMessage
     {
         Width  = clientSize.Width,
         Height = clientSize.Height
     });
     ClientSize = clientSize;
     RenderIfNeeded();
 }
Example #9
0
 public RemoteWidget(IAvaloniaRemoteTransportConnection connection)
 {
     _connection            = connection;
     _connection.OnMessage += (t, msg) => Dispatcher.UIThread.Post(() => OnMessage(msg));
     _connection.Send(new ClientSupportedPixelFormatsMessage
     {
         Formats = new[]
         {
             Avalonia.Remote.Protocol.Viewport.PixelFormat.Bgra8888,
             Avalonia.Remote.Protocol.Viewport.PixelFormat.Rgba8888,
         }
     });
 }
 public Task Send(object data)
 {
     if (data is FrameMessage frame)
     {
         _lastFrameMessage = frame;
         _wakeup.Set();
         return(Task.CompletedTask);
     }
     if (data is RequestViewportResizeMessage req)
     {
         return(Task.CompletedTask);
     }
     return(_signalTransport.Send(data));
 }
Example #11
0
 public void OnSessionStarted(IAvaloniaRemoteTransportConnection conn)
 {
     _conn = conn;
     if (_xaml != null)
     {
         _conn.Send(_xaml);
     }
     conn.OnMessage += (c, msg) => _dispatcher.Post(_ =>
     {
         if (c == _conn)
         {
             HandleMessage(c, msg);
         }
     }, null);
 }
Example #12
0
        public MainWindow()
        {
            this.InitializeComponent();
            var tb = this.FindControl <TextBox>("Xaml");

            tb.Text = InitialXaml;
            var scroll = this.FindControl <ScrollViewer>("Remote");
            var rem    = new Center();

            scroll.Content   = rem;
            _errorsContainer = this.FindControl <Control>("ErrorsContainer");
            _errors          = this.FindControl <TextBlock>("Errors");
            tb.GetObservable(TextBox.TextProperty).Subscribe(text => _connection?.Send(new UpdateXamlMessage
            {
                Xaml = text
            }));
            new BsonTcpTransport().Listen(IPAddress.Loopback, 25000, t =>
            {
                Dispatcher.UIThread.Post(() =>
                {
                    if (_connection != null)
                    {
                        _connection.Dispose();
                        _connection.OnMessage -= OnMessage;
                    }
                    _connection = t;
                    rem.Child   = _remote = new RemoteWidget(t);
                    t.Send(new UpdateXamlMessage
                    {
                        Xaml = tb.Text
                    });

                    t.OnMessage += OnMessage;
                });
            });
            Title = "Listening on 127.0.0.1:25000";
        }
Example #13
0
 public Task Send(object data)
 {
     return(_inner?.Send(data));
 }
        protected virtual void OnMessage(IAvaloniaRemoteTransportConnection transport, object obj)
        {
            lock (_lock)
            {
                if (obj is FrameReceivedMessage lastFrame)
                {
                    lock (_lock)
                    {
                        _lastReceivedFrame = Math.Max(lastFrame.SequenceId, _lastReceivedFrame);
                    }
                    Dispatcher.UIThread.Post(RenderIfNeeded);
                }
                if (obj is ClientRenderInfoMessage renderInfo)
                {
                    lock (_lock)
                    {
                        _dpi         = new Vector(renderInfo.DpiX, renderInfo.DpiY);
                        _invalidated = true;
                    }

                    Dispatcher.UIThread.Post(RenderIfNeeded);
                }
                if (obj is ClientSupportedPixelFormatsMessage supportedFormats)
                {
                    lock (_lock)
                        _supportedFormats = supportedFormats.Formats;
                    Dispatcher.UIThread.Post(RenderIfNeeded);
                }
                if (obj is MeasureViewportMessage measure)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        var m = Measure(new Size(measure.Width, measure.Height));
                        _transport.Send(new MeasureViewportMessage
                        {
                            Width  = m.Width,
                            Height = m.Height
                        });
                    });
                }
                if (obj is ClientViewportAllocatedMessage allocated)
                {
                    lock (_lock)
                    {
                        if (_pendingAllocation == null)
                        {
                            Dispatcher.UIThread.Post(() =>
                            {
                                ClientViewportAllocatedMessage allocation;
                                lock (_lock)
                                {
                                    allocation         = _pendingAllocation !;
                                    _pendingAllocation = null;
                                }
                                _dpi       = new Vector(allocation.DpiX, allocation.DpiY);
                                ClientSize = new Size(allocation.Width, allocation.Height);
                                RenderIfNeeded();
                            });
                        }

                        _pendingAllocation = allocated;
                    }
                }
                if (obj is PointerMovedEventMessage pointer)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        Input?.Invoke(new RawPointerEventArgs(
                                          MouseDevice,
                                          0,
                                          InputRoot !,
                                          RawPointerEventType.Move,
                                          new Point(pointer.X, pointer.Y),
                                          GetAvaloniaInputModifiers(pointer.Modifiers)));
                    }, DispatcherPriority.Input);
                }
                if (obj is PointerPressedEventMessage pressed)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        Input?.Invoke(new RawPointerEventArgs(
                                          MouseDevice,
                                          0,
                                          InputRoot !,
                                          GetAvaloniaEventType(pressed.Button, true),
                                          new Point(pressed.X, pressed.Y),
                                          GetAvaloniaInputModifiers(pressed.Modifiers)));
                    }, DispatcherPriority.Input);
                }
                if (obj is PointerReleasedEventMessage released)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        Input?.Invoke(new RawPointerEventArgs(
                                          MouseDevice,
                                          0,
                                          InputRoot !,
                                          GetAvaloniaEventType(released.Button, false),
                                          new Point(released.X, released.Y),
                                          GetAvaloniaInputModifiers(released.Modifiers)));
                    }, DispatcherPriority.Input);
                }
                if (obj is ScrollEventMessage scroll)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        Input?.Invoke(new RawMouseWheelEventArgs(
                                          MouseDevice,
                                          0,
                                          InputRoot !,
                                          new Point(scroll.X, scroll.Y),
                                          new Vector(scroll.DeltaX, scroll.DeltaY),
                                          GetAvaloniaInputModifiers(scroll.Modifiers)));
                    }, DispatcherPriority.Input);
                }
                if (obj is KeyEventMessage key)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        Dispatcher.UIThread.RunJobs(DispatcherPriority.Input + 1);

                        Input?.Invoke(new RawKeyEventArgs(
                                          KeyboardDevice,
                                          0,
                                          InputRoot !,
                                          key.IsDown ? RawKeyEventType.KeyDown : RawKeyEventType.KeyUp,
                                          (Key)key.Key,
                                          GetAvaloniaRawInputModifiers(key.Modifiers)));
                    }, DispatcherPriority.Input);
                }
                if (obj is TextInputEventMessage text)
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        Dispatcher.UIThread.RunJobs(DispatcherPriority.Input + 1);

                        Input?.Invoke(new RawTextInputEventArgs(
                                          KeyboardDevice,
                                          0,
                                          InputRoot !,
                                          text.Text));
                    }, DispatcherPriority.Input);
                }
            }
        }
        void EntitiesAreProperlySerializedAndDeserialized()
        {
            Init();
            var rnd = new Random();

            _server.OnMessage += (_, message) => { };


            object GetRandomValue(Type t, string pathInfo)
            {
                if (t.IsArray)
                {
                    var arr = Array.CreateInstance(t.GetElementType(), 1);
                    ((IList)arr)[0] = GetRandomValue(t.GetElementType(), pathInfo);
                    return(arr);
                }

                if (t == typeof(bool))
                {
                    return(true);
                }
                if (t == typeof(int) || t == typeof(long))
                {
                    return(rnd.Next());
                }
                if (t == typeof(byte))
                {
                    return((byte)rnd.Next(255));
                }
                if (t == typeof(double))
                {
                    return(rnd.NextDouble());
                }
                if (t.IsEnum)
                {
                    return(((IList)Enum.GetValues(t)).Cast <object>().Last());
                }
                if (t == typeof(string))
                {
                    return(Guid.NewGuid().ToString());
                }
                if (t == typeof(Guid))
                {
                    return(Guid.NewGuid());
                }
                if (t == typeof(Exception))
                {
                    return(new Exception("Here"));
                }
                if (t == typeof(ExceptionDetails))
                {
                    return new ExceptionDetails
                           {
                               ExceptionType = "Exception",
                               LineNumber    = 5,
                               LinePosition  = 6,
                               Message       = "Here",
                           }
                }
                ;
                throw new Exception($"Doesn't know how to fabricate a random value for {t}, path {pathInfo}");
            }

            foreach (var t in typeof(MeasureViewportMessage).Assembly.GetTypes().Where(t =>
                                                                                       t.GetCustomAttribute(typeof(AvaloniaRemoteMessageGuidAttribute)) != null))
            {
                var o = Activator.CreateInstance(t);
                foreach (var p in t.GetProperties())
                {
                    p.SetValue(o, GetRandomValue(p.PropertyType, $"{t.FullName}.{p.Name}"));
                }

                _client.Send(o).Wait(200);
                var received = TakeServer();
                Helpers.StructDiff(received, o);
            }
        }