// Остановка сервиса отправки сообщений // private bool StopSMPPService(bool showUnrun = false) { bool serviceStoped = true; if (clientSMS != null) { BackgroundSMPPPWorker.CancelAsync(); clientSMS.Disconnect(); clientSMS = null; GC.Collect(); GC.WaitForFullGCComplete(); Notification.AddEventToLog("Сервис отправки сообщений остановлен"); } else { if (showUnrun) { Notification.AddEventToLog("Сервис отправки сообщений не запущен"); } } CreateTrayIconText(); return(serviceStoped); }
// Проверяет возможно ли отправлять сообщения, если нет, то сервис отправки останавливается // private void SMPPService(object sender, DoWorkEventArgs e) { clientSMS = new SmsClient(settings.WriteSMPPLog); clientSMS.SQL = SQL; Notification.AddEventToLog("Запуск сервиса отправки сообщений..."); int sendCount = 0; int allSend = 0; int unsendCount = 0; System.Threading.Thread.Sleep(ParametersSMS.SMPP_CONNECTION_TIMEOUT); if (clientSMS.CanSend()) { Notification.AddEventToLog("Сервис отправки сообщений успешно запущен"); while (!BackgroundSMPPPWorker.CancellationPending) { List <SMS> messages = SQL.GetUnsentMessages(ParametersSMS.MAX_SMS_IN_PORTION); if (messages.Count > 0) { sendCount = 0; unsendCount = 0; Notification.AddEventToLog("Сообщений для отправки " + messages.Count.ToString()); DateTime beginSend = DateTime.Now; foreach (SMS currentSMS in messages) { /* * if (!SQL.SaveMessageUIN(currentSMS.id, currentSMS.id + 1)) * { * Notification.AddEventToLog("Не удалось сохранить uin сообщения: " + SQL.TextStatus, true); * unsendCount++; * } * else * { * if (!SQL.SaveMessageDeliveryDate(currentSMS.id + 1, 5, "Bla-bla-bla")) * { * Notification.AddEventToLog("Не удалось сохранить дату доставки сообщения: " + SQL.TextStatus, true); * unsendCount++; * } * else * { * sendCount++; * } * } */ if (clientSMS.SendSms(currentSMS.source, currentSMS.NumberTxt, currentSMS.text, currentSMS.id, true)) { sendCount++; } else { unsendCount++; } if (sendCount % ParametersSMS.MAX_SMS_PER_SECOND == 0) { // Соблюдение интервала между отправками DateTime endSend = DateTime.Now; TimeSpan sendTime = endSend - beginSend; int elapsedMillisecond = Convert.ToInt32(sendTime.TotalMilliseconds); if (elapsedMillisecond < 1000) { System.Threading.Thread.Sleep(1000 - elapsedMillisecond); } } if (sendCount % 1000 == 0) { LoadUnsentMessages(); } } if (sendCount > 0) { Notification.AddEventToLog(String.Format("Успешно отправлено {0} сообщений", sendCount)); } if (unsendCount > 0) { Notification.AddEventToLog(String.Format("Не удалось отправить {0} сообщений", unsendCount), true); } BackgroundSMPPPWorker.CancelAsync(); } else { LoadUnsentMessages(); messages = null; GC.Collect(); GC.WaitForFullGCComplete(); System.Threading.Thread.Sleep(ParametersSMS.SENDING_INTERVAL); } } } else { Notification.AddEventToLog("Не удалось запустить сервис отправки сообщений", true); BackgroundSMPPPWorker.CancelAsync(); clientSMS.Disconnect(); clientSMS = null; GC.Collect(); GC.WaitForFullGCComplete(); } }