Exemplo n.º 1
0
        void bw_AddOutgoing(SimMessage objMessage, Queue <SimMessage> qOut, List <SimMessage> lsGenerated, bool bCouldBeMultipart)
        {
            var lsParts = new List <SimMessage>();

            if (bCouldBeMultipart)
            {
                AxSms.Message objRoot = objMessage.CreateAxSms();
                if (m_objSplitSms.CountParts(objRoot) > 1)
                {
                    AxSms.Message objPart = m_objSplitSms.GetFirstPart(objRoot);
                    while (m_objSplitSms.LastError == 0)
                    {
                        lsParts.Add(new SimMessage(objPart));
                        objPart = m_objSplitSms.GetNextPart();
                    }
                }
            }

            if (lsParts.Count == 0)
            {
                lsParts.Add(objMessage);
            }

            if (qOut.Count > MAX_OUTGOING)
            {
                foreach (SimMessage objPart in lsParts)
                {
                    objPart.Status = "SIMQFULL";
                    lsGenerated.Add(objPart);
                }
            }
            else
            {
                foreach (SimMessage objPart in lsParts)
                {
                    objPart.Status  = "QUEUED";
                    objPart.UserTag = m_objSimModel.GetAndIncLastUserTag();
                    lsGenerated.Add(objPart);
                    qOut.Enqueue(objPart);
                }
            }
        }
Exemplo n.º 2
0
        private void bw_DoWork(BackgroundWorker objWorker, Logger objLogger, AxSms.SmppServer objServer)
        {
            if (m_objSimModel.IsServerLogEnabled)
            {
                objServer.LogFile = m_objSimModel.ServerLog;
            }

            AxSms.Constants objSmsConsts = new AxSms.Constants();

            objServer.LastReference = m_objSimModel.LastReference;
            objServer.Start(m_objSimModel.Port, m_objSimModel.IpVersion, m_objSimModel.Certificate);

            SimCommand objCommand = new SimCommand();

            objCommand.CommandId            = SimCommand.ECommandId.RESPONSECODE;
            objCommand.LastError            = objServer.LastError;
            objCommand.LastErrorDescription = objServer.GetErrorDescription(objServer.LastError);
            m_objSimModel.IsServerStarted   = objServer.LastError == 0;
            PushGui(objCommand);

            if (objServer.LastError != 0)
            {
                objLogger.WriteLine("SmppServer, failed: [{0}:{1}]", objCommand.LastError, objCommand.LastErrorDescription);
                return;
            }
            else
            {
                objLogger.WriteLine("SmppServer started");
            }

            // Initialize Smpp instance to do SMS splitting
            // This is very convenient because we don't need to use the orginal SmppSession object
            m_objSplitSms = new AxSms.Smpp();
            m_objSplitSms.MultipartMode  = m_objSimModel.MultipartMode;
            m_objSplitSms.UseGsmEncoding = m_objSimModel.UseGsmEncoding;

            // Replies
            long          nLastReloadReplies = GetTickCount();
            bool          bEcho         = m_objSimModel.Echo;
            SimErrorRates objErrorRates = m_objSimModel.GetErrorRates();

            // Auto messages
            long nLastReloadAutoMsg = GetTickCount();
            var  lsAutoMessages     = new List <SimMessage>();

            lsAutoMessages = m_objSimModel.GetAutoMessages();
            int    nGeneratePerMinute = m_objSimModel.GeneratePerMinute;
            bool   bRandomOrder       = m_objSimModel.RandomOrder;
            int    nAutoMsgIndex      = 0;
            Random objRandom          = new Random();

            var dctWrkSessions = new Dictionary <int, WrkSession>();

            while (!objWorker.CancellationPending)
            {
                var lsMessagesUpdated   = new List <SimMessage>();
                var lsMessagesGenerated = new List <SimMessage>();
                var lsMessagesReceived  = new List <SimMessage>();

                // Check if the GUI has some requests
                while ((objCommand = PollGui()) != null)
                {
                    WrkSession objWrkSession = null;
                    dctWrkSessions.TryGetValue(objCommand.SessionId, out objWrkSession);
                    switch (objCommand.CommandId)
                    {
                    case SimCommand.ECommandId.DROPSESSION:
                        if (objWrkSession != null)
                        {
                            objWrkSession.bDrop = true;
                        }
                        break;

                    case SimCommand.ECommandId.SENDMESSAGE:
                        if (objWrkSession != null)
                        {
                            bw_AddOutgoing(objCommand.Message, objWrkSession.qOutMessages, lsMessagesGenerated, true);
                        }
                        break;
                    }
                }

                // Quit if the server stopped for some reason
                if (!objServer.IsStarted)
                {
                    m_objSimModel.IsServerStarted = false;
                    break;
                }

                // Reset statistics count
                int nRecvPerSecond = 0, nSentPerSecond = 0;
                // Handle connected sessions
                var lsSessions               = new List <SimSession>();
                AxSms.SmppSession objSession = objServer.GetFirstSession();
                while (objServer.LastError == 0)
                {   // Creating objSimSession object early and only access it's properties since
                    // accessing ActiveX properties (and methods) seems to be expensive in C#
                    SimSession objSimSession = new SimSession(objSession);
                    lsSessions.Add(objSimSession);

                    // Get the current worker session
                    WrkSession objWrkSession = null;
                    if (!dctWrkSessions.ContainsKey(objSession.Id))
                    {
                        dctWrkSessions[objSession.Id] = objWrkSession = new WrkSession(objSimSession, GetTickCount());
                    }
                    else
                    {
                        objWrkSession = dctWrkSessions[objSession.Id];
                    }

                    // Handle session bind requests
                    if (objSimSession.ConnectionState == objSmsConsts.SMPP_SESSIONSTATE_BINDING)
                    {
                        // Respond to a bind required. If authentication is required test sysid
                        int iResponse = 0;
                        if (m_objSimModel.IsAuthRequired &&
                            (objSimSession.SystemId != m_objSimModel.SystemId ||
                             objSimSession.Password != m_objSimModel.Password))
                        {
                            iResponse = objSmsConsts.SMPP_ESME_RINVPASWD;
                        }
                        objSession.RespondToBind(iResponse);

                        // Generate a logfile path and set it
                        if (m_objSimModel.IsSessionLogEnabled)
                        {
                            string strLogIp;
                            if (objSimSession.Ip.Contains("."))
                            {   // IPv4; np
                                strLogIp = objSimSession.Ip;
                            }
                            else
                            {   // IPv6, can't have ':''s in a filename !
                                strLogIp = string.Format("[{0}]", objSimSession.Ip).Replace(':', '_');
                            }

                            string strLogPath = string.Format("{0}{1}_{2}.log",
                                                              m_objSimModel.SessionLog, strLogIp, objSimSession.Port);
                            objSession.LogPduDetails = m_objSimModel.IsPduLogEnabled;
                            objSession.LogFile       = strLogPath;
                        }

                        // Apply server settings
                        objSession.PduTimeout      = m_objSimModel.PduTimeout;
                        objSession.EnquireInterval = m_objSimModel.EnquireInterval;
                        objSession.UseGsmEncoding  = m_objSimModel.UseGsmEncoding;
                        objSession.DeliverMode     = m_objSimModel.DeliverMode;
                    }

                    // Generate auto-messages.
                    int iNumMessages = objWrkSession.CalculateMessagesToSend(GetTickCount(), nGeneratePerMinute);
                    if (lsAutoMessages.Count > 0)
                    {
                        for (int i = 0; i < iNumMessages; i++)
                        {   // Determine the message index of the auto-message to sent
                            if (bRandomOrder)
                            {
                                nAutoMsgIndex = objRandom.Next(lsAutoMessages.Count);
                            }
                            else
                            {
                                if (nAutoMsgIndex < lsAutoMessages.Count - 1)
                                {
                                    nAutoMsgIndex++;
                                }
                                else
                                {
                                    nAutoMsgIndex = 0;
                                }
                            }

                            // If there's room in the output queue, add it to the output queue
                            // othewise, reject the message, queue full..
                            SimMessage objMessage = new SimMessage(lsAutoMessages[nAutoMsgIndex]);
                            objMessage.SessionId = objSimSession.Id;
                            objMessage.SystemId  = objSimSession.SystemId;
                            bw_AddOutgoing(objMessage, objWrkSession.qOutMessages, lsMessagesGenerated, true);
                        }
                    }

                    // Sent SMS messages in sent queue
                    int nOut = objSession.CountSmsDeliverySpace();
                    while (objWrkSession.qOutMessages.Count > 0 && nOut > 0)
                    {
                        SimMessage    objSimMessage = objWrkSession.qOutMessages.Dequeue();
                        AxSms.Message objMessage    = objSimMessage.CreateAxSms();

                        // reject multipart messages because they should all have been splitup before beeing
                        // added to the output queue.
                        objSession.DeliverSms(objMessage, objSmsConsts.MULTIPART_REJECT);
                        if (objSession.LastError != 0)
                        {
                            objSimMessage.Status = string.Format("SUBMITERROR({0})", objSession.LastError);
                        }
                        else
                        {
                            objSimMessage.Status = "DELIVERED";
                        }

                        lsMessagesUpdated.Add(objSimMessage);
                    }

                    // Fetch updates for messages that have been sent
                    AxSms.Message objResponse = objSession.ReceiveDeliverResponse();
                    while (objSession.LastError == 0)
                    {
                        SimMessage objSimMessage = new SimMessage(objResponse);
                        objSimMessage.SystemId  = objSimSession.SystemId;
                        objSimMessage.SessionId = objSimSession.Id;
                        objSimMessage.Status    = objSmsConsts.SmppEsmeToString(objSimMessage.CommandStatus).Replace("SMPP_ESME_", "");
                        lsMessagesUpdated.Add(objSimMessage);
                        objResponse = objSession.ReceiveDeliverResponse();
                    }

                    // Fetch received messages
                    AxSms.Message objReceived = objSession.ReceiveSubmitSms();
                    while (objSession.LastError == 0)
                    {
                        SimMessage objSimMessage = new SimMessage(objReceived);
                        objSimMessage.SystemId  = objSimSession.SystemId;
                        objSimMessage.SessionId = objSimSession.Id;

                        // Determine command status
                        int nCommandStatus     = 0;
                        int nRandCmdStatus     = objRandom.Next(100);
                        int nRandCmdStatusOffs = 0;
                        foreach (SimMessageErrorRate objErrr in objErrorRates.MessageErrorRates)
                        {
                            nRandCmdStatusOffs += objErrr.Occurance;
                            if (nRandCmdStatus <= nRandCmdStatusOffs)
                            {
                                nCommandStatus = objErrr.StatusCode;
                                break;
                            }
                        }

                        // Respond to SMS message
                        objReceived.SmppCommandStatus = objSimMessage.CommandStatus = nCommandStatus;
                        objSession.RespondToSubmitSms(objReceived);

                        // Set message status text and add it to the 'received' list
                        // generate a usertag for received messages as well. The GUI relies on every message to have a unique ID
                        objSimMessage.Status  = objSmsConsts.SmppEsmeToString(objSimMessage.CommandStatus).Replace("SMPP_ESME_", "");
                        objSimMessage.UserTag = m_objSimModel.GetAndIncLastUserTag();
                        lsMessagesReceived.Add(objSimMessage);

                        // Determine command status
                        if (objSimMessage.RequestDeliveryReport && nCommandStatus == 0)
                        {
                            int    nRandDlrStatus     = objRandom.Next(100);
                            int    nRandDlrStatusOffs = 0;
                            int    nStatusCode        = objSmsConsts.SMPP_MESSAGESTATE_DELIVERED;
                            string strStatusText      = "DELIVRD";
                            foreach (SimDeliveryErrorRate objErrr in objErrorRates.DeliveryErrorRates)
                            {
                                nRandDlrStatusOffs += objErrr.Occurance;
                                if (nRandDlrStatus <= nRandDlrStatusOffs)
                                {
                                    nStatusCode   = objErrr.Code;
                                    strStatusText = objErrr.Text;
                                    break;
                                }
                            }
                            SimMessage objDlr = bw_CreateDlr(objSimMessage, objSimSession.Version, nStatusCode, strStatusText, objSmsConsts);
                            bw_AddOutgoing(objDlr, objWrkSession.qOutMessages, lsMessagesGenerated, false);
                        }

                        if (bEcho)
                        {
                            SimMessage objEcho = new SimMessage(objSimMessage);
                            objEcho.FromAddress    = objSimMessage.ToAddress;
                            objEcho.FromAddressNpi = objSimMessage.ToAddressNpi;
                            objEcho.FromAddressTon = objSimMessage.ToAddressTon;
                            objEcho.ToAddress      = objSimMessage.FromAddress;
                            objEcho.ToAddressNpi   = objSimMessage.FromAddressNpi;
                            objEcho.ToAddressTon   = objSimMessage.FromAddressTon;
                            bw_AddOutgoing(objEcho, objWrkSession.qOutMessages, lsMessagesGenerated, false);
                        }

                        objReceived = objSession.ReceiveSubmitSms();
                    }

                    // Don't support SMS queries
                    objReceived = objSession.ReceiveQuerySms();
                    while (objSession.LastError == 0)
                    {   // Just immediately respond with an error.
                        objReceived.SmppCommandStatus = objSmsConsts.SMPP_ESME_RINVCMDID;
                        objSession.RespondToQuerySms(objReceived);
                        objReceived = objSession.ReceiveQuerySms();
                    }

                    if (objWrkSession.bDrop)
                    {
                        objSession.Disconnect();
                    }

                    // Update statistics
                    nRecvPerSecond += objSession.SmsReceivedPerSecond;
                    nSentPerSecond += objSession.SmsSentPerSecond;

                    objSession = objServer.GetNextSession();
                }

                // Make a list of disconnected sessions
                var lsDisconnected = new List <SimSession>();
                objSession = objServer.GetClosedSession();
                while (objServer.LastError == 0)
                {
                    SimSession objSimSession = new SimSession(objSession);
                    lsDisconnected.Add(objSimSession);
                    dctWrkSessions.Remove(objSimSession.Id);
                    objSession = objServer.GetClosedSession();
                }

                // Set global stats
                m_objSimModel.RecvPerSecond = nRecvPerSecond;
                m_objSimModel.SentPerSecond = nSentPerSecond;
                m_objSimModel.LastReference = objServer.LastReference;

                // See if there's a need to sent the delta messages to the GUI
                if (lsMessagesUpdated.Count > 0 ||
                    lsMessagesReceived.Count > 0 ||
                    lsMessagesGenerated.Count > 0)
                {
                    objCommand                   = new SimCommand();
                    objCommand.CommandId         = SimCommand.ECommandId.MESSAGESDELTA;
                    objCommand.MessagesUpdated   = lsMessagesUpdated;
                    objCommand.MessagesReceived  = lsMessagesReceived;
                    objCommand.MessagesGenerated = lsMessagesGenerated;
                    PushGui(objCommand);
                }

                if (lsDisconnected.Count > 0)
                {
                    objCommand           = new SimCommand();
                    objCommand.CommandId = SimCommand.ECommandId.DROPSESSION;
                    objCommand.Sessions  = lsDisconnected;
                    PushGui(objCommand);
                }

                // Don't continuously refresh the auto-messages. They are not updated that often and
                // a small lag here is not very noticable.
                if (GetTickCount() - nLastReloadAutoMsg > AUTOMESSAGE_RELOAD_TOMS)
                {
                    nLastReloadAutoMsg = GetTickCount();
                    lsAutoMessages     = m_objSimModel.GetAutoMessages();
                    nGeneratePerMinute = m_objSimModel.GeneratePerMinute;
                    bRandomOrder       = m_objSimModel.RandomOrder;
                }

                // Don't continuously refresh reply settings, but do refresh them a bit
                // more often than the autoreply settings because its very visible.
                if (GetTickCount() - nLastReloadReplies > REPLIES_RELOAD_TOMS)
                {
                    nLastReloadReplies = GetTickCount();
                    bEcho         = m_objSimModel.Echo;
                    objErrorRates = m_objSimModel.GetErrorRates();
                }

                m_objSimModel.SetSessions(lsSessions);

                Thread.Sleep(100);
            }
        }