diff --git a/example/transcceiver_with_manual_response/main.go b/example/transcceiver_with_manual_response/main.go new file mode 100644 index 0000000..5bb9236 --- /dev/null +++ b/example/transcceiver_with_manual_response/main.go @@ -0,0 +1,134 @@ +package main + +import ( + "fmt" + "log" + "sync" + "time" + + "github.com/linxGnu/gosmpp" + "github.com/linxGnu/gosmpp/data" + "github.com/linxGnu/gosmpp/pdu" +) + +func main() { + var wg sync.WaitGroup + + wg.Add(1) + go sendingAndReceiveSMS(&wg) + + wg.Wait() +} + +func sendingAndReceiveSMS(wg *sync.WaitGroup) { + defer wg.Done() + + auth := gosmpp.Auth{ + SMSC: "localhost:2775", + SystemID: "169994", + Password: "EDXPJU", + SystemType: "", + } + + sendResponse := make(chan pdu.PDU) + defer close(sendResponse) + trans, err := gosmpp.NewSession( + gosmpp.TRXConnector(gosmpp.NonTLSDialer, auth), + gosmpp.Settings{ + EnquireLink: 5 * time.Second, + + ReadTimeout: 10 * time.Second, + + OnSubmitError: func(_ pdu.PDU, err error) { + log.Fatal("SubmitPDU error:", err) + }, + + OnReceivingError: func(err error) { + fmt.Println("Receiving PDU/Network error:", err) + }, + + OnRebindingError: func(err error) { + fmt.Println("Rebinding but error:", err) + }, + + OnAllPDU: handlePDU(), + + OnClosed: func(state gosmpp.State) { + fmt.Println(state) + }, + }, 5*time.Second) + if err != nil { + log.Fatal(err) + } + defer func() { + _ = trans.Close() + }() + + // sending SMS(s) + for i := 0; i < 30; i++ { + if err = trans.Transceiver().Submit(newSubmitSM()); err != nil { + fmt.Println(err) + } + time.Sleep(time.Second) + } + +} + +func handlePDU() func(pdu.PDU) pdu.PDU { + return func(p pdu.PDU) pdu.PDU { + switch pd := p.(type) { + case *pdu.Unbind: + fmt.Println("Unbind Received") + return pd.GetResponse() + + case *pdu.UnbindResp: + fmt.Println("UnbindResp Received") + + case *pdu.SubmitSMResp: + fmt.Println("SubmitSMResp Received") + + case *pdu.GenericNack: + fmt.Println("GenericNack Received") + + case *pdu.EnquireLinkResp: + fmt.Println("EnquireLinkResp Received") + + case *pdu.EnquireLink: + fmt.Println("EnquireLink Received") + return pd.GetResponse() + + case *pdu.DataSM: + fmt.Println("DataSM receiver") + return pd.GetResponse() + + case *pdu.DeliverSM: + fmt.Println("DeliverSM receiver") + return pd.GetResponse() + } + return nil + } +} + +func newSubmitSM() *pdu.SubmitSM { + // build up submitSM + srcAddr := pdu.NewAddress() + srcAddr.SetTon(5) + srcAddr.SetNpi(0) + _ = srcAddr.SetAddress("00" + "522241") + + destAddr := pdu.NewAddress() + destAddr.SetTon(1) + destAddr.SetNpi(1) + _ = destAddr.SetAddress("99" + "522241") + + submitSM := pdu.NewSubmitSM().(*pdu.SubmitSM) + submitSM.SourceAddr = srcAddr + submitSM.DestAddr = destAddr + _ = submitSM.Message.SetMessageWithEncoding("Đừng buồn thế dù ngoài kia vẫn mưa nghiễng rợi tý tỵ", data.UCS2) + submitSM.ProtocolID = 0 + submitSM.RegisteredDelivery = 1 + submitSM.ReplaceIfPresentFlag = 0 + submitSM.EsmClass = 0 + + return submitSM +} diff --git a/example/main.go b/example/transceiver_with_auto_response/main.go similarity index 100% rename from example/main.go rename to example/transceiver_with_auto_response/main.go diff --git a/pkg.go b/pkg.go index c13fdf4..8b7ab42 100644 --- a/pkg.go +++ b/pkg.go @@ -49,6 +49,8 @@ type Settings struct { // // `Responded` flag indicates this pdu is responded automatically, // no manual respond needed. + // + // Will be ignored if OnAllPDU is set OnPDU PDUCallback // OnReceivingError notifies happened error while reading PDU @@ -64,5 +66,11 @@ 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 2e46a6d..4269ccf 100644 --- a/receivable.go +++ b/receivable.go @@ -107,6 +107,13 @@ 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) + } + return + } switch pp := p.(type) { case *pdu.EnquireLink: if t.settings.response != nil { diff --git a/transceivable.go b/transceivable.go index 27c76ec..042b0fa 100644 --- a/transceivable.go +++ b/transceivable.go @@ -50,6 +50,8 @@ func newTransceivable(conn *Connection, settings Settings) *transceivable { OnPDU: settings.OnPDU, + OnAllPDU: settings.OnAllPDU, + OnReceivingError: settings.OnReceivingError, OnClosed: func(state State) {