|
|
@ -143,43 +143,12 @@ namespace LocalServices.Data
|
|
|
|
return JsonSerializer.Deserialize<Dictionary<K, V>>(text);
|
|
|
|
return JsonSerializer.Deserialize<Dictionary<K, V>>(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void Save<K, T>(string fileName, Dictionary<K, T> dict)
|
|
|
|
private void Save<K, T>(string fileName, Dictionary<K, T> dict)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string json = JsonSerializer.Serialize(dict);
|
|
|
|
string json = JsonSerializer.Serialize(dict);
|
|
|
|
using (var stream = WaitForFile(dbPath + "/" + fileName, FileMode.Open, FileAccess.Write, FileShare.Write))
|
|
|
|
Debug.WriteLine(json);
|
|
|
|
{
|
|
|
|
|
|
|
|
var bytes = Encoding.ASCII.GetBytes(json);
|
|
|
|
File.WriteAllText(dbPath + "/" + fileName, json);
|
|
|
|
await stream.WriteAsync(bytes, 0, bytes.Length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This is a workaround function to wait for a file to be released before opening it.
|
|
|
|
|
|
|
|
// This was to fix the Save method that used to throw sometimes as the file were oftenly being scanned by the androids' antivirus.
|
|
|
|
|
|
|
|
// Simply wait until the file is released and return it. This function will never return until
|
|
|
|
|
|
|
|
private static FileStream WaitForFile(string fullPath, FileMode mode, FileAccess access, FileShare share)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int attempt = 0 ; attempt < 40; attempt++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FileStream? fs = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fs = new FileStream(fullPath, mode, access, share);
|
|
|
|
|
|
|
|
return fs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (FileNotFoundException e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Debug.WriteLine(e.Message + " in thread " + Thread.CurrentThread.Name + " " + Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
|
|
|
if (fs != null)
|
|
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new TimeoutException("Could not access file '" + fullPath + "', maximum attempts reached.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|