Esempio n. 1
0
        /// <summary>
        /// Override which adds the PDU to the string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(base.ToString());
            if (HasPDU && pdu_.IsValid)
            {
                sb.Append(" ");
                sb.Append(pdu_.ToString());
            }
            return(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// This method is called when data is received from our socket.
        /// </summary>
        /// <param name="sender">The socket which sent the data</param>
        /// <param name="args">Socket data</param>
        protected virtual void OnReceiveData(object sender, SocketEventArgs args)
        {
            SocketClient  socket       = (SocketClient)sender;
            ReadEventArgs re           = (ReadEventArgs)args;
            int           packetLength = SmppPdu.REQUIRED_SIZE;

            if (re.Length >= packetLength)
            {
                // Get the first DWORD from the buffer; this is the total size of the packet.
                packetLength = BitConverter.ToInt32(new byte[] { re.Buffer[3], re.Buffer[2], re.Buffer[1], re.Buffer[0] }, 0);
                if (re.Length >= packetLength)
                {
                    try
                    {
                        // Have the entire buffer; parse it out.
                        SmppPdu pdu = SmppPdu.Create(new SmppByteStream(re.Buffer));
                        SmppPduReceivedEventArgs ea = new SmppPduReceivedEventArgs(this, pdu);
                        FireEvent(EventType.PreProcessPdu, ea);

                        PduSyncronizer sync = null;
                        if (pdu is SmppResponse)
                        {
                            if ((sync = FindAndRemoveWaitingPdu(pdu.SequenceNumber)) != null)
                            {
                                ((SmppResponse)pdu).OriginalRequest = (SmppRequest)sync.PduRequest;
                            }
                            else
                            {
                                throw new SmppException("Invalid pdu response received with no pending request: " + pdu.ToString());
                            }
                        }

                        if (!ea.Handled)
                        {
                            try
                            {
                                ProcessPdu(pdu);
                            }
                            catch (InvalidSmppStateException)
                            {
                                if (pdu.RequiresResponse)
                                {
                                    SendPdu(new generic_nack(StatusCodes.ESME_RINVCMDID, pdu.SequenceNumber));
                                }
                            }
                        }

                        if (sync != null)
                        {
                            sync.PduResponse = pdu;
                        }
                    }
                    catch (PduException pduex)
                    {
                        SmppPdu pdu = (pduex.HasPDU) ? pduex.PDU : null;
                        if (pdu != null && pdu.RequiresResponse)
                        {
                            SendPdu(new generic_nack(StatusCodes.ESME_RINVCMDID, pdu.SequenceNumber));
                        }
                        FireEvent(EventType.Error, new SmppErrorEventArgs(this, pduex, pdu));
                    }
                    catch (Exception ex)
                    {
                        socket.Close(true);
                        FireEvent(EventType.SessionDisconnected, new SmppDisconnectEventArgs(this, ex));
                    }

                    // Reset the buffer
                    re.AppendToBuffer = false;
                    re.NextReadSize   = SmppPdu.REQUIRED_SIZE;
                    return;
                }
            }

            // Wait for more data to show up on the socket.
            re.AppendToBuffer = true;
            re.NextReadSize   = packetLength - re.Length;
        }