public void SetLease(LeaseToken leaseToken)
        {
            LeaseToken token = new LeaseToken(leaseToken.Path, leaseToken.Expiry, leaseToken.Type, leaseToken.LastSync, leaseToken.Expiry + this.interSiteLeaseExpiryInterval + FileLeaseManager.LeaseExpiryCriticalAlertPadding, leaseToken.Version);

            FileLeaseManager.LeaseOperationResult leaseOperationResult = FileLeaseManager.TryRunLeaseOperation(new FileLeaseManager.LeaseOperation(this.SetLeaseOperation), new FileLeaseManager.LeaseOperationRequest(token));
            if (!leaseOperationResult.Succeeded)
            {
                this.logSession.LogException(EdgeSyncLoggingLevel.Low, EdgeSyncEvent.TargetConnection, leaseOperationResult.Exception, "Sync failed because Edgesync failed to update lease file");
                throw new ExDirectoryException(leaseOperationResult.Exception);
            }
        }
 public static FileLeaseManager.LeaseOperationResult GetLeaseOperation(FileLeaseManager.LeaseOperationRequest request)
 {
     FileLeaseManager.LeaseOperationResult result;
     using (FileStream fileStream = new FileStream(request.LeasePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
     {
         byte[] array   = new byte[fileStream.Length];
         int    count   = fileStream.Read(array, 0, array.Length);
         string @string = Encoding.ASCII.GetString(array, 0, count);
         result = new FileLeaseManager.LeaseOperationResult(LeaseToken.Parse(@string));
     }
     return(result);
 }
        private FileLeaseManager.LeaseOperationResult SetLeaseOperation(FileLeaseManager.LeaseOperationRequest request)
        {
            string path = this.useBackupLeaseLocation ? this.backupLeaseFilePath : this.primaryLeaseFilePath;

            FileLeaseManager.LeaseOperationResult result;
            using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                byte[] bytes = Encoding.ASCII.GetBytes(request.Token.StringForm);
                fileStream.Write(bytes, 0, bytes.Length);
                fileStream.Flush();
                result = new FileLeaseManager.LeaseOperationResult();
            }
            return(result);
        }
 public LeaseToken GetLease()
 {
     FileLeaseManager.LeaseOperationResult leaseOperationResult = FileLeaseManager.TryRunLeaseOperation(new FileLeaseManager.LeaseOperation(FileLeaseManager.GetLeaseOperation), new FileLeaseManager.LeaseOperationRequest(this.primaryLeaseFilePath));
     if (leaseOperationResult.Succeeded)
     {
         if (this.useBackupLeaseLocation)
         {
             this.logSession.LogEvent(EdgeSyncLoggingLevel.Low, EdgeSyncEvent.TargetConnection, null, "Switch back to primary lease from backup lease");
         }
         this.useBackupLeaseLocation = false;
         return(leaseOperationResult.ResultToken);
     }
     this.logSession.LogException(EdgeSyncLoggingLevel.Low, EdgeSyncEvent.TargetConnection, leaseOperationResult.Exception, "Failed to open primary lease file. Switch to backup lease file");
     leaseOperationResult = FileLeaseManager.TryRunLeaseOperation(new FileLeaseManager.LeaseOperation(FileLeaseManager.GetLeaseOperation), new FileLeaseManager.LeaseOperationRequest(this.backupLeaseFilePath));
     if (leaseOperationResult.Succeeded)
     {
         this.logSession.LogEvent(EdgeSyncLoggingLevel.Low, EdgeSyncEvent.TargetConnection, null, "Successfully failed over to backup lease file");
         this.useBackupLeaseLocation = true;
         return(leaseOperationResult.ResultToken);
     }
     this.logSession.LogException(EdgeSyncLoggingLevel.Low, EdgeSyncEvent.TargetConnection, leaseOperationResult.Exception, "Failed to open backup lease file");
     throw new ExDirectoryException(leaseOperationResult.Exception);
 }