Example #1
0
        async void ConnectionReceived(object sender, NewConnectionEventArgs e)
        {
            await ClientEngine.MainLoop;

            try {
                if (Engine.ConnectionManager.ShouldBanPeer(e.Peer))
                {
                    e.Connection.Dispose();
                    return;
                }

                if (!e.Connection.IsIncoming)
                {
                    var id = new PeerId(e.Peer, e.Connection, e.TorrentManager.Bitfield?.Clone().SetAll(false));
                    id.LastMessageSent.Restart();
                    id.LastMessageReceived.Restart();

                    Engine.ConnectionManager.ProcessNewOutgoingConnection(e.TorrentManager, id);
                    return;
                }

                Logger.Log(e.Connection, "ListenManager - ConnectionReceived");

                IConnection2 connection = ConnectionConverter.Convert(e.Connection);
                EncryptorFactory.EncryptorResult result = await EncryptorFactory.CheckIncomingConnectionAsync(connection, e.Peer.AllowedEncryption, Engine.Settings, SKeys);

                if (!await HandleHandshake(e.Peer, connection, result.Handshake, result.Decryptor, result.Encryptor))
                {
                    connection.Dispose();
                }
            } catch {
                e.Connection.Dispose();
            }
        }
Example #2
0
        public void AddNavigationConnectionField <TSource, TReturn>(
            ComplexGraphType <TSource> graph,
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> >?resolve = null,
            Type?itemGraphType = null,
            IEnumerable <QueryArgument>?arguments = null,
            IEnumerable <string>?includeNames     = null,
            int pageSize       = 10,
            string?description = null)
            where TReturn : class
        {
            Guard.AgainstNull(nameof(graph), graph);

            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNegative(nameof(pageSize), pageSize);

            itemGraphType ??= GraphTypeFinder.FindGraphType <TReturn>();
            var fieldType = GetFieldType <TSource>(name, itemGraphType);

            var builder = ConnectionBuilder <TSource> .Create <FakeGraph>(name);

            if (description != null)
            {
                builder.Description(description);
            }

            builder.PageSize(pageSize).Bidirectional();
            SetField(builder, fieldType);
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeNames);

            var hasId = keyNames.ContainsKey(typeof(TReturn));

            if (resolve != null)
            {
                builder.ResolveAsync(async context =>
                {
                    var efFieldContext = BuildContext(context);

                    var enumerable = resolve(efFieldContext);


                    enumerable = enumerable.ApplyGraphQlArguments(hasId, context);
                    enumerable = await efFieldContext.Filters.ApplyFilter(enumerable, context.UserContext);
                    var page   = enumerable.ToList();

                    return(ConnectionConverter.ApplyConnectionContext(
                               page,
                               context.First,
                               context.After,
                               context.Last,
                               context.Before));
                });
            }

            var connection = builder;

            var field = graph.AddField(connection.FieldType);

            field.AddWhereArgument(hasId, arguments);
        }
Example #3
0
    public void List(int?first, int?after, int?last, int?before)
    {
        NamerFactory.AdditionalInformation = $"first_{first}_after_{after}_last_{last}_before_{before}";
        var connection = ConnectionConverter.ApplyConnectionContext(list, first, after, last, before);

        ObjectApprover.VerifyWithJson(connection);
    }
        ConnectionBuilder <FakeGraph, TSource> BuildListConnectionField <TSource, TReturn>(
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> > resolve,
            IEnumerable <string> includeName,
            int pageSize,
            Type graphType)
            where TReturn : class
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (resolve == null)
            {
                throw new ArgumentNullException(nameof(resolve));
            }
            if (pageSize < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(pageSize));
            }

            //lookup the graph type if not explicitly specified
            graphType = graphType ?? GraphTypeFinder.FindGraphType <TReturn>();
            //create a ConnectionBuilder<graphType, TSource> type by invoking the static Create method on the generic type
            var fieldType = GetFieldType <TSource>(name, graphType);
            //create a ConnectionBuilder<FakeGraph, TSource> which will be returned from this method
            var builder = ConnectionBuilder <FakeGraph, TSource> .Create(name);

            //set the page size
            builder.PageSize(pageSize);
            //using reflection, override the private field type property of the ConnectionBuilder<FakeGraph, TSource> to be the ConnectionBuilder<graphType, TSource> object
            SetField(builder, fieldType);
            //add the metadata for the tables to be included in the query to the ConnectionBuilder<graphType, TSource> object
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);
            //set the custom resolver
            builder.ResolveAsync(async context =>
            {
                //run the specified resolve function
                var enumerable = resolve(BuildEfContextFromGraphQlContext(context));
                //apply any query filters specified in the arguments
                enumerable = enumerable.ApplyGraphQlArguments(context);
                //apply the global filter on each individually enumerated item
                if (filters != null)
                {
                    enumerable = await filters.ApplyFilter(enumerable, context.UserContext);
                }
                //pagination does NOT occur server-side at this point, as the query has already executed
                var page = enumerable.ToList();
                //return the proper page of data
                return(ConnectionConverter.ApplyConnectionContext(
                           page,
                           context.First,
                           context.After,
                           context.Last,
                           context.Before));
            });

            //return the field to be added to the graph
            return(builder);
        }
Example #5
0
        ConnectionBuilder <TGraph, TSource> BuildListConnectionField <TSource, TGraph, TReturn>(
            string name,
            Func <ResolveFieldContext <TSource>, IEnumerable <TReturn> > resolve,
            IEnumerable <string> includeName,
            int pageSize)
            where TGraph : ObjectGraphType <TReturn>, IGraphType
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNull(nameof(resolve), resolve);
            Guard.AgainstNegative(nameof(pageSize), pageSize);
            var builder = ConnectionBuilder.Create <TGraph, TSource>();

            builder.PageSize(pageSize);
            builder.Name(name);
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);
            builder.Resolve(context =>
            {
                var enumerable = resolve(context);
                enumerable     = enumerable.ApplyGraphQlArguments(context);
                enumerable     = enumerable.Where(item => GlobalFilters.ShouldInclude(context.UserContext, item));
                var page       = enumerable.ToList();

                return(ConnectionConverter.ApplyConnectionContext(
                           page,
                           context.First,
                           context.After,
                           context.Last,
                           context.Before));
            });
            return(builder);
        }
Example #6
0
        ConnectionBuilder <FakeGraph, TSource> BuildListConnectionField <TSource, TReturn>(
            string name,
            Func <ResolveFieldContext <TSource>, IEnumerable <TReturn> > resolve,
            IEnumerable <string> includeName,
            int pageSize,
            Type graphType)
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNull(nameof(resolve), resolve);
            Guard.AgainstNegative(nameof(pageSize), pageSize);
            graphType = GraphTypeFinder.FindGraphType <TReturn>(graphType);
            var fieldType = GetFieldType <TSource>(name, graphType);
            var builder   = ConnectionBuilder <FakeGraph, TSource> .Create(name);

            builder.PageSize(pageSize);
            SetField(builder, fieldType);
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);
            builder.ResolveAsync(async context =>
            {
                var enumerable = resolve(context);
                enumerable     = enumerable.ApplyGraphQlArguments(context);
                enumerable     = await filters.ApplyFilter(enumerable, context.UserContext);
                var page       = enumerable.ToList();

                return(ConnectionConverter.ApplyConnectionContext(
                           page,
                           context.First,
                           context.After,
                           context.Last,
                           context.Before));
            });
            return(builder);
        }
Example #7
0
        async void ConnectToPeer(TorrentManager manager, Peer peer)
        {
            // Connect to the peer.
            IConnection2 connection = ConnectionConverter.Convert(ConnectionFactory.Create(peer.ConnectionUri));

            if (connection == null)
            {
                return;
            }

            var state = new AsyncConnectState(manager, connection, ValueStopwatch.StartNew());

            PendingConnects.Add(state);
            manager.Peers.ConnectingToPeers.Add(peer);

            bool succeeded;

            try {
                await NetworkIO.ConnectAsync(connection);

                succeeded = true;
            } catch {
                succeeded = false;
            }

            PendingConnects.Remove(state);
            manager.Peers.ConnectingToPeers.Remove(peer);
            if (manager.Engine == null || !manager.Mode.CanAcceptConnections)
            {
                manager.Peers.AvailablePeers.Add(peer);
                connection.Dispose();
                return;
            }

            try {
                if (!succeeded)
                {
                    peer.FailedConnectionAttempts++;
                    connection.Dispose();
                    manager.Peers.BusyPeers.Add(peer);
                    manager.RaiseConnectionAttemptFailed(new ConnectionAttemptFailedEventArgs(peer, ConnectionFailureReason.Unreachable, manager));
                }
                else
                {
                    PeerId id = new PeerId(peer, connection, manager.Bitfield?.Clone().SetAll(false));
                    id.LastMessageReceived.Restart();
                    id.LastMessageSent.Restart();

                    Logger.Log(id.Connection, "ConnectionManager - Connection opened");

                    ProcessNewOutgoingConnection(manager, id);
                }
            } catch {
                // FIXME: Do nothing now?
            } finally {
                // Try to connect to another peer
                TryConnect();
            }
        }
    public Task List(int?first, int?after, int?last, int?before)
    {
        var connection = ConnectionConverter.ApplyConnectionContext(list, first, after, last, before);
        var settings   = new VerifySettings();

        settings.UseParameters(first, after, last, before);
        return(Verifier.Verify(connection, settings));
    }
Example #9
0
    public async Task Queryable(int?first, int?after, int?last, int?before)
    {
        NamerFactory.AdditionalInformation = $"first_{first}_after_{after}_last_{last}_before_{before}";
        var queryable  = new AsyncEnumerable <string>(list);
        var connection = await ConnectionConverter.ApplyConnectionContext(queryable, first, after, last, before, new ResolveFieldContext <string>(), new GlobalFilters(), CancellationToken.None);

        ObjectApprover.VerifyWithJson(connection);
    }
Example #10
0
    public async Task Queryable(int?first, int?after, int?last, int?before)
    {
        var fieldContext = new ResolveFieldContext <string>();

        await using var database = await sqlInstance.BuildWithRollback();

        var entities   = database.Context.Entities;
        var connection = await ConnectionConverter.ApplyConnectionContext(entities, first, after, last, before, fieldContext, new Filters());

        ObjectApprover.Verify(connection);
    }
Example #11
0
 internal PeerId(Peer peer, IConnection connection, BitField bitfield)
     : this(peer)
 {
     if (connection == null)
     {
         throw new ArgumentNullException(nameof(connection));
     }
     Connection = ConnectionConverter.Convert(connection);
     Peer       = peer ?? throw new ArgumentNullException(nameof(peer));
     BitField   = bitfield;
 }
    void AddEnumerableConnection <TSource, TGraph, TReturn>(
        ComplexGraphType <TSource> graph,
        string name,
        Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> >?resolve,
        int pageSize,
        string?description,
        IEnumerable <QueryArgument>?arguments,
        IEnumerable <string>?includeNames)
        where TGraph : IGraphType
        where TReturn : class
    {
        var builder = ConnectionBuilder.Create <TGraph, TSource>();

        builder.Name(name);

        if (description is not null)
        {
            builder.Description(description);
        }
        builder.PageSize(pageSize).Bidirectional();
        IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeNames);

        var hasId = keyNames.ContainsKey(typeof(TReturn));

        if (resolve is not null)
        {
            builder.ResolveAsync(async context =>
            {
                var efFieldContext = BuildContext(context);

                var enumerable = resolve(efFieldContext);

                enumerable = enumerable.ApplyGraphQlArguments(hasId, context);
                enumerable = await efFieldContext.Filters.ApplyFilter(enumerable, context.UserContext);
                var page   = enumerable.ToList();

                return(ConnectionConverter.ApplyConnectionContext(
                           page,
                           context.First,
                           context.After,
                           context.Last,
                           context.Before));
            });
        }

        // TODO: works around https://github.com/graphql-dotnet/graphql-dotnet/pull/2581/
        builder.FieldType.Type = typeof(NonNullGraphType <ConnectionType <TGraph, EdgeType <TGraph> > >);
        var field = graph.AddField(builder.FieldType);

        field.AddWhereArgument(hasId, arguments);
    }
    public async Task Queryable(int?first, int?after, int?last, int?before)
    {
        var fieldContext = new ResolveFieldContext <string>();

        await using var database = await sqlInstance.Build(databaseSuffix : $"{first.GetValueOrDefault(0)}{after.GetValueOrDefault(0)}{last.GetValueOrDefault(0)}{before.GetValueOrDefault(0)}");

        var entities   = database.Context.Entities;
        var connection = await ConnectionConverter.ApplyConnectionContext(entities.OrderBy(x => x.Property), first, after, last, before, fieldContext, new Filters());

        var settings = new VerifySettings();

        settings.UseParameters(first, after, last, before);
        await Verifier.Verify(connection.Items.OrderBy(x => x.Property), settings);
    }
        ConnectionBuilder <TSource> BuildListConnectionField <TSource, TReturn>(
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> >?resolve,
            IEnumerable <string>?includeName,
            int pageSize,
            Type?itemGraphType,
            string?description)
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNegative(nameof(pageSize), pageSize);

            itemGraphType ??= GraphTypeFinder.FindGraphType <TReturn>();
            var fieldType = GetFieldType <TSource>(name, itemGraphType);

            var builder = ConnectionBuilder <TSource> .Create <FakeGraph>(name);

            if (description != null)
            {
                builder.Description(description);
            }
            builder.PageSize(pageSize);
            SetField(builder, fieldType);
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);

            if (resolve != null)
            {
                builder.ResolveAsync(async context =>
                {
                    var efFieldContext = BuildContext(context);

                    var enumerable = resolve(efFieldContext);

                    enumerable = enumerable.ApplyGraphQlArguments(context);
                    enumerable = await efFieldContext.Filters.ApplyFilter(enumerable, context.UserContext);
                    var page   = enumerable.ToList();

                    return(ConnectionConverter.ApplyConnectionContext(
                               page,
                               context.First,
                               context.After,
                               context.Last,
                               context.Before));
                });
            }

            return(builder);
        }
Example #15
0
        ConnectionBuilder <TSource> BuildListConnectionField <TSource, TReturn>(
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> > resolve,
            IEnumerable <string>?includeName,
            int pageSize,
            Type?graphType)
            where TReturn : class
        {
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            Guard.AgainstNull(nameof(resolve), resolve);
            Guard.AgainstNegative(nameof(pageSize), pageSize);

            //lookup the graph type if not explicitly specified
            graphType ??= GraphTypeFinder.FindGraphType <TReturn>();
            //create a ConnectionBuilder<graphType, TSource> type by invoking the static Create<graphType> method on the generic type
            var builder = GetConnectionBuilder <TSource>(name, graphType);

            //set the page size
            builder.PageSize(pageSize);
            //add the metadata for the tables to be included in the query to the ConnectionBuilder<graphType, TSource> object
            IncludeAppender.SetIncludeMetadata(builder.FieldType, name, includeName);
            //set the custom resolver
            builder.ResolveAsync(async context =>
            {
                var efFieldContext = BuildContext(context);
                //run the specified resolve function
                var enumerable = resolve(efFieldContext);
                //apply any query filters specified in the arguments
                enumerable = enumerable.ApplyGraphQlArguments(context);
                //apply the global filter on each individually enumerated item
                enumerable = await efFieldContext.Filters.ApplyFilter(enumerable, context.UserContext);
                //pagination does NOT occur server-side at this point, as the query has already executed
                var page = enumerable.ToList();
                //return the proper page of data
                return(ConnectionConverter.ApplyConnectionContext(
                           page,
                           context.First,
                           context.After,
                           context.Last,
                           context.Before));
            });

            //return the field to be added to the graph
            return(builder);
        }
    public Task List(int?first, int?after, int?last, int?before)
    {
        var connection = ConnectionConverter.ApplyConnectionContext(list, first, after, last, before);

        return(Verify(connection));
    }
Example #17
0
    public void List(int?first, int?after, int?last, int?before)
    {
        var connection = ConnectionConverter.ApplyConnectionContext(list, first, after, last, before);

        ObjectApprover.Verify(connection);
    }