private static void Pump(Platform.NetworkConnectionType connectionType) { //#if DEBUG //if (FoodJournal.ViewModels.ScreenshotVM.InScreenshot) // return; //#endif try { if (connectionType == Platform.NetworkConnectionType.None) { return; } if (InHere) { PumpAgain = true; SessionLog.RecordTrace("Attempting to sync while already syncing"); return; } InHere = true; PumpAgain = false; var messages = new List <Package> (); var working = new List <PendingPost> (); List <PendingPost> PostQueue = null; try { ThreadSync.WaitOne(); // Wait till we have a sync signal, and clear the signal PostQueue = state.PostQueue.Where(x => x.State == PendingPost.SyncState.pending || x.State == PendingPost.SyncState.retry).ToList(); } catch (Exception ex) { LittleWatson.ReportException(ex); } finally { ThreadSync.Set(); // Set the signal for the next thread to go if it comes to it } int totallength = 0; foreach (var item in PostQueue) { var pkg = item.ToPackage(); if (messages.Count > 0 && totallength + pkg.Contents.Length > TotalMessageSizeLimit) { PumpAgain = true; break; } else { messages.Add(pkg); working.Add(item); totallength += pkg.Contents.Length; System.Diagnostics.Debug.WriteLine(string.Format("Posting {0} {1} {2}", pkg.Type, pkg.Key, pkg.ClientTime)); } } if (messages.Count > 0) { try { SyncService.Post(messages); } catch (Exception ex) { ReportQueueException(ex); foreach (var i in working.ToArray()) { if (i.Retry >= MAXRETRY) { //i.State = PendingPost.SyncState.abandoned; state.PostQueue.Remove(i); } else { i.State = PendingPost.SyncState.retry; i.Retry++; working.Remove(i); } } } try { ThreadSync.WaitOne(); // Wait till we have a sync signal, and clear the signal foreach (var i in working) { state.PostQueue.Remove(i); } } catch (Exception ex) { LittleWatson.ReportException(ex); } finally { ThreadSync.Set(); // Set the signal for the next thread to go if it comes to it } SaveState(); } } catch (Exception ex) { ReportQueueException(ex); } InHere = false; if (PumpAgain) { StartPump(); } return; }
private static void Pump(Platform.NetworkConnectionType connectionType) { try { if (connectionType == Platform.NetworkConnectionType.None) { return; } if (InHere) { PumpAgain = true; SessionLog.RecordTrace("Attempting to pump while already pumping"); return; } InHere = true; PumpAgain = false; var messages = new List <Message> (); var infomessages = new List <String> (); int totallength = 0; foreach (var item in MessageQueueDB.SelectWhere(m => m.Processed == null)) { var msg = new Message() { Key = item.Id.ToString(), MessageType = item.MessageType, Body = item.Message }; if (totallength + msg.Body.Length > TotalMessageSizeLimit) { if (messages.Count == 0) { messages.Add(msg); infomessages.Add("Msg " + msg.Key + " truncated from " + msg.Body.Length.ToString()); msg.Body = msg.Body.Substring(0, TotalMessageSizeLimit); } PumpAgain = true; break; } else { messages.Add(msg); totallength += msg.Body.Length; #if ANDROID try { // 3/25/15: no more retry for large messages // trying this for Android first, if successfull we may as well apply to WinPhone if (msg.Body.Length > LargeMessage) { item.Processed = DateTime.Now; MessageQueueDB.Update(item); } } catch (Exception ex) { ReportQueueException(ex); } #endif } } if (messages.Count > 0) { foreach (var info in infomessages) { messages.Add(new Message() { Key = "0", MessageType = "Info", Body = info }); } var svc = Services.NewServiceClient(); svc.PushCompleted += (x, y) => { InHere = false; // mark committed messages as processed in the database if (y.Error != null) { ReportQueueException(y.Error); } else { if (y.Result != null) { try { foreach (var result in y.Result) { int i; if (result.Key.Length > 8) { ParseLongResult(result.Key); } else if (int.TryParse(result.Key, out i) && i > 0) { foreach (var m in MessageQueueDB.SelectWhere(m => m.Id == i)) { m.Processed = DateTime.Now; MessageQueueDB.Update(m); } } } } catch (Exception ex) { LittleWatson.ReportException(ex); } } } InHere = false; if (PumpAgain) { StartPump(); } else { SessionLog.EndPerformance("Push"); } }; var upd = AppStats.Current.CultureSettingsLastUpdated.ToString("yyyy/MM/dd"); svc.PushAsync(AppStats.Current.AppInstance, AppStats.Current.Culture, AppStats.Current.Version + "?" + upd, messages); return; } } catch (Exception ex) { ReportQueueException(ex); } InHere = false; return; }