예제 #1
0
        /// <summary>
        /// Sends message to a remote SMPP server
        /// </summary>
        /// <param name="message">A message to send</param>
        /// <param name="timeOut">A value in miliseconds after which the send operation times out</param>
        public void SendMessage(ShortMessage message, int timeOut)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            //Check if connection is open
            if (vState != SmppConnectionState.Connected)
            {
                throw new SmppClientException("Sending message operation failed because the SmppClient is not connected");
            }

            string messageId = null;

            foreach (SendSmPDU pdu in message.GetMessagePDUs(vProperties.DefaultEncoding, vSmppEncodingService))
            {
                if (_Log.IsDebugEnabled)
                {
                    _Log.DebugFormat("SendMessage SendSmPDU: {0}", LoggingExtensions.DumpString(pdu, vSmppEncodingService));
                }
                ResponsePDU resp         = SendPdu(pdu, timeOut);
                var         submitSmResp = resp as SubmitSmResp;
                if (submitSmResp != null)
                {
                    if (_Log.IsDebugEnabled)
                    {
                        _Log.DebugFormat("SendMessage Response: {0}", LoggingExtensions.DumpString(resp, vSmppEncodingService));
                    }
                    messageId = ((SubmitSmResp)resp).MessageID;
                }
                message.ReceiptedMessageId = messageId;
                RaiseMessageSentEvent(message);
            }
        }
예제 #2
0
        private void PduReceivedEventHander(object sender, PduReceivedEventArgs e)
        {
            //This handler is interested in SingleDestinationPDU only
            SingleDestinationPDU pdu = e.Request as SingleDestinationPDU;

            if (pdu == null)
            {
                return;
            }

            if (_Log.IsDebugEnabled)
            {
                _Log.DebugFormat("Received PDU: {0}", LoggingExtensions.DumpString(pdu, vSmppEncodingService));
            }

            if (vTraceSwitch.TraceVerbose)
            {
                Trace.WriteLine(string.Format("PduReceived: RequestType: {0}", e.Request?.GetType()?.Name));
            }
            ShortMessage message = null;

            try { message = MessageFactory.CreateMessage(pdu); }
            catch (SmppException smppEx)
            {
                _Log.ErrorFormat("200019:SMPP message decoding failure - {0} - {1} {2}", smppEx, smppEx.ErrorCode, new ByteBuffer(pdu.GetBytes()).DumpString(), smppEx.Message);
                if (vTraceSwitch.TraceError)
                {
                    Trace.WriteLine(string.Format(
                                        "200019:SMPP message decoding failure - {0} - {1} {2};",
                                        smppEx.ErrorCode, new ByteBuffer(pdu.GetBytes()).DumpString(), smppEx.Message));
                }
                //Notify the SMSC that we encountered an error while processing the message
                e.Response = pdu.CreateDefaultResponce();
                e.Response.Header.ErrorCode = smppEx.ErrorCode;
                return;
            }
            catch (Exception ex)
            {
                _Log.ErrorFormat("200019:SMPP message decoding failure - {0}", ex, new ByteBuffer(pdu.GetBytes()).DumpString());
                if (vTraceSwitch.TraceError)
                {
                    Trace.WriteLine(string.Format(
                                        "200019:SMPP message decoding failure - {0} {1};",
                                        new ByteBuffer(pdu.GetBytes()).DumpString(), ex.Message));
                }
                //Let the receiver know that this message was rejected
                e.Response = pdu.CreateDefaultResponce();
                e.Response.Header.ErrorCode = SmppErrorCode.ESME_RX_P_APPN; //ESME Receiver Reject Message
                return;
            }

            if (message != null && _Log.IsDebugEnabled)
            {
                _Log.DebugFormat("PduReceived: message: {0}", LoggingExtensions.DumpString(message, vSmppEncodingService));
            }

            if (vTraceSwitch.TraceVerbose)
            {
#if DEBUG
                Console.WriteLine(string.Format("PduReceived: pdu: Header:{0}, EsmClass:{1}, ServiceType:{2}, DataCoding:{3}", pdu.Header, pdu.EsmClass, pdu.ServiceType, pdu.DataCoding));
#endif
                Trace.WriteLine(string.Format("PduReceived: pdu: Header:{0}, EsmClass:{1}, ServiceType:{2}, DataCoding:{3}", pdu.Header, pdu.EsmClass, pdu.ServiceType, pdu.DataCoding));
                if (message != null)
                {
                    Trace.WriteLine(string.Format("PduReceived: message: DestinationAddress:{0}, MessageCount:{1}, ReceiptedMessageId:{2}, RegisterDeliveryNotification:{3}, SegmentID:{4}, SequenceNumber:{5}, SourceAddress:{6}, UserMessageReference:{7}",
                                                  message.DestinationAddress, message.MessageCount, message.ReceiptedMessageId, message.RegisterDeliveryNotification, message.SegmentID, message.SequenceNumber, message.SourceAddress, message.UserMessageReference));
                }
            }
            //If we have just a normal message
            if ((((byte)pdu.EsmClass) | 0xc3) == 0xc3)
            {
                RaiseMessageReceivedEvent(message);
            }
            //Or if we have received a delivery receipt
            else if ((pdu.EsmClass & EsmClass.DeliveryReceipt) == EsmClass.DeliveryReceipt)
            {
                // Extract receipted message id
                message.ReceiptedMessageId = pdu.GetOptionalParamString(Tag.receipted_message_id);
                // Extract receipted message state
                message.MessageState = pdu.GetOptionalParamByte <MessageState>(Tag.message_state);
                // Extract receipted network error code
                message.NetworkErrorCode = pdu.GetOptionalParamBytes(Tag.network_error_code);
                // Extract user message reference
                message.UserMessageReference = pdu.GetOptionalParamString(Tag.user_message_reference);
                RaiseMessageDeliveredEvent(message);
            }
        }