Ejemplo n.º 1
0
        protected override void Process(Timer timer)
        {
            List <SMSQueue> queue = Context.GetSMSToUpdateStatus(Settings.Default.SMSCheckStatusBatchSize, DateTime.Now.Add(-Settings.Default.CheckStatusInterval)).ToList();

            foreach (SMSQueue sms in queue)
            {
                if (!string.IsNullOrEmpty(sms.SMSId))
                {
                    try
                    {
                        RoaminSMPP.SMPPCommunicator connection = SMPPPool.GetConnection(sms.ProviderId.Value, sms.source_addr);
                        SmppQuerySm queryPdu = new SmppQuerySm()
                        {
                            MessageId        = sms.SMSId,
                            SourceAddress    = sms.source_addr,
                            SourceAddressNpi = connection.NpiType,
                            SourceAddressTon = connection.TonType
                        };
                        connection.SendPdu(queryPdu);
                        lock (_syncLock)
                        {
                            if (!_sentMessages.ContainsKey(sms.SMSId))
                            {
                                _sentMessages.Add(sms.SMSId, sms.Id);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceWarning("Невозможно отправить сообщение с Id = {0}, ошибка: {1}", sms.Id, ex);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static void connection_OnDeliverSm(object source, RoaminSMPP.EventObjects.DeliverSmEventArgs e)
        {
            string networkMessage = string.Empty;

            try
            {
                networkMessage = e.DeliverSmPdu.NetworkErrorCode;
            }
            catch
            {
                networkMessage = e.DeliverSmPdu.ShortMessage;
            }
            RoaminSMPP.SMPPCommunicator connection = source as RoaminSMPP.SMPPCommunicator;
            if (connection != null)
            {
                if (DeliveryRecieptRecieved != null)
                {
                    DeliveryRecieptRecieved(e.DeliverSmPdu.ReceiptedMessageId, connection.ProviderId, e.DeliverSmPdu.SequenceNumber, e.DeliverSmPdu.MessageState, networkMessage);
                }
                SmppDeliverSmResp resp = new SmppDeliverSmResp()
                {
                    CommandStatus  = (uint)SmppCommandStatus.ESME_ROK,
                    SequenceNumber = e.DeliverSmPdu.SequenceNumber
                };
                connection.SendPdu(resp);
            }
        }
Ejemplo n.º 3
0
 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;
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        protected override void Process(Timer timer)
        {
            List <SMSQueue> queue = Context.GetSMSToSend(Settings.Default.SMSSendBatchSize).ToList();

            foreach (SMSQueue sms in queue)
            {
                try
                {
                    Trace.TraceInformation("Отправка сообщения {0}", sms.Id);
                    DestinationAddress[] dests = sms.DestinationMap.Select
                                                 (
                        item =>
                        item.IsDistributionList ?
                        new DestinationAddress(item.destination_addr) :
                        new DestinationAddress(
                            (Pdu.TonType)item.dest_addr_ton,
                            (Pdu.NpiType)item.dest_addr_npi,
                            item.destination_addr)
                                                 ).ToArray();
                    MessageLcd2 sendPdu;
                    if (sms.number_of_dests > 1)
                    {
                        RoaminSMPP.Packet.Request.SmppSubmitMulti multi = new RoaminSMPP.Packet.Request.SmppSubmitMulti();
                        multi.DestinationAddresses = dests;
                        sendPdu = multi;
                    }
                    else
                    {
                        DestinationAddress dest = dests[0];
                        RoaminSMPP.Packet.Request.SmppSubmitSm single = new RoaminSMPP.Packet.Request.SmppSubmitSm()
                        {
                            DestinationAddress    = dest.DestAddress,
                            DestinationAddressNpi = dest.DestinationAddressNpi,
                            DestinationAddressTon = dest.DestinationAddressTon
                        };
                        sendPdu = single;
                    }
                    Hashtable tlvTable = new Hashtable();
                    sms.SMSTLV.ToList().ForEach(item => tlvTable.Add(Convert.ToUInt16(item.Tag), Convert.FromBase64String(item.Value)));
                    sendPdu.TlvTable.tlvTable    = tlvTable;
                    sendPdu.SequenceNumber       = (uint)sms.sequence_number;
                    sendPdu.ServiceType          = sms.service_type;
                    sendPdu.SourceAddress        = sms.source_addr;
                    sendPdu.EsmClass             = (byte)sms.esm_class;
                    sendPdu.ProtocolId           = (Pdu.SmppVersionType)sms.protocol_id;
                    sendPdu.PriorityFlag         = (Pdu.PriorityType)sms.priority_flag;
                    sendPdu.ScheduleDeliveryTime = sms.schedule_delivery_time;
                    sendPdu.ValidityPeriod       = sms.validity_period;
                    sendPdu.RegisteredDelivery   = (Pdu.RegisteredDeliveryType)sms.registered_delivery;
                    sendPdu.ReplaceIfPresentFlag = sms.replace_if_present_flag >= 1 ? true : false;
                    sendPdu.DataCoding           = (Pdu.DataCodingType)sms.data_coding;
                    sendPdu.SmDefaultMessageId   = (byte)(sms.sm_default_msg_id ?? 0);
                    sendPdu.ShortMessage         = sms.short_message.GetBytesFromHex();
                    RoaminSMPP.SMPPCommunicator connection = SMPPPool.GetConnection(sms.ProviderId.Value, sms.source_addr);
                    sendPdu.SourceAddressNpi   = connection.NpiType;
                    sendPdu.SourceAddressTon   = connection.TonType;
                    sendPdu.RegisteredDelivery = (Pdu.RegisteredDeliveryType)sms.registered_delivery;
                    connection.SendPdu(sendPdu);
                    Context.GetStatusUpdater().UpdateSMSStatus(sms.Id, null, SMSStatus.Send, null, RoaminSMPP.Packet.Pdu.MessageStateType.Accepted);
                    lock (_syncLock)
                    {
                        _sentMessages.Add(sendPdu.SequenceNumber, sms.Id);
                    }
                }
                catch (Exception ex)
                {
                    //TODO: Реализовать логику RetryCount
                    Trace.TraceWarning("Невозможно отправить сообщение с Id = {0}, ошибка: {1}", sms.Id, ex);
                }
            }
        }