public AxSms.Message CreateAxSms() { AxSms.Message objResult = new AxSms.Message(); objResult.UserTag = UserTag; objResult.FromAddress = FromAddress; objResult.ToAddress = ToAddress; objResult.Body = Body; objResult.RequestDeliveryReport = RequestDeliveryReport; objResult.ToAddressNPI = ToAddressNpi; objResult.ToAddressTON = ToAddressTon; objResult.FromAddressNPI = FromAddressNpi; objResult.FromAddressTON = FromAddressTon; objResult.Reference = Reference; objResult.DataCoding = DataCoding; objResult.BodyFormat = BodyFormat; objResult.HasUdh = HasUdh; objResult.SmppIsDeliveryReport = IsDeliveryReport; objResult.SmppCommandStatus = CommandStatus; objResult.LanguageSingleShift = LanguageShift; objResult.LanguageLockingShift = LanguageShift; foreach (SimTlv objTlv in Tlvs) { AxSms.Tlv objAxTlv = new AxSms.Tlv(); objAxTlv.Tag = objTlv.Tag; objAxTlv.ValueAsHexString = objTlv.HexValue; objResult.SmppAddTlv(objAxTlv); } return(objResult); }
public SimMessage(AxSms.Message objMessage) { m_nUserTag = objMessage.UserTag; m_strFromAddress = objMessage.FromAddress; m_strToAddress = objMessage.ToAddress; m_strBody = objMessage.Body; m_bRequestDeliveryReport = objMessage.RequestDeliveryReport; m_nToAddressNpi = objMessage.ToAddressNPI; m_nToAddressTon = objMessage.ToAddressTON; m_nFromAddressNpi = objMessage.FromAddressNPI; m_nFromAddressTon = objMessage.FromAddressTON; m_strReference = objMessage.Reference; m_nDataCoding = objMessage.DataCoding; m_nBodyFormat = objMessage.BodyFormat; m_bHasUdh = objMessage.HasUdh; m_bIsDeliveryReport = objMessage.SmppIsDeliveryReport; m_nCommandStatus = objMessage.SmppCommandStatus; m_nSequenceNumber = objMessage.SmppSequenceNumber; m_nTotalParts = objMessage.TotalParts; m_nPartNumber = objMessage.PartNumber; m_nMultipartReference = objMessage.MultipartRef; m_nLanguageShift = objMessage.LanguageSingleShift; AxSms.Tlv objTlv = objMessage.SmppGetFirstTlv(); while (objMessage.LastError == 0) { m_lsTlvs.Add(new SimTlv(objTlv)); objTlv = objMessage.SmppGetNextTlv(); } }
private void btnShowComplete_Click(object sender, EventArgs e) { // Assemble from the parts we have m_objSmsAsm.ResetSmsAssembler(); foreach (SimMessage objMessage in m_lsParts) { AxSms.Message objAxMsg = objMessage.CreateAxSms(); m_objSmsAsm.AssembleSms(objAxMsg); } AxSms.Message objAssembled = m_objSmsAsm.GetAssembledSms(true); // Set all of the properties that may have changed m_bLoading = true; // make sure updatecontrols is not triggered txtBody.Text = objAssembled.Body; txtToAddress.Text = objAssembled.ToAddress; txtFromAddress.Text = objAssembled.FromAddress; cbxBodyFormat.SelectedValue = objAssembled.BodyFormat; cbUDH.Checked = objAssembled.HasUdh; m_bLoading = false; // Make sure character and parts count is set correctly UpdateCharacterCount(); // Disable this button; there's no going back now .. btnShowComplete.Enabled = false; }
public void UpdateCharacterCount() { // Don't let this function trigger while still filling the comboboxes in 'onLoad' if (m_bLoading) { return; } AxSms.Message objSms = new AxSms.Message(); objSms.ToAddress = "1234567890"; // make up a 'ToAddress' to pass validation objSms.BodyFormat = cbxBodyFormat.SelectedValue == null ? 0: (int)cbxBodyFormat.SelectedValue; objSms.DataCoding = cbxDataCoding.SelectedValue == null ? 0: (int)cbxDataCoding.SelectedValue; objSms.HasUdh = cbUDH.Checked; string strText = txtBody.Text; string strParts = ""; int nChars = 0; if (objSms.BodyFormat == m_objConstants.BODYFORMAT_HEX) { // Count SMS size based on HEX input strText = strText.Replace(" ", ""); strText = strText.Replace("\r", ""); strText = strText.Replace("\n", ""); nChars = strText.Length / 2; if ((strText.Length & 1) == 1) { strText = strText.Remove(strText.Length - 1); } } else { // Count SMS size based on regular text strText = strText.Replace("\r", ""); nChars = strText.Length; } // Count the message parts, only if we don't already know if (strParts == "") { objSms.Body = strText; int nParts = m_objSmsAsm.CountParts(objSms); int nLastError = m_objSmsAsm.LastError; strParts = nParts > 0 ? nParts.ToString() : "?"; } lblCharacters.Text = string.Format("{0} Characters / {1} Parts", nChars, strParts); }
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); } } }
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); } }