Esempio n. 1
0
        private async void _server_ConnectionRequest(object sender, ResonanceListeningServerConnectionRequestEventArgs <BluetoothAdapter> e)
        {
            IResonanceTransporter transporter = ResonanceTransporter.Builder
                                                .Create()
                                                .WithAdapter(e.Accept())
                                                .WithJsonTranscoding()
                                                .Build();

            transporter.StateChanged += (x, ee) =>
            {
                if (ee.NewState != ResonanceComponentState.Connected)
                {
                    InvokeUI(() =>
                    {
                        ConnectedDevices.Remove(transporter);
                    });
                }
            };

            transporter.RegisterRequestHandler <ChatMessageRequest, ChatMessageResponse>(OnChatRequest);

            await transporter.ConnectAsync();

            InvokeUI(() =>
            {
                ConnectedDevices.Add(transporter);
                if (SelectedDevice == null)
                {
                    SelectedDevice = transporter;
                }
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Starts a SignalR session and WebRTC session with the selected client.
        /// </summary>
        private async void StartSession()
        {
            if (!IsInSession)
            {
                try
                {
                    IsFree = false;

                    Logger.LogInformation($"Starting SignalR session with {SelectedClient.ServiceId} for signaling...");

                    _signalingTransporter = ResonanceTransporter.Builder
                                            .Create()
                                            .WithSignalRAdapter(SignalRMode.Legacy)
                                            .WithCredentials(new DemoCredentials()
                    {
                        Name = ClientID
                    })
                                            .WithServiceId(SelectedClient.ServiceId)
                                            .WithUrl(HubUrl)
                                            .WithJsonTranscoding()
                                            .Build();

                    _signalingTransporter.ConnectionLost += OnSignalingConnectionLost;

                    await _signalingTransporter.ConnectAsync();

                    Logger.LogInformation("SignalR session started.");
                    Logger.LogInformation("Initiating WebRTC session...");

                    _webRtcTransporter = ResonanceTransporter.Builder
                                         .Create()
                                         .WithWebRTCAdapter()
                                         .WithSignalingTransporter(_signalingTransporter)
                                         .WithRole(WebRTCAdapterRole.Connect)
                                         .WithDefaultIceServers()
                                         .WithJsonTranscoding()
                                         .Build();

                    _webRtcTransporter.ConnectionLost += OnWebRTCConnectionLost;

                    _webRtcTransporter.RegisterRequestHandler <EchoTextRequest, EchoTextResponse>(OnEchoTextMessageReceived);

                    Logger.LogInformation("Connecting WebRTC transporter.");
                    await _webRtcTransporter.ConnectAsync();

                    IsInSession = true;

                    Logger.LogInformation("WebRTC transporter connected!");
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, ex.Message);
                }
                finally
                {
                    IsFree = true;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Handles incoming signaling connection request from the registered listening service.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="ConnectionRequestEventArgs{DemoCredentials, DemoAdapterInformation}"/> instance containing the event data.</param>
        private async void OnServiceConnectionRequest(object sender, ConnectionRequestEventArgs <DemoCredentials, DemoAdapterInformation> e)
        {
            Logger.LogInformation($"Connection request from {e.RemoteAdapterInformation.Name}.");

            if (IsInSession)
            {
                Logger.LogInformation($"Already in session. Connection request declined.");
                e.Decline();
                return;
            }

            try
            {
                Logger.LogInformation("Starting SignalR session for signaling...");

                SelectedClient = ConnectedClients.FirstOrDefault(x => x.ServiceId == e.RemoteAdapterInformation.Name);

                IsFree = false;

                _signalingTransporter = ResonanceTransporter.Builder
                                        .Create()
                                        .WithAdapter(e.Accept())
                                        .WithJsonTranscoding()
                                        .Build();

                _signalingTransporter.ConnectionLost += OnSignalingConnectionLost;

                await _signalingTransporter.ConnectAsync();

                Logger.LogInformation("SignalR session started. Waiting for WebRTC offer...");

                Logger.LogInformation("Connecting WebRTC transporter...");
                _webRtcTransporter = ResonanceTransporter.Builder
                                     .Create()
                                     .WithWebRTCAdapter()
                                     .WithSignalingTransporter(_signalingTransporter)
                                     .WithRole(WebRTCAdapterRole.Accept)
                                     .WithDefaultIceServers()
                                     .WithJsonTranscoding()
                                     .Build();

                _webRtcTransporter.ConnectionLost += OnWebRTCConnectionLost;
                _webRtcTransporter.RegisterRequestHandler <EchoTextRequest, EchoTextResponse>(OnEchoTextMessageReceived);

                await _webRtcTransporter.ConnectAsync();

                IsInSession = true;
                IsFree      = true;

                Logger.LogInformation("WebRTC transporter connected!");
            }
            catch (Exception ex)
            {
                IsFree = true;
                Logger.LogError(ex, "");
            }
        }
Esempio n. 4
0
        protected async override void OnApplicationReady()
        {
            base.OnApplicationReady();

            _transporter = ResonanceTransporter.Builder
                           .Create()
                           .WithUdpAdapter()
                           .WithServer(TcpAdapter.GetLocalIPAddress(), 9999)
                           .WithJsonTranscoding()
                           .Build();

            _transporter.ConnectionLost += (x, e) =>
            {
                e.FailTransporter = true;
                Logger.LogError($"Remote server has closed. {e.Exception.Message}");
            };

            _client = _transporter.CreateClientProxy <IRemoteDrawingBoardService>();
            _client.RectangleAdded += _client_RectangleAdded;

            try
            {
                await _transporter.ConnectAsync();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Could not connect to the remote server. Please restart the application.");
                return;
            }

            //This is just for testing async methods...
            String welcomeMessage = await _client.GetWelcomeMessage("Roy", 99);

            int countAsync = await _client.GetRectanglesCountAsync();

            int sum = await _client.CalcAsync(10, 15);

            try
            {
                foreach (var rect in _client.Rectangles)
                {
                    Rectangles.Add(rect);
                }
            }
            catch (Exception ex)
            {
                //TODO: Add log here that we could not fetch any server logs or something.
            }
        }
Esempio n. 5
0
        private async void Connect()
        {
            if (!IsConnected)
            {
                try
                {
                    IsFree = false;

                    _transporter = ResonanceTransporter.Builder
                                   .Create()
                                   .WithTcpAdapter()
                                   .WithAddress(SelectedService.Address)
                                   .WithPort(SelectedService.DiscoveryInfo.Port)
                                   .WithJsonTranscoding()
                                   .Build();

                    _transporter.ConnectionLost += ConnectionLost;
                    _transporter.RegisterRequestHandler <JoinSessionRequest>(OnJoinSessionRequest);
                    _transporter.RegisterRequestHandler <NotifyAvailableClientsRequest>(OnNotifyAvailableClientsRequest);
                    _transporter.RegisterRequestHandler <EchoTextRequest, EchoTextResponse>(OnEchoTextRequest);
                    _transporter.RegisterRequestHandler <LeaveSessionRequest>(OnLeaveSessionRequest);

                    await _transporter.ConnectAsync();

                    await _transporter.SendRequestAsync <LoginRequest, LoginResponse>(new LoginRequest()
                    {
                        ClientID = ClientID
                    });

                    await _discoveryClient.StopAsync();

                    IsConnected = true;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, ex.Message);
                }
                finally
                {
                    IsFree = true;
                }
            }
        }
Esempio n. 6
0
        private async void Connect()
        {
            try
            {
                using (UserDialogs.Instance.Loading("Connecting..."))
                {
                    _transporter = ResonanceTransporter.Builder
                                   .Create()
                                   .WithAdapter(new BluetoothAdapter(SelectedDevice))
                                   .WithJsonTranscoding()
                                   .Build();

                    _transporter.StateChanged += (x, e) =>
                    {
                        InvokeUI(async() =>
                        {
                            SendCommand.ChangeCanExecute();

                            if (e.PreviousState == ResonanceComponentState.Connected && e.NewState == ResonanceComponentState.Failed)
                            {
                                await Navigation.PopAsync();
                                UserDialogs.Instance.Toast($"Connection lost.\n{_transporter.FailedStateException}");
                            }
                        });
                    };

                    _transporter.RegisterRequestHandler <ChatMessageRequest, ChatMessageResponse>(OnChatRequest);

                    await _transporter.ConnectAsync();

                    SelectedDevice.Refresh();
                    Logs.Clear();
                }

                await Navigation.PushAsync(new ChatPage(), true);
            }
            catch (Exception ex)
            {
                UserDialogs.Instance.Toast($"Error connecting to the selected device.\n{ex.Message}");
            }
        }
Esempio n. 7
0
        public void Protobuf_Encoding()
        {
            IResonanceTransporter t1 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithProtobufTranscoding()
                                       .Build();

            IResonanceTransporter t2 = ResonanceTransporter.Builder
                                       .Create()
                                       .WithInMemoryAdapter()
                                       .WithAddress("TST")
                                       .WithProtobufTranscoding()
                                       .Build();

            t1.ConnectAsync().Wait();
            t2.ConnectAsync().Wait();

            t2.RequestReceived += (s, e) =>
            {
                Messages.Proto.CalculateRequest receivedRequest = e.Message.Object as Messages.Proto.CalculateRequest;
                t2.SendResponseAsync(new Messages.Proto.CalculateResponse()
                {
                    Sum = receivedRequest.A + receivedRequest.B
                }, e.Message.Token);
            };

            for (int i = 0; i < 1000; i++)
            {
                var request = new Messages.Proto.CalculateRequest()
                {
                    A = 10, B = i
                };
                var response = t1.SendRequestAsync <Messages.Proto.CalculateRequest, Messages.Proto.CalculateResponse>(request).GetAwaiter().GetResult();
            }

            t1.Dispose(true);
            t2.Dispose(true);
        }
Esempio n. 8
0
        private async void Connect()
        {
            if (!IsConnected)
            {
                try
                {
                    IsFree = false;

                    _transporter = ResonanceTransporter.Builder
                                   .Create()
                                   .WithSignalRAdapter(SignalRMode.Legacy)
                                   .WithCredentials(new DemoCredentials()
                    {
                        Name = ClientID
                    })
                                   .WithServiceId(SelectedService.ServiceId)
                                   .WithUrl(HubUrl)
                                   .WithJsonTranscoding()
                                   .Build();

                    _transporter.ConnectionLost += ConnectionLost;

                    await _transporter.ConnectAsync();

                    await _discoveryClient.StopAsync();

                    IsConnected = true;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, ex.Message);
                }
                finally
                {
                    IsFree = true;
                }
            }
        }