/** * Flushes the event buffer. * The buffer is flushed to an HTTP backend. * Failing to flush to the HTTP backend, the data will be flushed to a file. */ private static async Task FlushBufferThread(string username, List <string> buffer) { string bufferLines = "sessiontime(ms);event"; // Read lines from bufferfile first. DataReporter.ensureBufferFile(BUFFER_FILE); string[] fileLines = System.IO.File.ReadAllLines(BUFFER_FILE); bufferLines += "\n" + String.Join("\n", fileLines.Skip(1)); // Add all lines from the in-memory buffer. lock (buffer) { foreach (string line in buffer) { bufferLines += "\n" + line; } buffer.Clear(); } // Overwrite the buffer file with it's new contents. System.IO.File.WriteAllText(BUFFER_FILE, bufferLines); try { // Try to upload data to server. string url = string.Format("http://{0}/?app={1}&username={2}", DataReporter.REMOTE_IP, DataReporter.APP_ID, username); HttpClient client = new HttpClient(); HttpResponseMessage response = await client.PostAsync(url, new ByteArrayContent(Encoding.ASCII.GetBytes(bufferLines))); response.EnsureSuccessStatusCode(); // Clear the buffer file. System.IO.File.WriteAllText(BUFFER_FILE, "sessiontime(ms);event"); } catch (Exception e) { // Ignore all exceptions here, just log them. Debug.LogError("[DataReporter] Encountered an exception while attempting to upload the data. (" + e.Message + ")"); } }