Skip to content

Commit

Permalink
OnAllPdu users are now able to return a bool to close the bind (#120)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
laduchesneau authored Nov 18, 2023
1 parent 7bfd2f8 commit 9371038
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
16 changes: 7 additions & 9 deletions example/transcceiver_with_manual_response/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}

Expand Down
14 changes: 8 additions & 6 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
9 changes: 6 additions & 3 deletions receivable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9371038

Please sign in to comment.