示例#1
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));
        }
示例#2
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))));
 }