Example #1
0
        protected override void ReadModifyWriteState(Action <SmartSyncState> act)
        {
            int retries         = 5;
            int sleepIntervalMs = 1000;

            for (; ;)
            {
                var current = _counter.GetCurrent();
                var success = _counter.IncrementWithAction(current, () =>
                {
                    var state = ReadState();
                    act(state);
                    var nextStateFile = _stateFile.FullName + ".next";
                    using (var fs = new FileStream(nextStateFile, FileMode.Create, FileAccess.Write))
                    {
                        SmartSyncState.WriteToStream(fs, state);
                    }
                    File.Replace(nextStateFile, _stateFile.FullName, null, true);
                });
                if (success)
                {
                    return;
                }
                else
                {
                    if (retries < 0)
                    {
                        throw new System.IO.IOException("Unable to update after several retries.");
                    }
                    System.Threading.Thread.Sleep(sleepIntervalMs);
                    sleepIntervalMs *= 2;
                    retries--;
                }
            }
        }
Example #2
0
 public static void WriteToStream(Stream strm, SmartSyncState state)
 {
     using (var xwr = XmlWriter.Create(strm, settings))
     {
         seralizer.WriteObject(xwr, state);
     }
 }
Example #3
0
 public static void WriteToStream(Stream strm,SmartSyncState state)
 {
     using (var xwr = XmlWriter.Create(strm, settings))
     {
         seralizer.WriteObject(xwr, state);
     }
 }
Example #4
0
 protected override SmartSyncState ReadState()
 {
     using (var fs = new FileStream(_stateFile.FullName, FileMode.Open, FileAccess.Read))
     {
         var ret = SmartSyncState.ReadFromStream(fs);
         return(ret);
     }
 }
 public override bool Initialize(bool force = false)
 {
     var ret =  _counter.Init();
        var mode = force ? FileMode.Create : FileMode.CreateNew;
        if (force || ret)
        {
        using (var fs = new FileStream(_stateFile.FullName, mode, FileAccess.Write))
        {
            var initState = new SmartSyncState();
            SmartSyncState.WriteToStream(fs, initState);
        }
        }
        return ret;
 }
Example #6
0
        public override bool Initialize(bool force = false)
        {
            var ret  = _counter.Init();
            var mode = force ? FileMode.Create : FileMode.CreateNew;

            if (force || ret)
            {
                using (var fs = new FileStream(_stateFile.FullName, mode, FileAccess.Write))
                {
                    var initState = new SmartSyncState();
                    SmartSyncState.WriteToStream(fs, initState);
                }
            }
            return(ret);
        }