From 9371038450cabf2dc5abc77b8d0db4c8cebc9cbe Mon Sep 17 00:00:00 2001 From: Louis-Alexandre Duchesneau Date: Sat, 18 Nov 2023 09:45:02 -0500 Subject: [PATCH] OnAllPdu users are now able to return a bool to close the bind (#120) What: OnAllPdu setting did not allow users to report bind closing status. This caused an io error when users responded to a unbind request. Why: The change allow users to send back a bool to let the app close the bind, if needed, like with a unbind request. How: The OnAllPDU is called from handleOrClose function, that has a return value to close the bind. The OnAllPdu will also have a bool return value and that value is forward to the handleorClose function. If the OnCallPdu is returned with a true value, handleOrClose will initiate the close status, like it does without the OnAllPdu setting. This change also include some other minor changes: - using a Callback type, to align with existing code - no need to check if return value of OnAllPDU is nil. Its a struck, it will never be nil and all fields will have default values - fix manual response example to align with new return bool on OnAllPdu call --- .../transcceiver_with_manual_response/main.go | 16 +++++++--------- pkg.go | 14 ++++++++------ receivable.go | 9 ++++++--- types.go | 6 ++++++ 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/example/transcceiver_with_manual_response/main.go b/example/transcceiver_with_manual_response/main.go index 5bb9236..e5bf2a9 100644 --- a/example/transcceiver_with_manual_response/main.go +++ b/example/transcceiver_with_manual_response/main.go @@ -30,8 +30,6 @@ func sendingAndReceiveSMS(wg *sync.WaitGroup) { SystemType: "", } - sendResponse := make(chan pdu.PDU) - defer close(sendResponse) trans, err := gosmpp.NewSession( gosmpp.TRXConnector(gosmpp.NonTLSDialer, auth), gosmpp.Settings{ @@ -74,12 +72,12 @@ func sendingAndReceiveSMS(wg *sync.WaitGroup) { } -func handlePDU() func(pdu.PDU) pdu.PDU { - return func(p pdu.PDU) pdu.PDU { +func handlePDU() func(pdu.PDU) (pdu.PDU, bool) { + return func(p pdu.PDU) (pdu.PDU, bool) { switch pd := p.(type) { case *pdu.Unbind: fmt.Println("Unbind Received") - return pd.GetResponse() + return pd.GetResponse(), true case *pdu.UnbindResp: fmt.Println("UnbindResp Received") @@ -95,17 +93,17 @@ func handlePDU() func(pdu.PDU) pdu.PDU { case *pdu.EnquireLink: fmt.Println("EnquireLink Received") - return pd.GetResponse() + return pd.GetResponse(), false case *pdu.DataSM: fmt.Println("DataSM receiver") - return pd.GetResponse() + return pd.GetResponse(), false case *pdu.DeliverSM: fmt.Println("DeliverSM receiver") - return pd.GetResponse() + return pd.GetResponse(), false } - return nil + return nil, false } } diff --git a/pkg.go b/pkg.go index 8b7ab42..4a54f30 100644 --- a/pkg.go +++ b/pkg.go @@ -53,6 +53,14 @@ type Settings struct { // Will be ignored if OnAllPDU is set OnPDU PDUCallback + // OnAllPDU handles all received PDU from SMSC. + // + // This pdu is NOT responded to automatically, + // manual response/handling is needed + // + // User can also decide to close bind by retuning true, default is false + OnAllPDU AllPDUCallback + // OnReceivingError notifies happened error while reading PDU // from SMSC. OnReceivingError ErrorCallback @@ -66,11 +74,5 @@ type Settings struct { // OnClosed notifies `closed` event due to State. OnClosed ClosedCallback - // OnAllPDU handles all received PDU from SMSC. - // - // This pdu is NOT responded to automatically, - // manual response/handling is needed - OnAllPDU func(pdu pdu.PDU) pdu.PDU - response func(pdu.PDU) } diff --git a/receivable.go b/receivable.go index cf3a621..e64b826 100644 --- a/receivable.go +++ b/receivable.go @@ -108,9 +108,12 @@ func (t *receivable) loop() { func (t *receivable) handleOrClose(p pdu.PDU) (closing bool) { if p != nil { if t.settings.OnAllPDU != nil { - r := t.settings.OnAllPDU(p) - if r != nil { - t.settings.response(r) + r, closeBind := t.settings.OnAllPDU(p) + t.settings.response(r) + if closeBind { + time.Sleep(50 * time.Millisecond) + closing = true + t.closing(UnbindClosing) } return } diff --git a/types.go b/types.go index b602eb6..55f5231 100644 --- a/types.go +++ b/types.go @@ -5,6 +5,12 @@ import "github.com/linxGnu/gosmpp/pdu" // PDUCallback handles received PDU. type PDUCallback func(pdu pdu.PDU, responded bool) +// AllPDUCallback handles all received PDU. +// +// This pdu is NOT responded to automatically, manual response/handling is needed +// and the bind can be closed by retuning true on closeBind. +type AllPDUCallback func(pdu pdu.PDU) (responsePdu pdu.PDU, closeBind bool) + // PDUErrorCallback notifies fail-to-submit PDU with along error. type PDUErrorCallback func(pdu pdu.PDU, err error)