Exemplo n.º 1
0
        /// <summary>
        /// Callback method to handle socket closing.
        /// </summary>
        /// <param name="client">The client to receive messages from.</param>
        internal void ClientCloseHandler(AsyncSocketClient client)
        {
            lock (_bindingLock)
            {
                _Log.Warn("Socket closed, scheduling a rebind operation.");
                _ReBindRequired = true;
            }

            DispatchOnClose(new EventArgs());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Callback method to handle received messages.  The AsyncSocketClient
 /// library calls this; don't call it yourself.
 /// </summary>
 /// <param name="client">The client to receive messages from.</param>
 internal void ClientMessageHandler(AsyncSocketClient client)
 {
     try
     {
         // OPTIMIZE: Use a single PduFactory instance, instead of a new one each time.
         Queue responseQueue = new PduFactory().GetPduQueue(client.Buffer);
         ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessPduQueue), responseQueue);
     }
     catch (Exception exception)
     {
         DispatchOnError(new CommonErrorEventArgs(exception));
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Callback method to handle received messages.  The AsyncSocketClient
 /// library calls this; don't call it yourself.
 /// </summary>
 /// <param name="client">The client to receive messages from.</param>
 internal void ClientMessageHandler(AsyncSocketClient client)
 {
     try
     {
         // OPTIMIZE: Use a single PduFactory instance, instead of a new one each time.
         Queue responseQueue = new PduFactory().GetPduQueue(client.Buffer);
         ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessPduQueue), responseQueue);
     }
     catch (Exception exception)
     {
         DispatchOnError(new CommonErrorEventArgs(exception));
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Callback method to handle errors.
 /// </summary>
 /// <param name="client">The client to receive messages from.</param>
 /// <param name="exception">The generated exception.</param>
 internal void ClientErrorHandler(AsyncSocketClient client, Exception exception)
 {
     DispatchOnError(new CommonErrorEventArgs(exception));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Callback method to handle socket closing.
        /// </summary>
        /// <param name="client">The client to receive messages from.</param>
        internal void ClientCloseHandler(AsyncSocketClient client)
        {
            lock (_bindingLock)
            {
                _Log.Warn("Socket closed, scheduling a rebind operation.");
                _ReBindRequired = true;
            }

            DispatchOnClose(new EventArgs());
        }
Exemplo n.º 6
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;
        }
Exemplo n.º 7
0
 /// <summary>
 /// Callback method to handle errors.
 /// </summary>
 /// <param name="client">The client to receive messages from.</param>
 /// <param name="exception">The generated exception.</param>
 internal void ClientErrorHandler(AsyncSocketClient client, Exception exception)
 {
     DispatchOnError(new CommonErrorEventArgs(exception));
 }
Exemplo n.º 8
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);
        }