Skip to content

Commit

Permalink
Merge branch 'master' into add_OnRebind_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
laduchesneau authored Nov 18, 2023
2 parents 917fb50 + 9371038 commit 7223568
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,12 +74,6 @@ 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

// OnRebind notifies `rebind` event due to State.
OnRebind RebindCallback

Expand Down
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 7223568

Please sign in to comment.