static void connectionTimer_Elapsed(object sender, ElapsedEventArgs e) { if (sender is KeyedTimer <string> ) { KeyedTimer <string> timer = sender as KeyedTimer <string>; lock (_syncLock) { if (_connections.ContainsKey(timer.Key)) { ConnectionItem item = _connections[timer.Key]; if (!item.IsRunning) { try { RoaminSMPP.SMPPCommunicator conn = _connections[timer.Key].Connection; if (conn.IsBinded) { conn.SendPdu(new SmppEnquireLink()); } else { conn.Bind(); } } catch (Exception ex) { Trace.TraceWarning("Ошибка соединения с поставщиком: {0}", ex); item.ReconnectAttempts++; if (item.ReconnectAttempts > Settings.Default.MaxReconnectAttempts) { item.ConnectionRefreshTimer.Enabled = false; item.ConnectionRefreshTimer.Elapsed -= connectionTimer_Elapsed; _connections.Remove(timer.Key); item.ConnectionRefreshTimer.Dispose(); item.Connection.Dispose(); } } finally { item.IsRunning = false; } } } } } }
public static RoaminSMPP.SMPPCommunicator GetConnection(Guid providerId, string senderName) { RoaminSMPP.SMPPCommunicator connection = null; string key = GenerateKey(providerId, senderName); lock (_syncLock) { if (_connections.ContainsKey(key)) { connection = _connections[key].Connection; } else { Providers provider = Context.GetProvider(providerId).FirstOrDefault(); if (provider != null) { ProviderConfiguration conf = _providerConfigurationSerializer.Deserialize <ProviderConfiguration>(provider.Configuration); TonNpiPair senderPlan = conf.SourceNumberings.GetNpiTonPair(senderName); if (senderPlan == null) { throw new InvalidOperationException(Resources.Error_NumberTypeNotSupported); } KeyedTimer <string> connectionTimer = new KeyedTimer <string>(conf.EnqureLinkInterval, key); connectionTimer.Elapsed += new ElapsedEventHandler(connectionTimer_Elapsed); connection = new RoaminSMPP.SMPPCommunicator() { BindType = conf.BindingTypes.FirstOrDefault(), AddressRange = conf.AddressRange, Version = conf.SupportedSMPPVersions.Max(), Host = conf.Host, Port = (short)conf.Port, EnquireLinkInterval = (int)conf.EnqureLinkInterval.TotalSeconds, TonType = senderPlan.Ton, NpiType = senderPlan.Npi, SystemType = conf.SystemType, SystemId = conf.SystemId, Password = conf.Password, SleepTimeAfterSocketFailure = (int)Settings.Default.SleepTimeAfterSocketFailure.TotalSeconds, Username = conf.UserName, ProviderId = provider.Id }; connection.OnSubmitMultiResp += new RoaminSMPP.SMPPCommunicator.SubmitMultiRespEventHandler(connection_OnSubmitMultiResp); connection.OnSubmitSmResp += new RoaminSMPP.SMPPCommunicator.SubmitSmRespEventHandler(connection_OnSubmitSmResp); connection.OnQuerySmResp += new RoaminSMPP.SMPPCommunicator.QuerySmRespEventHandler(connection_OnQuerySmResp); connection.OnDeliverSm += new RoaminSMPP.SMPPCommunicator.DeliverSmEventHandler(connection_OnDeliverSm); connection.OnError += new RoaminSMPP.SMPPCommunicator.ErrorEventHandler(connection_OnError); _connections.Add(key, new ConnectionItem { Connection = connection, ConnectionRefreshTimer = connectionTimer, ReconnectAttempts = 0 }); try { connection.Bind(); } catch (Exception ex) { Trace.TraceError(ex.ToString()); } finally { connectionTimer.Enabled = true; } } } } return(connection); }