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

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
jstester/node_modules
encoding/tmp
pdu/tmp
tmp

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
}

44
main.go
View File

@@ -4,9 +4,9 @@ import (
"bytes"
"encoding/binary"
"log"
"net"
"sync"
"smpptester/client"
"smpptester/pdu"
)
@@ -166,47 +166,19 @@ func main() {
buf := pdu.ByteBufferPool.Get(submit.Size())
defer pdu.ByteBufferPool.Put(buf)
submit.Encode(buf)
log.Println(buf.Bytes())
log.Println(createSubmitSMPDU())
// log.Println(pdu.Encode())
// serviceType := "CMT"
// sourceAddrTON := byte(0x01)
// sourceAddrNPI := byte(0x01)
// sourceAddr := "12345"
// destAddrTON := byte(0x01)
// destAddrNPI := byte(0x01)
// destinationAddr := "67890"
// esmClass := byte(0x00)
// protocolID := byte(0x00)
// priorityFlag := byte(0x00)
// scheduleDeliveryTime := ""
// validityPeriod := ""
// registeredDelivery := byte(0x01)
// replaceIfPresentFlag := byte(0x00)
// dataCoding := byte(0x01)
// smDefaultMsgID := byte(0x00)
// shortMessage := "Hello, SMPP!"
wg := &sync.WaitGroup{}
wg.Add(1)
conn, err := net.Dial("tcp", "localhost:2775")
client := client.NewSMPPClient("localhost:2775", 1)
err := client.Connect()
if err != nil {
log.Printf("Failed to connect to SMPP server: %+v", err)
return
log.Fatalf("Failed to connect to SMPP server: %v", err)
}
err = client.Send(submit)
if err != nil {
log.Fatalf("Failed to send PDU: %v", err)
}
log.Printf("Connected to SMPP server")
defer conn.Close()
go func() {
conn.Write(buf.Bytes())
data, err := conn.Read(make([]byte, 1024))
if err != nil {
log.Printf("Failed to read from connection: %+v", err)
return
}
log.Printf("Received data: %#v", data)
}()
wg.Wait()
}