Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions smpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package smpp

// Imports
import (
"os"
"net"
"bufio"
"strconv"
"fmt"
"errors"
)

// Used for all outbound connections
Expand All @@ -25,9 +24,9 @@ type smpp struct {
}

// Connect to server
func (smpp *smpp) connect(host string, port int) (err os.Error) {
func (smpp *smpp) connect(host string, port int) (err error) {
// Create TCP connection
smpp.conn, err = net.Dial("tcp", "", host + ":" + strconv.Itoa(port))
smpp.conn, err = net.Dial("tcp", host + ":" + strconv.Itoa(port))
if err != nil {
return
}
Expand All @@ -39,14 +38,16 @@ func (smpp *smpp) connect(host string, port int) (err os.Error) {
}

// Close connection
func (smpp *smpp) close() (err os.Error) {
err = smpp.conn.Close()
smpp.connected = false
func (smpp *smpp) close() (err error) {
if(smpp != nil && smpp.conn != nil){
err = smpp.conn.Close()
smpp.connected = false
}
return
}

// Send bind request (called via NewTransmitter/NewReceiver/NewTransceiver) always synchronous
func (smpp *smpp) bind(cmd, rcmd SMPPCommand, params Params) (err os.Error) {
func (smpp *smpp) bind(cmd, rcmd SMPPCommand, params Params) (err error) {
// Sequence number starts at 1
smpp.sequence ++
// PDU header
Expand All @@ -61,7 +62,7 @@ func (smpp *smpp) bind(cmd, rcmd SMPPCommand, params Params) (err os.Error) {
paramOK := false
defer func() {
if !paramOK && recover() != nil {
err = os.NewError("Bind: Panic, invalid params")
err = errors.New("Bind: Panic, invalid params")
return
}
}()
Expand Down Expand Up @@ -97,10 +98,10 @@ func (smpp *smpp) Async(async bool) {
}

// Send unbind request
func (smpp *smpp) Unbind() (sequence uint32, err os.Error) {
func (smpp *smpp) Unbind() (sequence uint32, err error) {
// Check connected and bound
if !smpp.connected || !smpp.bound {
err = os.NewError("Unbind: A bound connection is required to unbind")
err = errors.New("Unbind: A bound connection is required to unbind")
return
}
// Increment sequence number
Expand Down Expand Up @@ -129,11 +130,11 @@ func (smpp *smpp) Unbind() (sequence uint32, err os.Error) {
}

// Get response PDU
func (smpp *smpp) GetResp(cmd SMPPCommand, sequence uint32) (rpdu PDU, err os.Error) {
func (smpp *smpp) GetResp(cmd SMPPCommand, sequence uint32) (rpdu PDU, err error) {
// Read the header
hdr := new(PDUHeader)
err = hdr.read(smpp.reader)
fmt.Printf("Header: %#v\n", hdr)
//fmt.Printf("Header: %#v\n", hdr)
if err != nil {
return nil, err
}
Expand All @@ -148,17 +149,17 @@ func (smpp *smpp) GetResp(cmd SMPPCommand, sequence uint32) (rpdu PDU, err os.Er
}()
// Check cmd and/or sequence if not 0
if cmd != CMD_NONE && hdr.CmdId != cmd {
err = os.NewError("Get Response: Invalid command")
err = errors.New("Get Response: Invalid command")
return nil, err
}
// Check sequence number if not 0
if sequence > 0 && hdr.Sequence != sequence {
err = os.NewError("Get Response: Invalid sequence number")
err = errors.New("Get Response: Invalid sequence number")
return nil, err
}
// Check for error response
if hdr.CmdStatus != STATUS_ESME_ROK {
err = os.NewError("Get Response: PDU contains an error")
err = errors.New("Get Response: PDU contains an error")
return nil, err
}
// Set PDU as read (to disable the deferred read)
Expand All @@ -167,7 +168,7 @@ func (smpp *smpp) GetResp(cmd SMPPCommand, sequence uint32) (rpdu PDU, err os.Er
switch hdr.CmdId {
// Default unhandled PDU
default:
err = os.NewError("Get Response: Unknown or unhandled PDU received")
err = errors.New("Get Response: Unknown or unhandled PDU received")
return nil, err
// Bind responses
case CMD_BIND_RECEIVER_RESP, CMD_BIND_TRANSMITTER_RESP, CMD_BIND_TRANSCEIVER_RESP:
Expand Down Expand Up @@ -211,7 +212,7 @@ func (smpp *smpp) GetResp(cmd SMPPCommand, sequence uint32) (rpdu PDU, err os.Er
}

// Create a new Transmitter
func NewTransmitter(host string, port int, params Params) (tx *Transmitter, err os.Error) {
func NewTransmitter(host string, port int, params Params) (tx *Transmitter, err error) {
// Merge params with defaults
allParams := mergeParams(params, defaultsBind)
// Create new transmitter
Expand All @@ -236,7 +237,7 @@ func NewTransmitter(host string, port int, params Params) (tx *Transmitter, err
}

// Create a new Receiver
func NewReceiver(host string, port int, params Params) (rx *Receiver, err os.Error) {
func NewReceiver(host string, port int, params Params) (rx *Receiver, err error) {
// Merge params with defaults
allParams := mergeParams(params, defaultsBind)
// Create new receiver
Expand All @@ -261,7 +262,7 @@ func NewReceiver(host string, port int, params Params) (rx *Receiver, err os.Err
}

// Create a new Transceiver
func NewTransceiver(host string, port int, params Params) (trx *Transceiver, err os.Error) {
func NewTransceiver(host string, port int, params Params) (trx *Transceiver, err error) {
// Merge params with defaults
allParams := mergeParams(params, defaultsBind)
// Create new receiver
Expand Down
4 changes: 2 additions & 2 deletions smpp_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var (
defaultsBind = Params{"systemId": "", "password": "", "systemType": "", "addrTon": SMPPTypeOfNumber(TON_UNKNOWN), "addrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "addressRange": ""}

// SubmitSM defaults
defaultsSubmitSM = Params{"serviceType": "", "sourceAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "sourceAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "sourceAddr": "", "destAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "destAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "esmClass": SMPPEsmClassESME(ESME_MSG_MODE_DEFAULT), "protocolId": uint8(0x00), "priorityFlag": SMPPPriority(PRIORITY_NORMAL), "schedDelTime": "", "validityPeriod": "", "regDelivery": SMPPDelivery(DELIVERY_NONE), "replaceFlag": uint8(0x00), "dataCoding": SMPPDataCoding(CODING_LATIN1), "smDefaultMsgId": uint8(0x00)}
defaultsSubmitSM = Params{"serviceType": "", "sourceAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "sourceAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "sourceAddr": "", "destAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "destAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "esmClass": SMPPEsmClassESME(ESME_MSG_MODE_DEFAULT), "protocolId": uint8(0x00), "priorityFlag": SMPPPriority(PRIORITY_NORMAL), "schedDelTime": "", "validityPeriod": "", "regDelivery": SMPPDelivery(DELIVERY_NONE), "replaceFlag": uint8(0x00), "dataCoding": SMPPDataCoding(CODING_DEFAULT), "smDefaultMsgId": uint8(0x00)}

// SubmitMulti defaults
defaultsSubmitMulti = Params{"serviceType": "", "sourceAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "sourceAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "sourceAddr": "", "destAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "destAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "esmClass": SMPPEsmClassESME(ESME_MSG_MODE_DEFAULT), "protocolId": uint8(0x00), "priorityFlag": SMPPPriority(PRIORITY_NORMAL), "schedDelTime": "", "validityPeriod": "", "regDelivery": SMPPDelivery(DELIVERY_NONE), "replaceFlag": uint8(0x00), "dataCoding": SMPPDataCoding(CODING_LATIN1), "smDefaultMsgId": uint8(0x00)}
defaultsSubmitMulti = Params{"serviceType": "", "sourceAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "sourceAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "sourceAddr": "", "destAddrTon": SMPPTypeOfNumber(TON_UNKNOWN), "destAddrNpi": SMPPNumericPlanIndicator(NPI_UNKNOWN), "esmClass": SMPPEsmClassESME(ESME_MSG_MODE_DEFAULT), "protocolId": uint8(0x00), "priorityFlag": SMPPPriority(PRIORITY_NORMAL), "schedDelTime": "", "validityPeriod": "", "regDelivery": SMPPDelivery(DELIVERY_NONE), "replaceFlag": uint8(0x00), "dataCoding": SMPPDataCoding(CODING_DEFAULT), "smDefaultMsgId": uint8(0x00)}
)

// Params definitions
Expand Down
Loading