Esempio n. 1
0
        public async override Task <TEndpoint> Discover(int timeout = 0)
        {
            //            using (System.IO.File.Open(@"C:\Development\Projects\CFS\Wellspring\clr\test.dat", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) { }

            //            using (var fs = new FileStream(@"C:\Development\Projects\CFS\Wellspring\clr\test.dat", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            //#if NETSTANDARD
            //            using (var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(fs, this.MapName, 1024 * 32, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false))
            //#else
            //            using (var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(fs, this.MapName, 1024 * 32, MemoryMappedFileAccess.Read, null, HandleInheritability.None, false))
            //#endif
            using (var mmf = MemoryMappedFile.OpenExisting(this.MapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.None))
                using (var accessor = mmf.CreateViewAccessor(0, 1024 * 32, MemoryMappedFileAccess.ReadWrite))
                {
                    var databaseAccessor = new MemoryMappedDatabaseAccessor(accessor);

                    var startTime = DateTime.UtcNow;
                    var records   = this.ResolveValidRecords(databaseAccessor, this.Identifier).ToArray();
                    while (records.Length <= 0)
                    {
                        if (timeout > 0 && (DateTime.UtcNow - startTime).TotalMilliseconds > timeout)
                        {
                            throw new Exception($"Discovery timeout ({this.Identifier})");
                        }

                        await Task.Delay(500);

                        records = this.ResolveValidRecords(databaseAccessor, this.Identifier).ToArray();
                    }

                    var rand           = new Random();
                    var encodedPayload = records[rand.Next(0, records.Length)];

                    return(this.EndpointDecoder.Decode(encodedPayload.Payload));
                }
        }
Esempio n. 2
0
        public override Task Register(IEncodableEndpoint endpoint)
        {
            //            bool isNew = false;
            //            if (!System.IO.File.Exists(@"C:\Development\Projects\CFS\Wellspring\clr\test.dat"))
            //            {
            //                isNew = true;
            //                using (System.IO.File.Open(@"C:\Development\Projects\CFS\Wellspring\clr\test.dat", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) { }
            //            }

            //            using (var fs = new FileStream(@"C:\Development\Projects\CFS\Wellspring\clr\test.dat", FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            //#if NETSTANDARD
            //            using (var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(fs, this.MapName, 1024 * 32, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false))
            //#else
            //            using (var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(fs, this.MapName, 1024 * 32, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, false))
            //#endif
            using (var mmf = MemoryMappedFile.OpenExisting("Local\\" + this.MapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.None))
                using (var accessor = mmf.CreateViewAccessor(0, 1024 * 32, MemoryMappedFileAccess.ReadWrite))
                {
                    var databaseAccessor = new MemoryMappedDatabaseAccessor(accessor);

                    //if (isNew)
                    //    databaseAccessor.InitializeDatabase();

                    var existingRecords = databaseAccessor.ResolveRecords(this.Identifier);

                    var encodedEndpoint       = endpoint.Encode();
                    var matchedExistingRecord = existingRecords.SingleOrDefault(record => ((System.Collections.IStructuralEquatable)record.Payload).Equals(encodedEndpoint, System.Collections.StructuralComparisons.StructuralEqualityComparer));
                    if (matchedExistingRecord == null)
                    {
                        databaseAccessor.AppendRecord(this.Identifier, encodedEndpoint);
                    }
                    else if (DateTime.UtcNow.ToUnixTimestamp() - matchedExistingRecord.Timestamp > 60)
                    {
                        databaseAccessor.UpdateRecord(matchedExistingRecord.Offset, this.Identifier, encodedEndpoint);
                    }
                }

            return(Task.FromResult(true));
        }
Esempio n. 3
0
        public override async Task <TEndpoint[]> DiscoverAll(int timeout = 0)
        {
            using (var mmf = MemoryMappedFile.OpenExisting(this.MapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.None))
                using (var accessor = mmf.CreateViewAccessor(0, 1024 * 32, MemoryMappedFileAccess.ReadWrite))
                {
                    var databaseAccessor = new MemoryMappedDatabaseAccessor(accessor);

                    var startTime = DateTime.UtcNow;
                    var records   = this.ResolveValidRecords(databaseAccessor, this.Identifier).ToArray();
                    while (records.Length <= 0)
                    {
                        if (timeout > 0 && (DateTime.UtcNow - startTime).TotalMilliseconds > timeout)
                        {
                            throw new Exception("Discovery timeout");
                        }

                        await Task.Delay(500);

                        records = this.ResolveValidRecords(databaseAccessor, this.Identifier).ToArray();
                    }

                    return(records.Select(encodedPayload => this.EndpointDecoder.Decode(encodedPayload.Payload)).ToArray());
                }
        }
Esempio n. 4
0
 private IEnumerable <MemoryMappedDatabaseRecord> ResolveValidRecords(MemoryMappedDatabaseAccessor databaseAccessor, string identifier)
 {
     return(databaseAccessor.ResolveRecords(this.Identifier)
            .Where(r => DateTime.UtcNow.ToUnixTimestamp() - r.Timestamp <= 60)
            .Where(r => !this.BlacklistedPayloads.Any(br => ((System.Collections.IStructuralEquatable)r.Payload).Equals(br, System.Collections.StructuralComparisons.StructuralEqualityComparer))));
 }