Exemplo n.º 1
0
        /// <summary>
        /// Fires an event off based on what Pdu is sent in.
        /// </summary>
        /// <param name="response">The response to fire an event for.</param>
        private void FireEvents(Pdu response)
        {
            //понеслась...
            if (response is SmppBindResp)
            {
                SmppBindResp resp = (SmppBindResp)response;
                if (resp.CommandStatus == (uint)SmppCommandStatus.ESME_ROK)
                {
                    _isBinded = true; //Мы сопоставлены, все отлично
                }
                if (OnBindResp != null)
                {
                    OnBindResp(this, new BindRespEventArgs(resp));
                }
            }
            else if (response is SmppUnbindResp)
            {
                _isBinded = false;
                //Отсоединяемся
                asClient.Disconnect();
                if (OnUnboundResp != null)
                {
                    OnUnboundResp(this, new UnbindRespEventArgs((SmppUnbindResp)response));
                }
            }
            else if (response is SmppAlertNotification)
            {
                if (OnAlert != null)
                {
                    OnAlert(this, new AlertEventArgs((SmppAlertNotification)response));
                }
            }
            else if (response is SmppSubmitSmResp)
            {
                if (OnSubmitSmResp != null)
                {
                    OnSubmitSmResp(this,
                                   new SubmitSmRespEventArgs((SmppSubmitSmResp)response));
                }
            }
            else if (response is SmppEnquireLinkResp)
            {
                if (OnEnquireLinkResp != null)
                {
                    OnEnquireLinkResp(this, new EnquireLinkRespEventArgs((SmppEnquireLinkResp)response));
                }
            }
            else if (response is SmppSubmitSm)
            {
                if (OnSubmitSm != null)
                {
                    OnSubmitSm(this, new SubmitSmEventArgs((SmppSubmitSm)response));
                }
                else
                {
                    //Нет отбработчика, значит сами
                    SmppSubmitSmResp pdu = new SmppSubmitSmResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    pdu.MessageId      = System.Guid.NewGuid().ToString().Substring(0, 10);
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RSUBMITFAIL;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }

                    SendPdu(pdu);
                }
            }
            else if (response is SmppQuerySm)
            {
                if (OnQuerySm != null)
                {
                    OnQuerySm(this, new QuerySmEventArgs((SmppQuerySm)response));
                }
                else
                {
                    //Нет обработчика, отдаем пустой ответ "незнаю"
                    SmppQuerySmResp pdu = new SmppQuerySmResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RQUERYFAIL;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }
                    SendPdu(pdu);
                }
            }
            else if (response is SmppGenericNack)
            {
                if (OnGenericNack != null)
                {
                    OnGenericNack(this, new GenericNackEventArgs((SmppGenericNack)response));
                }
            }
            else if (response is SmppEnquireLink)
            {
                if (OnEnquireLink != null)
                {
                    OnEnquireLink(this, new EnquireLinkEventArgs((SmppEnquireLink)response));
                }

                //Ответ, что с соединением все хорошо. Ответ взад
                SmppEnquireLinkResp pdu = new SmppEnquireLinkResp();
                pdu.SequenceNumber = response.SequenceNumber;
                pdu.CommandStatus  = (uint)SmppCommandStatus.ESME_ROK;
                SendPdu(pdu);
            }
            else if (response is SmppUnbind)
            {
                if (OnUnbind != null)
                {
                    OnUnbind(this, new UnbindEventArgs((SmppUnbind)response));
                }
                else
                {
                    //Нет обработчика, сами отвечаем
                    SmppUnbindResp pdu = new SmppUnbindResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    pdu.CommandStatus  = (uint)SmppCommandStatus.ESME_ROK;
                    SendPdu(pdu);
                }
                //Отключаемся, не сопряжены
                asClient.Disconnect();
                _isBinded = false;
            }
            else if (response is SmppBind)
            {
                SmppBind bindCommand = (SmppBind)response;
                if (_serverMode)
                {
                    this._AddressRange = bindCommand.AddressRange;
                    this._BindType     = bindCommand.BindType;
                    this._NpiType      = bindCommand.AddressNpi;
                    this._TonType      = bindCommand.AddressTon;
                    this._Version      = bindCommand.InterfaceVersion;
                    this._SystemType   = bindCommand.SystemType;
                    this._SystemId     = bindCommand.SystemId;
                    this._Password     = bindCommand.Password;
                }
                if (OnBind != null)
                {
                    BindEventArgs bindEA = new BindEventArgs(bindCommand);
                    OnBind(this, bindEA);
                    _isBinded = bindEA.IsBindSucessfull;
                }
                else
                {
                    //Если нет обработчика, то все плохо, возвращаем стандартный ответ
                    SmppBindResp pdu = new SmppBindResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RALYBND;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RBINDFAIL;
                        }
                        _isBinded = false;
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                        _isBinded         = true;
                    }
                    pdu.SystemId = "Generic";
                    SendPdu(pdu);
                }
            }
            else if (response is SmppCancelSm)
            {
                if (OnCancelSm != null)
                {
                    OnCancelSm(this, new CancelSmEventArgs((SmppCancelSm)response));
                }
                else
                {
                    //Нет обработчика - стандартный ответ "незнаю"
                    SmppCancelSmResp pdu = new SmppCancelSmResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RCANCELFAIL;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }
                    SendPdu(pdu);
                }
            }
            else if (response is SmppCancelSmResp)
            {
                if (OnCancelSmResp != null)
                {
                    OnCancelSmResp(this, new CancelSmRespEventArgs((SmppCancelSmResp)response));
                }
            }
            else if (response is SmppCancelSmResp)
            {
                if (OnCancelSmResp != null)
                {
                    OnCancelSmResp(this, new CancelSmRespEventArgs((SmppCancelSmResp)response));
                }
            }
            else if (response is SmppQuerySmResp)
            {
                if (OnQuerySmResp != null)
                {
                    OnQuerySmResp(this, new QuerySmRespEventArgs((SmppQuerySmResp)response));
                }
            }
            else if (response is SmppDataSm)
            {
                if (OnDataSm != null)
                {
                    OnDataSm(this, new DataSmEventArgs((SmppDataSm)response));
                }
                else
                {
                    //Нет обработчика, стандартный ответ
                    SmppDataSmResp pdu = new SmppDataSmResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RSUBMITFAIL;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }
                    pdu.MessageId = "Generic";

                    SendPdu(pdu);
                }
            }
            else if (response is SmppDataSmResp)
            {
                if (OnDataSmResp != null)
                {
                    OnDataSmResp(this, new DataSmRespEventArgs((SmppDataSmResp)response));
                }
            }
            else if (response is SmppDeliverSm)
            {
                if (OnDeliverSm != null)
                {
                    OnDeliverSm(this, new DeliverSmEventArgs((SmppDeliverSm)response));
                }
                else
                {
                    //Нет обработчика - стандартный ответ
                    SmppDeliverSmResp pdu = new SmppDeliverSmResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RDELIVERYFAILURE;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }
                    SendPdu(pdu);
                }
            }
            else if (response is SmppDeliverSmResp)
            {
                if (OnDeliverSmResp != null)
                {
                    OnDeliverSmResp(this, new DeliverSmRespEventArgs((SmppDeliverSmResp)response));
                }
            }
            else if (response is SmppReplaceSm)
            {
                if (OnReplaceSm != null)
                {
                    OnReplaceSm(this, new ReplaceSmEventArgs((SmppReplaceSm)response));
                }
                else
                {
                    //Нет обработчика - стандартный ответ
                    SmppReplaceSmResp pdu = new SmppReplaceSmResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RREPLACEFAIL;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }
                    SendPdu(pdu);
                }
            }
            else if (response is SmppReplaceSmResp)
            {
                if (OnReplaceSmResp != null)
                {
                    OnReplaceSmResp(this, new ReplaceSmRespEventArgs((SmppReplaceSmResp)response));
                }
            }
            else if (response is SmppSubmitMulti)
            {
                if (OnSubmitMulti != null)
                {
                    OnSubmitMulti(this, new SubmitMultiEventArgs((SmppSubmitMulti)response));
                }
                else
                {
                    //Нет обработчика - стандартный ответ
                    SmppSubmitMultiResp pdu = new SmppSubmitMultiResp();
                    pdu.SequenceNumber = response.SequenceNumber;
                    if (_serverMode)
                    {
                        if (_isBinded)
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RSUBMITFAIL;
                        }
                        else
                        {
                            pdu.CommandStatus = (uint)SmppCommandStatus.ESME_RINVBNDSTS;
                        }
                    }
                    else
                    {
                        pdu.CommandStatus = (uint)SmppCommandStatus.ESME_ROK;
                    }
                    SendPdu(pdu);
                }
            }
            else if (response is SmppSubmitMultiResp)
            {
                if (OnSubmitMultiResp != null)
                {
                    OnSubmitMultiResp(this, new SubmitMultiRespEventArgs((SmppSubmitMultiResp)response));
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Sets up the BindEventArgs.
 /// </summary>
 /// <param name="response">The SmppBindResp.</param>
 internal BindEventArgs(SmppBind response) : base(response)
 {
     _response = response;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Connects and binds the SMPPCommunicator to the SMSC, using the
        /// values that have been set in the constructor and through the
        /// properties.  This will also start the timer that sends enquire_link packets
        /// at regular intervals, if it has been enabled.
        /// </summary>
        /// <remarks>
        /// Can be used only in client mode
        /// </remarks>
        public void Bind()
        {
            if (!_serverMode)
            {
                try
                {
                    if (asClient != null)
                    {
                        asClient.Disconnect();
                    }
                }
                catch
                {
                    //drop it on the floor
                }

                //connect
                try
                {
                    asClient = new AsyncSocketClient(10240, null,
                                                     new AsyncSocketClient.MessageHandler(ClientMessageHandler),
                                                     new AsyncSocketClient.SocketClosingHandler(ClientCloseHandler),
                                                     new AsyncSocketClient.ErrorHandler(ClientErrorHandler));

                    asClient.Connect(Host, Port);

                    SmppBind request = new SmppBind();
                    request.SystemId         = SystemId;
                    request.Password         = Password;
                    request.SystemType       = SystemType;
                    request.InterfaceVersion = Version;
                    request.AddressTon       = TonType;
                    request.AddressNpi       = NpiType;
                    request.AddressRange     = AddressRange;
                    request.BindType         = BindType;

                    SendPdu(request);
                    _SentUnbindPacket = false;
                    _isBinded         = true;

                    if (_EnquireLinkInterval > 0)
                    {
                        if (timer == null)
                        {
                            timer          = new System.Timers.Timer();
                            timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
                        }

                        if (timer != null)              //reset the old timer
                        {
                            timer.Stop();

                            timer.Interval = EnquireLinkInterval * 1000;
                            timer.Start();
                        }
                    }
                }
                catch (Exception exc)
                {
                    if (OnError != null)
                    {
                        OnError(this, new CommonErrorEventArgs(exc));
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Connects and binds the SMPPCommunicator to the SMSC, using the
        /// values that have been set in the constructor and through the
        /// properties.  This will also start the timers that at regular intervals
        /// send enquire_link packets and the one which re-binds the session, if their
        /// interval properties have a non-zero value.
        /// </summary>
        /// <returns>
        /// True if bind was successfull or false if connection/bind failed
        /// (and will be retried later upon ReBindInterval)
        /// </returns>
        public bool Bind()
        {
            lock (_bindingLock)
            {
                _Log.DebugFormat("Binding to {0}:{1}.", Host, Port);

                if (_Bound)
                {
                    throw new InvalidOperationException("Already bound to remote party, unbind session first!");
                }

                _ReBindTimer.Stop();                 // (temporarilly) disable re-binding timer.

                try
                {
                    if (asClient != null)
                    {
                        var tmp = asClient;
                        asClient = null;
                        tmp.Dispose();
                    }
                }
                catch
                {
                    //drop it on the floor
                }

                //connect
                try
                {
                    asClient = new AsyncSocketClient(10240, null,
                                                     new AsyncSocketClient.MessageHandler(ClientMessageHandler),
                                                     new AsyncSocketClient.SocketClosingHandler(ClientCloseHandler),
                                                     new AsyncSocketClient.ErrorHandler(ClientErrorHandler));

                    asClient.Connect(Host, Port);

                    // re-initialize seq. numbers.
                    lock (this) _SequenceNumber = 1;
                    // SequenceNumbers are per-session, so reset waiting list..
                    lock (_RequestsAwaitingResponse) _RequestsAwaitingResponse.Clear();

                    SmppBind request = new SmppBind();
                    request.SystemId         = SystemId;
                    request.Password         = Password;
                    request.SystemType       = SystemType;
                    request.InterfaceVersion = Version;
                    request.AddressTon       = TonType;
                    request.AddressNpi       = NpiType;
                    request.AddressRange     = AddressRange;
                    request.BindType         = BindType;

                    var response = SendRequest(request);

                    if (response.CommandStatus != 0)
                    {
                        throw new SmppRemoteException("Bind request failed.", response.CommandStatus);
                    }

                    _SentUnbindPacket = false;

                    // Enable/Disable enquire timer.
                    _EnquireLinkTimer.Stop();

                    if (_EnquireLinkInterval > 0)
                    {
                        _EnquireLinkTimer.Interval = _EnquireLinkInterval * 1000;
                        _EnquireLinkTimer.Start();
                    }

                    _Bound          = true;
                    _ReBindRequired = false;

                    return(true);
                }
                catch (Exception exc)
                {
                    DispatchOnError(new BindErrorEventArgs(exc));
                }
                finally
                {
                    // Re-enable rebinding timer..

                    if (_ReBindInterval > 0)
                    {
                        _ReBindTimer.Interval = _ReBindInterval * 1000;
                        _ReBindTimer.Start();
                    }
                }
            }

            return(false);
        }