62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"smpptester/pdu"
|
|
)
|
|
|
|
type SMPPClient struct {
|
|
Id int
|
|
conn net.Conn
|
|
port string
|
|
log log.Logger
|
|
}
|
|
|
|
func NewSMPPClient(port string, id int) *SMPPClient {
|
|
client := &SMPPClient{
|
|
Id: id,
|
|
port: port,
|
|
log: log.Logger{},
|
|
}
|
|
client.log = *log.New(log.Writer(), "", log.LstdFlags)
|
|
client.log.SetPrefix(fmt.Sprintf("SMPP client %d: ", client.Id))
|
|
return client
|
|
}
|
|
|
|
func (c *SMPPClient) Connect() error {
|
|
conn, err := net.Dial("tcp", c.port)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to SMPP server: %w", err)
|
|
}
|
|
c.log.Printf("SMPP client %d connected to %s", c.Id, c.port)
|
|
c.conn = conn
|
|
return nil
|
|
}
|
|
|
|
func (c *SMPPClient) Close() {
|
|
c.conn.Close()
|
|
c.log.Printf("SMPP client %d closed connection", c.Id)
|
|
}
|
|
|
|
func (c *SMPPClient) Send(pdata pdu.PDU) error {
|
|
if c.conn == nil {
|
|
return fmt.Errorf("connection is not established")
|
|
}
|
|
|
|
pdata.UpdateSize()
|
|
buf := pdu.ByteBufferPool.Get(pdata.Size())
|
|
err := pdata.Encode(buf)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to encode PDU: %w", err)
|
|
}
|
|
|
|
_, err = c.conn.Write(buf.Bytes())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to send PDU: %w", err)
|
|
}
|
|
|
|
c.log.Printf("SMPP client %d sent PDU: %+v", c.Id, pdata)
|
|
return nil
|
|
} |