Implement a basic smpp client
All checks were successful
Run Tests / Test (push) Successful in 17s
Benchmark BufferPool / RunBenchmarks (push) Successful in 31s

This commit is contained in:
2024-07-31 22:59:53 +02:00
parent 38af7b146d
commit 4d7e13a304
3 changed files with 71 additions and 37 deletions

View File

@@ -1 +1,62 @@
package client
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
}