Esempio n. 1
0
        public async Task StartWithExistingSocket(StreamSocket socket)
        {
            try
            {
                Debug.WriteLine($"{nameof(PushClient)}: Starting with existing socket.");
                Socket = socket;
                if (_loopGroup != null)
                {
                    await Shutdown().ConfigureAwait(false);
                }
                _loopGroup = new SingleThreadEventLoop();

                var streamSocketChannel = new StreamSocketChannel(Socket);
                streamSocketChannel.Pipeline.AddLast(new FbnsPacketEncoder(), new FbnsPacketDecoder(), this);

                await _loopGroup.RegisterAsync(streamSocketChannel).ConfigureAwait(false);
                await SendPing().ConfigureAwait(false);

                await _context.Executor.Schedule(KeepAliveLoop, TimeSpan.FromSeconds(KEEP_ALIVE - 60));
            }
            catch (TaskCanceledException)
            {
                // pass
            }
            catch (Exception e)
            {
#if !DEBUG
                Crashes.TrackError(e);
#endif
                await Shutdown().ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        public async Task StartFresh()
        {
            try
            {
                Debug.WriteLine($"{nameof(PushClient)}: Starting fresh.");
                if (_loopGroup != null)
                {
                    await Shutdown();
                }
                _loopGroup = new SingleThreadEventLoop();

                var connectPacket = new FbnsConnectPacket
                {
                    Payload = await PayloadProcessor.BuildPayload(ConnectionData)
                };

                Socket = new StreamSocket();
                Socket.Control.KeepAlive = true;
                Socket.Control.NoDelay   = true;
                if (await RequestBackgroundAccess())
                {
                    try
                    {
                        Socket.EnableTransferOwnership(_socketActivityTask.TaskId, SocketActivityConnectedStandbyAction.Wake);
                    }
                    catch (Exception connectedStandby)
                    {
                        Debug.WriteLine(connectedStandby);
                        Debug.WriteLine($"{nameof(PushClient)}: Connected standby not available.");
                        try
                        {
                            Socket.EnableTransferOwnership(_socketActivityTask.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        }
                        catch (Exception e)
                        {
#if !DEBUG
                            Crashes.TrackError(e);
#endif
                            Debug.WriteLine(e);
                            Debug.WriteLine($"{nameof(PushClient)}: Failed to transfer socket completely!");
                            await Shutdown().ConfigureAwait(false);

                            return;
                        }
                    }
                }

                await Socket.ConnectAsync(new HostName(HOST_NAME), "443", SocketProtectionLevel.Tls12);

                var streamSocketChannel = new StreamSocketChannel(Socket);
                streamSocketChannel.Pipeline.AddLast(new FbnsPacketEncoder(), new FbnsPacketDecoder(), this);

                await _loopGroup.RegisterAsync(streamSocketChannel).ConfigureAwait(false);

                await streamSocketChannel.WriteAndFlushAsync(connectPacket).ConfigureAwait(false);
            }
            catch (Exception e)
            {
#if !DEBUG
                Crashes.TrackError(e);
#endif
                await Shutdown().ConfigureAwait(false);
            }
        }