public async Task <Response> Set(uint?cookie, byte[] data, uint?flag, TimeSpan?time, CancellationToken cancellationToken) { logger.Verbose("Set operation for {StateId} with {DataSize} bytes of data.", id, data?.Length); var r = new Response(); var(l, c) = await store.GetLockAsync(id, cancellationToken); var(d, f, t, m) = await store.GetDataAsync(id, cancellationToken); // expire session if required if ((m + t) - DateTime.UtcNow < TimeSpan.Zero) { await store.RemoveLockAsync(id, cancellationToken); await store.RemoveDataAsync(id, cancellationToken); // reload current state (l, c) = await store.GetLockAsync(id, cancellationToken); (d, f, t, m) = await store.GetDataAsync(id, cancellationToken); } // no data sent if (data == null) { r.Status = ResponseStatus.BadRequest; return(r); } // return lock information if present if (l != null) { r.LockCookie = l; r.LockTime = c; r.LockAge = DateTime.UtcNow - r.LockTime; } // flag is set if (d != null && flag == 1) { r.Status = ResponseStatus.Ok; return(r); } // data exists and is locked if (d != null && l != null && l != cookie) { r.Status = ResponseStatus.Locked; return(r); } // save new data await store.SetDataAsync(id, data, flag, time, cancellationToken); (d, f, t, m) = await store.GetDataAsync(id, cancellationToken); r.Status = ResponseStatus.Ok; r.Timeout = (m + t) - DateTime.UtcNow; // ensure we auto expire if (r.Timeout > TimeSpan.Zero) { await store.SetTimeoutAsync(id, r.Timeout, cancellationToken); } return(r); }
public async Task <Response> Set(uint?cookie, byte[] data, uint?flag, TimeSpan?time) { var r = new Response(); var(l, c) = await store.GetLockAsync(id); var(d, f, t, u) = await store.GetDataAsync(id); // expire session if required if ((u + t) - DateTime.Now < TimeSpan.Zero) { await store.RemoveLockAsync(id); await store.RemoveDataAsync(id); // reload current state (l, c) = await store.GetLockAsync(id); (d, f, t, u) = await store.GetDataAsync(id); } // no data sent if (data == null) { r.Status = ResponseStatus.BadRequest; return(r); } // return lock information if present if (l != null) { r.LockCookie = l; r.LockTime = c; r.LockAge = DateTime.Now - r.LockTime; } // flag is set if (d != null && flag == 1) { r.Status = ResponseStatus.Ok; return(r); } // data exists and is locked if (d != null && l != null && l != cookie) { r.Status = ResponseStatus.Locked; return(r); } // save new data await store.SetDataAsync(id, data, flag, time); (d, f, t, u) = await store.GetDataAsync(id); r.Status = ResponseStatus.Ok; r.Timeout = (u + t) - DateTime.Now; // ensure we auto expire if (r.Timeout > TimeSpan.Zero) { await store.SetTimeoutAsync(id, r.Timeout); } return(r); }