Compare commits
66 Commits
3d0d8ce676
...
master
Author | SHA1 | Date | |
---|---|---|---|
78fc53a9a4 | |||
0315a0877b | |||
e8b9eb934b | |||
797e1f025b | |||
4d7e13a304 | |||
38af7b146d | |||
f1eb6b065d | |||
4771ffa154 | |||
76bdc5214b | |||
9088bc14bc | |||
c56d441891 | |||
85e674753e | |||
b74a7c842f | |||
d477951fb4 | |||
4c92723df0 | |||
d0c868ca5c | |||
7001f2c51a | |||
7b4fcf3de1 | |||
ad8eaff930 | |||
f847588a24 | |||
ee4f8ecfd6 | |||
f5e263752e | |||
39c7876ed1 | |||
b2a588e667 | |||
db61da9fc2 | |||
9fbcdd86cc | |||
76d7102e72 | |||
74928d9f7a | |||
a6339587d4 | |||
48d28def47 | |||
e6199da5d6 | |||
a2f4e9af25 | |||
b26c64d0d9 | |||
930e5b9b4f | |||
7f9717266f | |||
92c0e83e73 | |||
0eb0f5d773 | |||
161ff14189 | |||
7bcc02c66d | |||
31efd3fdef | |||
38976d6bc8 | |||
d25058fdec | |||
2453d639ff | |||
14c861ccb4 | |||
500cb11235 | |||
ec04fa1fb6 | |||
a12c22587d | |||
f30680c26f | |||
bed69fbfd3 | |||
7faa683237 | |||
3d7702f1c6 | |||
6e58c5cd45 | |||
46ae251b32 | |||
921c31b9fc | |||
e0abb0ca70 | |||
123d90b4f5 | |||
0c8725a69e | |||
![]() |
75514c7d90 | ||
![]() |
6f164e5933 | ||
![]() |
36dcbbc154 | ||
![]() |
2759185dbb | ||
![]() |
a9827f10ce | ||
![]() |
aff4ffe070 | ||
![]() |
c73a6066c4 | ||
![]() |
f00dcafac4 | ||
![]() |
4ea320c784 |
23
.gitea/workflows/benchmark.yaml
Normal file
23
.gitea/workflows/benchmark.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
name: Benchmark BufferPool
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'master'
|
||||
|
||||
jobs:
|
||||
RunBenchmarks:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
RUNNER_TOOL_CACHE: /opt/hostedtoolcache
|
||||
GOMODCACHE: /opt/hostedtoolcache/go/pkg/mod
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4
|
||||
- name: Get Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version-file: 'go.mod'
|
||||
check-latest: true
|
||||
cache: true
|
||||
- name: Run benchmark
|
||||
run: cd benchmark && go run . -setid
|
24
.gitea/workflows/test.yaml
Normal file
24
.gitea/workflows/test.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
name: Run Tests
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '*'
|
||||
- '!benchmark'
|
||||
|
||||
jobs:
|
||||
Test:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
RUNNER_TOOL_CACHE: /opt/hostedtoolcache
|
||||
GOMODCACHE: /opt/hostedtoolcache/go/pkg/mod
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4
|
||||
- name: Get Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version-file: 'go.mod'
|
||||
check-latest: true
|
||||
cache: true
|
||||
- name: Run tests
|
||||
run: go test ./...
|
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
||||
jstester/node_modules
|
||||
encoding/tmp
|
||||
pdu/tmp
|
||||
tmp
|
||||
|
162
benchmark/benchmark.go
Normal file
162
benchmark/benchmark.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
setid := flag.Bool("setid", false, "Set the git user name and email to benchmark")
|
||||
flag.Parse()
|
||||
|
||||
log.Printf("Starting benchmarking...")
|
||||
|
||||
if *setid {
|
||||
setId()
|
||||
}
|
||||
head := getHeadHash()
|
||||
log.Printf("HEAD at %s", head)
|
||||
|
||||
err := os.MkdirAll("results", 0755)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating results directory with %v", err)
|
||||
}
|
||||
|
||||
now := time.Now().Format(time.DateOnly)
|
||||
resultFile := path.Join(".", "results", now+"_"+head+".txt")
|
||||
log.Printf("Writing results to %s", resultFile)
|
||||
outFile, err := os.Create(resultFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating result file %s with %v", resultFile, err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("go", "test", "-bench", ".", "./...")
|
||||
cmd.Dir = "../"
|
||||
cmd.Stdout = outFile
|
||||
|
||||
log.Printf("Running benchmark...")
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Printf("Benchmarking done; writing results to git...")
|
||||
log.Printf("Removing local benchmark branch")
|
||||
removeLocalBenchmarkBranch()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
log.Printf("Fetching benchmark branch")
|
||||
fetchBenchmark()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
log.Printf("Tracking benchmark branch")
|
||||
trackBenchmark()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
log.Printf("Checking out benchmark branch")
|
||||
checkoutBenchmark()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
log.Printf("Adding benchmark results")
|
||||
addResults()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
log.Printf("Committing benchmark results")
|
||||
commitBenchmark()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
log.Printf("Pushing benchmark results")
|
||||
pushBenchmark()
|
||||
log.Printf("Done")
|
||||
}
|
||||
|
||||
func removeLocalBenchmarkBranch() {
|
||||
gitBranch := exec.Command("git", "branch", "-D", "benchmark")
|
||||
gitBranch.Dir = "../"
|
||||
out, err := gitBranch.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Could not remove local benchmark branch with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func getHeadHash() string {
|
||||
cmd := exec.Command("git", "rev-parse", "HEAD")
|
||||
cmd.Dir = "../"
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not get HEAD with code %v and error %v", err, string(out))
|
||||
}
|
||||
return strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
func fetchBenchmark() {
|
||||
gitFetch := exec.Command("git", "fetch", "origin", "benchmark")
|
||||
gitFetch.Dir = "../"
|
||||
out, err := gitFetch.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not fetch benchmark branch with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func trackBenchmark() {
|
||||
gitTrack := exec.Command("git", "branch", "--track", "benchmark", "origin/benchmark")
|
||||
gitTrack.Dir = "../"
|
||||
out, err := gitTrack.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not track benchmark branch with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func checkoutBenchmark() {
|
||||
gitCheckout := exec.Command("git", "checkout", "benchmark")
|
||||
gitCheckout.Dir = "../"
|
||||
out, err := gitCheckout.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not checkout benchmark branch with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func setId() {
|
||||
gitConfig := exec.Command("git", "config", "user.name", "benchmark")
|
||||
gitConfig.Dir = "../"
|
||||
out, err := gitConfig.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not set user name with code %v and error %v", err, string(out))
|
||||
}
|
||||
|
||||
gitConfig = exec.Command("git", "config", "user.email", "benchmark@smpptester")
|
||||
gitConfig.Dir = "../"
|
||||
out, err = gitConfig.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not set user email with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func addResults() {
|
||||
gitAdd := exec.Command("git", "add", "benchmark/results")
|
||||
gitAdd.Dir = "../"
|
||||
out, err := gitAdd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not add results with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func commitBenchmark() {
|
||||
gitCommit := exec.Command("git", "commit", "-am", "Benchmark results")
|
||||
gitCommit.Dir = "../"
|
||||
out, err := gitCommit.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not commit benchmark results with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
||||
|
||||
func pushBenchmark() {
|
||||
gitPush := exec.Command("git", "push", "origin", "benchmark")
|
||||
gitPush.Dir = "../"
|
||||
out, err := gitPush.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not push benchmark results with code %v and error %v", err, string(out))
|
||||
}
|
||||
}
|
164
client/client.go
164
client/client.go
@@ -1 +1,165 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"smpptester/pdu"
|
||||
"smpptester/utils"
|
||||
"time"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var (
|
||||
SMPP_CLIENT_RETRY_TIMEOUT = 1 * time.Second
|
||||
SMPP_CLIENT_SEND_QUEUE_SIZE = 1024 * 8
|
||||
SMPP_CLIENT_RECEIVE_QUEUE_SIZE = 1024 * 8
|
||||
SMPP_CLIENT_ASYNC_SEND_WORKERS = 16
|
||||
SMPP_CLIENT_SEND_LIMIT = 4
|
||||
)
|
||||
|
||||
type SMPPClient struct {
|
||||
Id int
|
||||
Connected bool
|
||||
SendQueue chan pdu.PDU
|
||||
ReceiveQueue chan pdu.PDU
|
||||
SendLimit *utils.ReactiveValue[int]
|
||||
|
||||
sendLimiter *rate.Limiter
|
||||
enabled bool
|
||||
running bool
|
||||
conn net.Conn
|
||||
port string
|
||||
log log.Logger
|
||||
dcErr chan (error)
|
||||
}
|
||||
|
||||
func NewSMPPClient(port string, id int) *SMPPClient {
|
||||
client := &SMPPClient{
|
||||
Id: id,
|
||||
SendQueue: make(chan pdu.PDU, SMPP_CLIENT_SEND_QUEUE_SIZE),
|
||||
ReceiveQueue: make(chan pdu.PDU, SMPP_CLIENT_RECEIVE_QUEUE_SIZE),
|
||||
SendLimit: utils.NewReactiveValue[int](SMPP_CLIENT_SEND_LIMIT),
|
||||
|
||||
sendLimiter: rate.NewLimiter(rate.Limit(SMPP_CLIENT_SEND_LIMIT), SMPP_CLIENT_SEND_LIMIT),
|
||||
port: port,
|
||||
log: log.Logger{},
|
||||
dcErr: make(chan error, 128),
|
||||
}
|
||||
client.log = *log.New(log.Writer(), "", log.LstdFlags|log.Lshortfile)
|
||||
client.log.SetPrefix(fmt.Sprintf("SMPP client %d: ", client.Id))
|
||||
|
||||
go func() {
|
||||
limit := client.SendLimit.Subscribe()
|
||||
defer client.SendLimit.Unsubscribe(limit)
|
||||
for {
|
||||
v := <-limit
|
||||
client.log.Printf("Send limit changed to %d", *v)
|
||||
client.sendLimiter.SetLimit(rate.Limit(*v))
|
||||
client.sendLimiter.SetBurst(*v)
|
||||
}
|
||||
}()
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func (c *SMPPClient) Enable() {
|
||||
if c.enabled {
|
||||
c.log.Printf("SMPP client %d is already enabled", c.Id)
|
||||
return
|
||||
}
|
||||
c.enabled = true
|
||||
c.start()
|
||||
c.log.Printf("SMPP client %d enabled", c.Id)
|
||||
}
|
||||
func (c *SMPPClient) Disable() {
|
||||
if !c.enabled {
|
||||
c.log.Printf("SMPP client %d is already disabled", c.Id)
|
||||
return
|
||||
}
|
||||
c.enabled = false
|
||||
c.dcErr <- fmt.Errorf("client is disabled")
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
}
|
||||
c.log.Printf("SMPP client %d disabled", c.Id)
|
||||
}
|
||||
|
||||
func (c *SMPPClient) start() {
|
||||
if c.running {
|
||||
c.log.Printf("SMPP client %d is already running", c.Id)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
c.running = true
|
||||
defer func() {
|
||||
c.running = false
|
||||
}()
|
||||
|
||||
c.log.Printf("SMPP client %d started", c.Id)
|
||||
for {
|
||||
if c.enabled && !c.Connected {
|
||||
c.log.Printf("Trying to connect to %s", c.port)
|
||||
err := c.connect()
|
||||
if err != nil {
|
||||
c.dcErr <- err
|
||||
}
|
||||
}
|
||||
err := <-c.dcErr
|
||||
c.Connected = false
|
||||
if c.enabled {
|
||||
c.log.Printf("Disconnected: '%v' trying again in %d second(s)", err, int(SMPP_CLIENT_RETRY_TIMEOUT.Seconds()))
|
||||
time.Sleep(SMPP_CLIENT_RETRY_TIMEOUT)
|
||||
} else {
|
||||
c.log.Printf("Disconnected: '%v' & client is disabled, quitting", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *SMPPClient) connect() error {
|
||||
if !c.enabled {
|
||||
return fmt.Errorf("client is not enabled")
|
||||
}
|
||||
if c.Connected {
|
||||
return fmt.Errorf("client is already connected")
|
||||
}
|
||||
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.Connected = true
|
||||
c.conn = conn
|
||||
return nil
|
||||
}
|
||||
|
||||
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.sendLimiter.Wait(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to wait for rate limiter: %w", err)
|
||||
}
|
||||
|
||||
_, err = c.conn.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
c.dcErr <- err
|
||||
return fmt.Errorf("failed to send PDU: %w", err)
|
||||
}
|
||||
|
||||
// c.log.Printf("SMPP client %d sent PDU: %+v", c.Id, pdata)
|
||||
return nil
|
||||
}
|
||||
|
51
encoding/.air.toml
Normal file
51
encoding/.air.toml
Normal file
@@ -0,0 +1,51 @@
|
||||
root = "."
|
||||
testdata_dir = "testdata"
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
args_bin = []
|
||||
bin = "tmp\\main.exe"
|
||||
cmd = "go test ."
|
||||
delay = 1000
|
||||
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||
exclude_file = []
|
||||
exclude_regex = []
|
||||
exclude_unchanged = false
|
||||
follow_symlink = false
|
||||
full_bin = ""
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
include_file = []
|
||||
kill_delay = "0s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
post_cmd = []
|
||||
pre_cmd = []
|
||||
rerun = false
|
||||
rerun_delay = 100
|
||||
send_interrupt = false
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
app = ""
|
||||
build = "yellow"
|
||||
main = "magenta"
|
||||
runner = "green"
|
||||
watcher = "cyan"
|
||||
|
||||
[log]
|
||||
main_only = false
|
||||
time = false
|
||||
|
||||
[misc]
|
||||
clean_on_exit = true
|
||||
|
||||
[proxy]
|
||||
app_port = 0
|
||||
enabled = false
|
||||
proxy_port = 0
|
||||
|
||||
[screen]
|
||||
clear_on_rebuild = true
|
||||
keep_scroll = true
|
22
encoding/ascii.go
Normal file
22
encoding/ascii.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package encoding
|
||||
|
||||
import "bytes"
|
||||
|
||||
type ASCIICoder struct{}
|
||||
|
||||
func (c *ASCIICoder) Encode(s *string, buf *bytes.Buffer) error {
|
||||
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
||||
buf.WriteString(*s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) {
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func (c ASCIICoder) EncodesInto(s *string) int {
|
||||
return len(*s)
|
||||
}
|
||||
func (c ASCIICoder) DecodesInto(buf *bytes.Buffer) int {
|
||||
return buf.Len()
|
||||
}
|
74
encoding/ascii_test.go
Normal file
74
encoding/ascii_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestASCIIEncodeSimpleASCIIString(t *testing.T) {
|
||||
coder := &ASCIICoder{}
|
||||
var buf bytes.Buffer
|
||||
input := "Hello, World!"
|
||||
|
||||
expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
|
||||
err := coder.Encode(&input, &buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestASCIIDecodeSimpleASCIIString(t *testing.T) {
|
||||
coder := &ASCIICoder{}
|
||||
var buf bytes.Buffer
|
||||
input := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
|
||||
|
||||
expected := "Hello, World!"
|
||||
buf.Write(input)
|
||||
output, err := coder.Decode(&buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if output != expected {
|
||||
t.Errorf("Expected %v, but got %v", expected, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestASCIIEncodeEmptyString(t *testing.T) {
|
||||
coder := &ASCIICoder{}
|
||||
var buf bytes.Buffer
|
||||
input := ""
|
||||
|
||||
expected := []byte{}
|
||||
err := coder.Encode(&input, &buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Errorf("Expected %v, but got %v", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestASCIIDecodeEmptyString(t *testing.T) {
|
||||
coder := &ASCIICoder{}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
expected := ""
|
||||
output, err := coder.Decode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if output != expected {
|
||||
t.Errorf("Expected %v, but got %v", expected, output)
|
||||
}
|
||||
}
|
5
encoding/charset.json
Normal file
5
encoding/charset.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"ucs2": {
|
||||
"link": "http://www.columbia.edu/kermit/ucs2.html"
|
||||
}
|
||||
}
|
10
encoding/coder.go
Normal file
10
encoding/coder.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package encoding
|
||||
|
||||
import "bytes"
|
||||
|
||||
type Coder interface {
|
||||
Encode(s *string, buf *bytes.Buffer) error
|
||||
Decode(buf *bytes.Buffer) (string, error)
|
||||
EncodesInto(s *string) int
|
||||
DecodesInto(buf *bytes.Buffer) int
|
||||
}
|
136
encoding/gsm7.go
Normal file
136
encoding/gsm7.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type GSM7Coder struct{}
|
||||
|
||||
// Make sure buffer can fit EncodesInto bytes
|
||||
// Otherwise Encode will allocate memory as it sees fit
|
||||
// Which is fine but not optimal
|
||||
// Preallocate the buffer with the size of EncodesInto bytes
|
||||
func (c *GSM7Coder) Encode(s *string, buf *bytes.Buffer) error {
|
||||
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
|
||||
utf8 := []byte(*s)
|
||||
var (
|
||||
offset int = 1
|
||||
bitshift byte = 0
|
||||
leap, shift bool
|
||||
)
|
||||
encodedSize := c.EncodesInto(s)
|
||||
cap := buf.Cap()
|
||||
if cap < encodedSize {
|
||||
buf.Grow(encodedSize - cap)
|
||||
}
|
||||
|
||||
for index, septet := range utf8 {
|
||||
if septet > 0b01111111 {
|
||||
return fmt.Errorf("invalid character at index %d", index)
|
||||
}
|
||||
if index == 0 {
|
||||
continue
|
||||
}
|
||||
bitshift++
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte:%08b (%-3d) Leap:%5v", index, offset, bitshift, utf8[index], utf8[index], utf8[index-offset], utf8[index-offset], leap)
|
||||
mask := byte(255 >> (8 - bitshift))
|
||||
masked := (mask & septet) << (8 - bitshift)
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d Mask:%08b Masked:%08b", index, offset, bitshift, mask, masked)
|
||||
if leap {
|
||||
masked >>= 1
|
||||
}
|
||||
utf8[index-offset] |= masked
|
||||
utf8[index] >>= bitshift
|
||||
|
||||
if !leap {
|
||||
buf.WriteByte(utf8[index-offset])
|
||||
}
|
||||
if index == len(utf8)-1 && utf8[index] > 0 {
|
||||
buf.WriteByte(utf8[index])
|
||||
}
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte:%08b (%-3d) Leap:%5v", index, offset, bitshift, utf8[index], utf8[index], utf8[index-offset], utf8[index-offset], leap)
|
||||
if bitshift >= 7 {
|
||||
if leap {
|
||||
// log.Printf("Shift at Index:%-3d Offset:%-3d Bitshift:%-3d", index, offset, bitshift)
|
||||
leap = false
|
||||
bitshift = 0
|
||||
offset++
|
||||
shift = true
|
||||
continue
|
||||
}
|
||||
// log.Printf("Leap at Index:%-3d Offset:%-3d Bitshift:%-3d", index, offset, bitshift)
|
||||
leap = true
|
||||
bitshift = 6
|
||||
}
|
||||
if shift {
|
||||
offset = 1
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
|
||||
gsm7 := buf.Bytes()
|
||||
var (
|
||||
offset int
|
||||
bitshift byte = 0
|
||||
leap bool
|
||||
)
|
||||
outLength := c.DecodesInto(buf)
|
||||
lengthDiff := outLength - len(gsm7)
|
||||
gsm7 = append(gsm7, make([]byte, lengthDiff)...)
|
||||
start := len(gsm7) - 2
|
||||
|
||||
// We don't care about the last byte
|
||||
// Unless it's the %8.......
|
||||
// We'll deal with that later
|
||||
for index := start; index >= 0; index-- {
|
||||
octet := gsm7[index]
|
||||
bitshift = byte((index % 7) + 1)
|
||||
if bitshift == 7 {
|
||||
leap = true
|
||||
}
|
||||
offset = 1
|
||||
// log.Println(offset, index, index+offset)
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte(%-3d):%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], index+offset, gsm7[index+offset], gsm7[index+offset], leap)
|
||||
|
||||
mask := byte(255 << (8 - bitshift))
|
||||
masked := (mask & octet) >> (8 - bitshift)
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d Mask:%08b Masked:%08b", index, offset, bitshift, mask, masked)
|
||||
if leap {
|
||||
InsertAt(&gsm7, index+offset, masked)
|
||||
} else {
|
||||
gsm7[index+offset] |= masked
|
||||
}
|
||||
// Remove last bitshift bits
|
||||
gsm7[index] <<= bitshift
|
||||
// Move the remaining bit once to the right to form septet instead of octet
|
||||
gsm7[index] >>= 1
|
||||
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte(%-3d):%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], index+offset, gsm7[index+offset], gsm7[index+offset], leap)
|
||||
leap = false
|
||||
}
|
||||
return string(gsm7), nil
|
||||
}
|
||||
|
||||
// Allocation free
|
||||
// Which means data MUST have space for value
|
||||
func InsertAt(data *[]byte, index int, value byte) {
|
||||
copy((*data)[index+1:], (*data)[index:])
|
||||
(*data)[index] = value
|
||||
}
|
||||
|
||||
func (c GSM7Coder) EncodesInto(s *string) int {
|
||||
slen := len(*s)
|
||||
enclen := slen * 7 / 8
|
||||
if slen%8 != 0 {
|
||||
enclen++
|
||||
}
|
||||
return enclen
|
||||
}
|
||||
func (c GSM7Coder) DecodesInto(buf *bytes.Buffer) int {
|
||||
blen := buf.Len()
|
||||
declen := blen * 8 / 7
|
||||
return declen
|
||||
}
|
369
encoding/gsm7_test.go
Normal file
369
encoding/gsm7_test.go
Normal file
@@ -0,0 +1,369 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
short8nString = "Sunshine"
|
||||
longNot8nString = "Golden rays play, Chasing night away."
|
||||
long8nString = "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
|
||||
short8nStringEncodedBytes = []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
|
||||
longNot8nStringEncodedBytes = []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
|
||||
long8nStringEncodedBytes = []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
|
||||
)
|
||||
|
||||
// region encode
|
||||
func TestGSM7EncodeSimpleASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := short8nString
|
||||
|
||||
expected := short8nStringEncodedBytes
|
||||
err := coder.Encode(&input, &buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodeComplexASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := longNot8nString
|
||||
|
||||
expected := longNot8nStringEncodedBytes
|
||||
err := coder.Encode(&input, &buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodeComplex8nASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
|
||||
expected := long8nStringEncodedBytes
|
||||
err := coder.Encode(&input, &buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
|
||||
buf.Reset()
|
||||
|
||||
expected := buf.Cap()
|
||||
err := coder.Encode(&input, buf)
|
||||
actual := buf.Cap()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected buffer of size %v, but got %v", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodeDoesAllocateWhenNecessary(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
var buf bytes.Buffer
|
||||
buf.Reset()
|
||||
|
||||
original := buf.Cap()
|
||||
err := coder.Encode(&input, &buf)
|
||||
modified := buf.Cap()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !(modified > original) {
|
||||
t.Errorf("Expected buffer to grow but buffer changed from %v to %v", original, modified)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodeEmptyString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := ""
|
||||
|
||||
expected := []byte{}
|
||||
err := coder.Encode(&input, &buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
// region decode
|
||||
func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := short8nStringEncodedBytes
|
||||
|
||||
expected := short8nString
|
||||
buf.Write(input)
|
||||
actual, err := coder.Decode(&buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodeComplexASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := longNot8nStringEncodedBytes
|
||||
|
||||
expected := longNot8nString
|
||||
buf.Write(input)
|
||||
actual, err := coder.Decode(&buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodeComplex8nASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := long8nStringEncodedBytes
|
||||
|
||||
expected := long8nString
|
||||
buf.Write(input)
|
||||
actual, err := coder.Decode(&buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodeEmptyString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
expected := ""
|
||||
actual, err := coder.Decode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// region insertat
|
||||
func TestInsertAtBeginning(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 0, 1)
|
||||
expected := []byte{1, 2, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertInMiddle(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 1, 5)
|
||||
expected := []byte{2, 5, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAtEnd(t *testing.T) {
|
||||
data := []byte{1, 2, 3, 0}
|
||||
InsertAt(&data, 3, 4)
|
||||
expected := []byte{1, 2, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexOutOfBounds(t *testing.T) {
|
||||
data := []byte{2, 3, 4}
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Errorf("The code did not panic")
|
||||
}
|
||||
}()
|
||||
InsertAt(&data, 4, 5)
|
||||
}
|
||||
|
||||
func TestNegativeIndex(t *testing.T) {
|
||||
data := []byte{2, 3, 4}
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Errorf("The code did not panic")
|
||||
}
|
||||
}()
|
||||
InsertAt(&data, -1, 1)
|
||||
}
|
||||
|
||||
func TestMaintainsOrderAfterInsertion(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 1, 1)
|
||||
expected := []byte{2, 1, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaintainsLength(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 1, 1)
|
||||
if len(data) != 4 {
|
||||
t.Errorf("expected length 4, got %d", len(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletesLastValue(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 5}
|
||||
InsertAt(&data, 0, 1)
|
||||
expected := []byte{1, 2, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
// region misc tests
|
||||
func TestGSM7EncodesIntoSmallString(t *testing.T) {
|
||||
input := short8nString
|
||||
expected := 7
|
||||
actual := GSM7Coder{}.EncodesInto(&input)
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
|
||||
input := longNot8nString
|
||||
expected := 33
|
||||
actual := GSM7Coder{}.EncodesInto(&input)
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
|
||||
input := long8nString
|
||||
expected := 56
|
||||
actual := GSM7Coder{}.EncodesInto(&input)
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodesIntoSmallString(t *testing.T) {
|
||||
input := short8nStringEncodedBytes
|
||||
expected := 8
|
||||
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
|
||||
input := longNot8nStringEncodedBytes
|
||||
expected := 37
|
||||
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
|
||||
input := long8nStringEncodedBytes
|
||||
expected := 64
|
||||
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// region benchmark
|
||||
func BenchmarkGSM7EncodeSimpleASCIIString(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := short8nString
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
coder.Encode(&input, &buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGSM7EncodeComplexASCIIString(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := longNot8nString
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
coder.Encode(&input, &buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := long8nString
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
coder.Encode(&input, &buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
input := long8nString
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
|
||||
buf.Reset()
|
||||
coder.Encode(&input, buf)
|
||||
}
|
||||
}
|
19
encoding/ucs2.go
Normal file
19
encoding/ucs2.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package encoding
|
||||
|
||||
import "bytes"
|
||||
|
||||
type UCS2Coder struct{}
|
||||
|
||||
func (c *UCS2Coder) Encode(s *string, buf *bytes.Buffer) error {
|
||||
panic("UCS2 not implemented yet")
|
||||
}
|
||||
func (c *UCS2Coder) Decode(buf *bytes.Buffer) (string, error) {
|
||||
panic("UCS2 not implemented yet")
|
||||
}
|
||||
|
||||
func (c UCS2Coder) EncodesInto(s *string) int {
|
||||
return len(*s) * 2
|
||||
}
|
||||
func (c UCS2Coder) DecodesInto(buf *bytes.Buffer) int {
|
||||
return buf.Len() / 2
|
||||
}
|
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module smpptester
|
||||
|
||||
go 1.22.4
|
||||
|
||||
require github.com/yuin/gopher-lua v1.1.1
|
||||
require (
|
||||
github.com/yuin/gopher-lua v1.1.1
|
||||
golang.org/x/time v0.5.0
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@@ -1,2 +1,4 @@
|
||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
|
@@ -3,7 +3,7 @@ const smpp = require("smpp");
|
||||
let message_id = 0;
|
||||
const session = smpp.connect(
|
||||
{
|
||||
url: "smpp://0.0.0.0:6001",
|
||||
url: "smpp://0.0.0.0:2775",
|
||||
auto_enquire_link_period: 10000,
|
||||
debug: true,
|
||||
},
|
||||
|
68
main.go
68
main.go
@@ -4,12 +4,15 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"smpptester/client"
|
||||
"smpptester/pdu"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lmicroseconds)
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
}
|
||||
|
||||
// Function to create a submit_sm PDU
|
||||
@@ -29,7 +32,7 @@ func createSubmitSMPDU() []byte {
|
||||
validityPeriod := ""
|
||||
registeredDelivery := byte(0x01)
|
||||
replaceIfPresentFlag := byte(0x00)
|
||||
dataCoding := byte(0x00)
|
||||
dataCoding := byte(0x01)
|
||||
smDefaultMsgID := byte(0x00)
|
||||
shortMessage := "Hello, SMPP!"
|
||||
|
||||
@@ -37,7 +40,6 @@ func createSubmitSMPDU() []byte {
|
||||
headerLength := 16
|
||||
bodyLength := 1 + len(serviceType) + 1 + 1 + len(sourceAddr) + 1 + 1 + len(destinationAddr) + 1 + 1 + len(scheduleDeliveryTime) + 1 + len(validityPeriod) + 1 + 1 + 1 + 1 + 1 + len(shortMessage)
|
||||
totalLength := headerLength + bodyLength
|
||||
log.Printf("Total length: %d", totalLength)
|
||||
|
||||
// Create a buffer to hold the PDU
|
||||
var buffer bytes.Buffer
|
||||
@@ -136,27 +138,55 @@ func main() {
|
||||
// go handleConnection(conn)
|
||||
// }
|
||||
// }()
|
||||
submit := &pdu.SUBMIT_SM{
|
||||
Header: &pdu.PDU_HEADER{
|
||||
Command_length: 0,
|
||||
Command_id: 4,
|
||||
Command_status: 0,
|
||||
Sequence_number: 1,
|
||||
},
|
||||
Service_type: "hehe",
|
||||
Source_addr_ton: 0x01,
|
||||
Source_addr_npi: 0x01,
|
||||
Source_addr: "12345",
|
||||
Dest_addr_ton: 0x01,
|
||||
Dest_addr_npi: 0x01,
|
||||
Destination_addr: "67890",
|
||||
Esm_class: 0x00,
|
||||
Protocol_id: 0x00,
|
||||
Priority_flag: 0x00,
|
||||
Schedule_delivery_time: "",
|
||||
Validity_period: "",
|
||||
Registered_delivery: 0x01,
|
||||
Replace_if_present: 0x00,
|
||||
Data_coding: 0x01,
|
||||
Sm_default_msg_id: 0x00,
|
||||
Short_message: "Hello, SMPP!",
|
||||
}
|
||||
submit.UpdateSize()
|
||||
buf := pdu.ByteBufferPool.Get(submit.Size())
|
||||
defer pdu.ByteBufferPool.Put(buf)
|
||||
submit.Encode(buf)
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
||||
conn, err := net.Dial("tcp", "localhost:2775")
|
||||
if err != nil {
|
||||
log.Printf("Failed to connect to SMPP server: %+v", err)
|
||||
return
|
||||
client := client.NewSMPPClient("localhost:2775", 1)
|
||||
client.Enable()
|
||||
for {
|
||||
if client.Connected {
|
||||
// err := client.Send(submit)
|
||||
// if err != nil {
|
||||
// log.Printf("Failed to send PDU: %v", err)
|
||||
// }
|
||||
go client.Send(submit)
|
||||
// if rand.Int() % 1000 == 0 {
|
||||
// client.SendLimit.Set(rand.Int() % 1000)
|
||||
// }
|
||||
}
|
||||
log.Printf("Connected to SMPP server")
|
||||
defer conn.Close()
|
||||
|
||||
go func() {
|
||||
conn.Write(createSubmitSMPDU())
|
||||
data, err := conn.Read(make([]byte, 1024))
|
||||
if err != nil {
|
||||
log.Printf("Failed to read from connection: %+v", err)
|
||||
return
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
log.Printf("Received data: %#v", data)
|
||||
}()
|
||||
log.Println("Are we done")
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
51
pdu/.air.toml
Normal file
51
pdu/.air.toml
Normal file
@@ -0,0 +1,51 @@
|
||||
root = "."
|
||||
testdata_dir = "testdata"
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
args_bin = []
|
||||
bin = "tmp\\main.exe"
|
||||
cmd = "go test ."
|
||||
delay = 1000
|
||||
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||
exclude_file = []
|
||||
exclude_regex = []
|
||||
exclude_unchanged = false
|
||||
follow_symlink = false
|
||||
full_bin = ""
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
include_file = []
|
||||
kill_delay = "0s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
post_cmd = []
|
||||
pre_cmd = []
|
||||
rerun = false
|
||||
rerun_delay = 500
|
||||
send_interrupt = false
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
app = ""
|
||||
build = "yellow"
|
||||
main = "magenta"
|
||||
runner = "green"
|
||||
watcher = "cyan"
|
||||
|
||||
[log]
|
||||
main_only = false
|
||||
time = false
|
||||
|
||||
[misc]
|
||||
clean_on_exit = false
|
||||
|
||||
[proxy]
|
||||
app_port = 0
|
||||
enabled = false
|
||||
proxy_port = 0
|
||||
|
||||
[screen]
|
||||
clear_on_rebuild = false
|
||||
keep_scroll = true
|
26
pdu/bind.go
26
pdu/bind.go
@@ -2,19 +2,19 @@ package pdu
|
||||
|
||||
type (
|
||||
BIND struct {
|
||||
header PDU_HEADER
|
||||
system_id string
|
||||
password string
|
||||
system_type string
|
||||
interface_version uint8
|
||||
addr_ton uint8
|
||||
addr_npi uint8
|
||||
address_range string
|
||||
Header *PDU_HEADER
|
||||
System_id string
|
||||
Password string
|
||||
System_type string
|
||||
Interface_version byte
|
||||
Addr_ton byte
|
||||
Addr_npi byte
|
||||
Address_range string
|
||||
}
|
||||
BIND_RESP struct {
|
||||
header PDU_HEADER
|
||||
system_id string
|
||||
sc_interface_version uint8
|
||||
Header *PDU_HEADER
|
||||
System_id string
|
||||
Sc_interface_version byte
|
||||
}
|
||||
BIND_RECVEIVER struct {
|
||||
BIND
|
||||
@@ -36,9 +36,9 @@ type (
|
||||
}
|
||||
|
||||
UNBIND struct {
|
||||
PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
}
|
||||
UNBIND_RESP struct {
|
||||
PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
}
|
||||
)
|
||||
|
@@ -1,12 +1,21 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type BufferPoolManager struct {
|
||||
pools map[int]*sync.Pool
|
||||
mu sync.Mutex
|
||||
mu sync.RWMutex
|
||||
debug bool
|
||||
}
|
||||
|
||||
func (bpm *BufferPoolManager) logf(format string, args ...interface{}) {
|
||||
if bpm.debug {
|
||||
log.Printf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func NewBufferPoolManager() *BufferPoolManager {
|
||||
@@ -15,35 +24,47 @@ func NewBufferPoolManager() *BufferPoolManager {
|
||||
}
|
||||
}
|
||||
|
||||
func (bpm *BufferPoolManager) GetBuffer(size int) *([]uint8) {
|
||||
bpm.mu.Lock()
|
||||
func (bpm *BufferPoolManager) Get(size int) *bytes.Buffer {
|
||||
bpm.mu.RLock()
|
||||
pool, exists := bpm.pools[size]
|
||||
bpm.mu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
bpm.mu.Lock()
|
||||
// Double-check if another goroutine added the pool while we were waiting
|
||||
pool, exists = bpm.pools[size]
|
||||
if !exists {
|
||||
bpm.logf("Creating new pool for size %d\n", size)
|
||||
pool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
buf := make([]uint8, size)
|
||||
return &buf
|
||||
bpm.logf("Creating new buffer of size %d\n", size)
|
||||
buf := bytes.NewBuffer(make([]byte, size))
|
||||
buf.Reset()
|
||||
return buf
|
||||
},
|
||||
}
|
||||
bpm.pools[size] = pool
|
||||
}
|
||||
bpm.mu.Unlock()
|
||||
return pool.Get().(*[]uint8)
|
||||
}
|
||||
|
||||
buf := pool.Get().(*bytes.Buffer)
|
||||
bpm.logf("Returning buffer of size %d: %p\n", buf.Cap(), buf)
|
||||
return buf
|
||||
}
|
||||
|
||||
func (bpm *BufferPoolManager) PutBuffer(buf *([]uint8)) {
|
||||
size := len(*buf)
|
||||
bpm.mu.Lock()
|
||||
func (bpm *BufferPoolManager) Put(buf *bytes.Buffer) {
|
||||
size := buf.Cap()
|
||||
bpm.mu.RLock()
|
||||
pool, exists := bpm.pools[size]
|
||||
bpm.mu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
bpm.mu.Unlock()
|
||||
bpm.logf("Cannot return %p, No pool for size %d\n", buf, size)
|
||||
return
|
||||
}
|
||||
bpm.mu.Unlock()
|
||||
|
||||
// Reset buffer (optional)
|
||||
for i := range *buf {
|
||||
(*buf)[i] = 0
|
||||
}
|
||||
buf.Reset()
|
||||
pool.Put(buf)
|
||||
bpm.logf("Returned buffer of size %d: %p\n", size, buf)
|
||||
}
|
||||
|
@@ -3,49 +3,41 @@ package pdu
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer, got nil")
|
||||
}
|
||||
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestBufferSizeZero(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 0
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer, got nil")
|
||||
}
|
||||
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentAccessToBufferPool(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
var wg sync.WaitGroup
|
||||
@@ -55,12 +47,12 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
if buffer == nil {
|
||||
t.Errorf("Expected buffer, got nil")
|
||||
}
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -69,75 +61,164 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetBufferLockUnlock(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer, got nil")
|
||||
}
|
||||
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPoolCreationForNewSizes(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 512
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer, got nil")
|
||||
}
|
||||
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferPoolManagerGetBuffer(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer, got nil")
|
||||
}
|
||||
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBufferWithMultipleSizes(t *testing.T) {
|
||||
bpm := &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
sizes := []int{512, 1024, 2048}
|
||||
for _, size := range sizes {
|
||||
buffer := bpm.GetBuffer(size)
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer for size %d, got nil", size)
|
||||
}
|
||||
|
||||
if len(*buffer) != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBufferIsAlwaysZero(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024 * 64
|
||||
for i := 0; i < 1000; i++ {
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
if buffer == nil {
|
||||
t.Fatalf("Expected buffer for size %d, got nil", size)
|
||||
}
|
||||
|
||||
if buffer.Cap() != size {
|
||||
t.Errorf("Expected buffer size %d, got %d", size, buffer.Cap())
|
||||
}
|
||||
|
||||
for _, b := range buffer.Bytes() {
|
||||
if b != 0 {
|
||||
t.Errorf("Expected buffer to be zero, got %d", b)
|
||||
}
|
||||
}
|
||||
|
||||
bpm.Put(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoolReusesBuffers(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.Get(size)
|
||||
initialPtr := buffer
|
||||
bpm.Put(buffer)
|
||||
for i := 0; i < 1000; i++ {
|
||||
buffer = bpm.Get(size)
|
||||
if buffer != initialPtr {
|
||||
t.Errorf("Expected initial buffer (%p) to be reused, got %p", initialPtr, buffer)
|
||||
}
|
||||
bpm.Put(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
// region benchmark
|
||||
func BenchmarkBufferPoolManager(b *testing.B) {
|
||||
bpm := NewBufferPoolManager()
|
||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Benchmark Get
|
||||
buf := bpm.Get(bufSize)
|
||||
// Benchmark Put
|
||||
bpm.Put(buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
|
||||
bpm := NewBufferPoolManager()
|
||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
concurrency := 1000
|
||||
|
||||
for i := 0; i < concurrency; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < b.N/concurrency; j++ {
|
||||
buf := bpm.Get(bufSize)
|
||||
bpm.Put(buf)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func BenchmarkBufferPoolManager_Memory(b *testing.B) {
|
||||
bpm := NewBufferPoolManager()
|
||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
||||
b.ResetTimer()
|
||||
|
||||
var i byte
|
||||
buf := bpm.Get(bufSize)
|
||||
b.StopTimer()
|
||||
|
||||
// Simulate some work
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
for range buf.Bytes() {
|
||||
buf.WriteByte(i % 255)
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bpm.Put(buf)
|
||||
buf = bpm.Get(bufSize)
|
||||
}
|
||||
}
|
||||
|
@@ -2,17 +2,17 @@ package pdu
|
||||
|
||||
type (
|
||||
CANCEL_SM struct {
|
||||
header PDU_HEADER
|
||||
service_type string
|
||||
message_id string
|
||||
source_addr_ton uint8
|
||||
source_addr_npi uint8
|
||||
source_addr string
|
||||
dest_addr_ton uint8
|
||||
dest_addr_npi uint8
|
||||
destination_addr string
|
||||
Header *PDU_HEADER
|
||||
Service_type string
|
||||
Message_id string
|
||||
Source_addr_ton byte
|
||||
Source_addr_npi byte
|
||||
Source_addr string
|
||||
Dest_addr_ton byte
|
||||
Dest_addr_npi byte
|
||||
Destination_addr string
|
||||
}
|
||||
CANCEL_SM_RESP struct {
|
||||
header PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
}
|
||||
)
|
||||
|
@@ -2,11 +2,11 @@ package pdu
|
||||
|
||||
type (
|
||||
DELIVER_SM struct {
|
||||
header PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
SUBMIT_SM
|
||||
}
|
||||
DELIVER_SM_RESP struct {
|
||||
header PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
SUBMIT_SM_RESP
|
||||
}
|
||||
)
|
||||
|
@@ -2,9 +2,9 @@ package pdu
|
||||
|
||||
type (
|
||||
ENQUIRE_LINK struct {
|
||||
header PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
}
|
||||
ENQUIRE_LINK_RESP struct {
|
||||
header PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
}
|
||||
)
|
||||
|
@@ -1,8 +1,5 @@
|
||||
package pdu
|
||||
|
||||
import "sync"
|
||||
|
||||
var ByteBufferPool = &BufferPoolManager{
|
||||
pools: make(map[int]*sync.Pool),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
var ByteBufferPool = NewBufferPoolManager()
|
||||
const NULL = byte(0x00)
|
||||
var NULL_ARR = []byte{NULL}
|
102
pdu/pdu.go
102
pdu/pdu.go
@@ -1,66 +1,86 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
PDU interface {
|
||||
EncodeInto(*[]uint8)
|
||||
Encode() []uint8
|
||||
Decode([]uint8)
|
||||
Encode(*bytes.Buffer) error
|
||||
Decode(*bytes.Buffer) error
|
||||
// Size in bytes
|
||||
Size() uint32
|
||||
Size() int
|
||||
UpdateSize()
|
||||
}
|
||||
|
||||
PDU_HEADER struct {
|
||||
command_length uint32
|
||||
command_id uint32
|
||||
command_status uint32
|
||||
sequence_number uint32
|
||||
Command_length uint32
|
||||
Command_id uint32
|
||||
Command_status uint32
|
||||
Sequence_number uint32
|
||||
}
|
||||
|
||||
GENERIC_NACK struct {
|
||||
header PDU_HEADER
|
||||
Header *PDU_HEADER
|
||||
}
|
||||
)
|
||||
|
||||
func (p *PDU_HEADER) Encode() (*[]uint8, error) {
|
||||
buf := ByteBufferPool.GetBuffer(int(p.Size()))
|
||||
err := p.EncodeInto(buf)
|
||||
return buf, err
|
||||
}
|
||||
func (p *PDU_HEADER) EncodeInto(buf *[]uint8) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot encode PDU_HEADER, buffer is nil")
|
||||
}
|
||||
if len(*buf) < 16 {
|
||||
return fmt.Errorf("cannot encode PDU_HEADER, buffer too small (%d, required 16)", len(*buf))
|
||||
}
|
||||
bufVal := *buf
|
||||
binary.BigEndian.PutUint32(bufVal[0:4], p.command_length)
|
||||
binary.BigEndian.PutUint32(bufVal[4:8], p.command_id)
|
||||
binary.BigEndian.PutUint32(bufVal[8:12], p.command_status)
|
||||
binary.BigEndian.PutUint32(bufVal[12:16], p.sequence_number)
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Decode(data []uint8) error {
|
||||
if len(data) >= 4 {
|
||||
p.command_length = binary.BigEndian.Uint32(data[0:4])
|
||||
}
|
||||
if len(data) >= 8 {
|
||||
p.command_id = binary.BigEndian.Uint32(data[4:8])
|
||||
}
|
||||
if len(data) >= 12 {
|
||||
p.command_status = binary.BigEndian.Uint32(data[8:12])
|
||||
// Hmm the header can be partially encoded
|
||||
// As in command_status can just be null
|
||||
// So not 0s, null, non existent, not encoded
|
||||
// So we'll have to maybe set them to nil or something...
|
||||
// Wireshark dump of a submit_sm
|
||||
// See header: 00 00 00 3e (size, 62 bytes), 00 00 00 04 (command_id), status and sequence number are 0
|
||||
// So they are not encoded
|
||||
// Here the header is 8 bytes, not 16
|
||||
// 0000 02 00 00 00 45 00 00 66 6f ff 40 00 80 06 00 00 ....E..fo.@.....
|
||||
// 0010 7f 00 00 01 7f 00 00 01 31 4f 0a d7 e8 b0 b1 b5 ........1O......
|
||||
// 0020 3e 91 d9 dc 50 18 20 fa 2a 1f 00 00 00 00 00 3e >...P. .*......>
|
||||
// 0030 00 00 00 04 00 00 00 00 00 00 00 02 00 00 00 73 ...............s
|
||||
// 0040 6d 70 70 5f 74 65 73 74 5f 31 00 00 00 31 32 33 mpp_test_1...123
|
||||
// 0050 31 32 33 31 32 33 31 32 33 00 00 00 00 00 00 00 123123123.......
|
||||
// 0060 00 c0 00 06 48 65 6c 6c 6f 21 ....Hello!
|
||||
// I AM NOT RIGHT
|
||||
// Beyond the size and command_id, command_status and sequence_number here are:
|
||||
// 00 00 00 00 (0) 00 00 00 02 (2)
|
||||
//
|
||||
// Another example - bind_transciever:
|
||||
// 0000 02 00 00 00 45 00 00 47 6f fb 40 00 80 06 00 00 ....E..Go.@.....
|
||||
// 0010 7f 00 00 01 7f 00 00 01 31 4f 0a d7 e8 b0 b1 96 ........1O......
|
||||
// 0020 3e 91 d9 cb 50 18 20 fa df ab 00 00 00 00 00 1f >...P. .........
|
||||
// 0030 00 00 00 09 00 00 00 00 00 00 00 01 74 65 73 74 ............test
|
||||
// 0040 00 74 65 73 74 00 00 50 00 00 00 .test..P...
|
||||
// 00 00 00 1f - 00 00 00 09 - 00 00 00 00 - 00 00 00 01 (header)
|
||||
// 74 65 73 74 00 - 74 65 73 74 00 - 00 50 00 00 00
|
||||
// ^system_id ^password ^interface_version
|
||||
// What are the other 0s?
|
||||
// Don't know
|
||||
|
||||
func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot encode into nil buffer")
|
||||
}
|
||||
if len(data) >= 16 {
|
||||
p.sequence_number = binary.BigEndian.Uint32(data[12:16])
|
||||
}
|
||||
binary.Write(buf, binary.BigEndian, p.Command_length)
|
||||
binary.Write(buf, binary.BigEndian, p.Command_id)
|
||||
binary.Write(buf, binary.BigEndian, p.Command_status)
|
||||
binary.Write(buf, binary.BigEndian, p.Sequence_number)
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Size() uint32 {
|
||||
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot decode nil buffer")
|
||||
}
|
||||
binary.Read(buf, binary.BigEndian, &p.Command_length)
|
||||
binary.Read(buf, binary.BigEndian, &p.Command_id)
|
||||
binary.Read(buf, binary.BigEndian, &p.Command_status)
|
||||
binary.Read(buf, binary.BigEndian, &p.Sequence_number)
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Size() int {
|
||||
return 16
|
||||
}
|
||||
func (p *PDU_HEADER) UpdateSize() {
|
||||
p.Command_length = uint32(p.Size())
|
||||
}
|
||||
|
423
pdu/pdu_test.go
423
pdu/pdu_test.go
@@ -1,6 +1,7 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"sync"
|
||||
@@ -10,37 +11,41 @@ import (
|
||||
// region encode
|
||||
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 1,
|
||||
command_id: 1,
|
||||
command_status: 1,
|
||||
sequence_number: 1,
|
||||
Command_length: 1,
|
||||
Command_id: 1,
|
||||
Command_status: 1,
|
||||
Sequence_number: 1,
|
||||
}
|
||||
result, err := p.Encode()
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(*result) != 16 {
|
||||
t.Errorf("Expected byte slice of length 16, got %d", len(*result))
|
||||
if buf.Cap() != 16 {
|
||||
t.Errorf("Expected byte slice of length 16, got %d", buf.Cap())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeHandlesZeroValues(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 0,
|
||||
command_id: 0,
|
||||
command_status: 0,
|
||||
sequence_number: 0,
|
||||
Command_length: 0,
|
||||
Command_id: 0,
|
||||
Command_status: 0,
|
||||
Sequence_number: 0,
|
||||
}
|
||||
result, err := p.Encode()
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
expected := make([]uint8, 16)
|
||||
for i, v := range *result {
|
||||
expected := make([]byte, 16)
|
||||
for i, v := range buf.Bytes() {
|
||||
if v != expected[i] {
|
||||
t.Errorf("Expected byte slice with zero values, got %v", *result)
|
||||
t.Errorf("Expected byte slice with zero values, got %v", buf.Bytes())
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -48,21 +53,23 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
|
||||
|
||||
func TestEncodeEncodesProperly(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 1,
|
||||
command_id: 2,
|
||||
command_status: 3,
|
||||
sequence_number: 4,
|
||||
Command_length: 1,
|
||||
Command_id: 2,
|
||||
Command_status: 3,
|
||||
Sequence_number: 4,
|
||||
}
|
||||
result, err := p.Encode()
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
expected := []uint8{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
|
||||
for i, v := range *result {
|
||||
expected := []byte{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
|
||||
for i, v := range buf.Bytes() {
|
||||
if v != expected[i] {
|
||||
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
|
||||
t.Errorf("Expected byte slice with values %v, got %v", expected, buf.Bytes())
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -70,21 +77,23 @@ func TestEncodeEncodesProperly(t *testing.T) {
|
||||
|
||||
func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 13426724,
|
||||
command_id: 254352,
|
||||
command_status: 35634264,
|
||||
sequence_number: 476543523,
|
||||
Command_length: 13426724,
|
||||
Command_id: 254352,
|
||||
Command_status: 35634264,
|
||||
Sequence_number: 476543523,
|
||||
}
|
||||
result, err := p.Encode()
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
expected := []uint8{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
|
||||
for i, v := range *result {
|
||||
expected := []byte{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
|
||||
for i, v := range buf.Bytes() {
|
||||
if v != expected[i] {
|
||||
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
|
||||
t.Errorf("Expected byte slice with values %v, got %v", expected, buf.Bytes())
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -92,216 +101,198 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
||||
|
||||
func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
command_status: 0,
|
||||
sequence_number: 12345,
|
||||
Command_length: 16,
|
||||
Command_id: 1,
|
||||
Command_status: 0,
|
||||
Sequence_number: 12345,
|
||||
}
|
||||
buf := make([]uint8, 16)
|
||||
err := p.EncodeInto(&buf)
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||
innerbuf := buf.Bytes()
|
||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoHandlesNilBuffer(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
command_status: 0,
|
||||
sequence_number: 12345,
|
||||
}
|
||||
var buf *[]uint8 = nil
|
||||
|
||||
err := p.EncodeInto(buf)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error when buffer is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoHandlesSmallerBuffer(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
command_status: 0,
|
||||
sequence_number: 12345,
|
||||
}
|
||||
buf := make([]uint8, 12) // smaller buffer size
|
||||
|
||||
err := p.EncodeInto(&buf)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error when buffer is too small")
|
||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
command_status: 0,
|
||||
sequence_number: 12345,
|
||||
Command_length: 16,
|
||||
Command_id: 1,
|
||||
Command_status: 0,
|
||||
Sequence_number: 12345,
|
||||
}
|
||||
buf := make([]uint8, 20)
|
||||
err := p.EncodeInto(&buf)
|
||||
buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||
innerbuf := buf.Bytes()
|
||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
command_status: 0,
|
||||
sequence_number: 12345,
|
||||
Command_length: 16,
|
||||
Command_id: 1,
|
||||
Command_status: 0,
|
||||
Sequence_number: 12345,
|
||||
}
|
||||
buf := make([]uint8, 16)
|
||||
err := p.EncodeInto(&buf)
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||
innerbuf := buf.Bytes()
|
||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
command_status: 0,
|
||||
sequence_number: 12345,
|
||||
Command_length: 16,
|
||||
Command_id: 1,
|
||||
Command_status: 0,
|
||||
Sequence_number: 12345,
|
||||
}
|
||||
buf := make([]uint8, 16)
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 1000; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
p.EncodeInto(&buf)
|
||||
p.Encode(buf)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||
innerbuf := buf.Bytes()
|
||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: math.MaxUint32,
|
||||
command_id: math.MaxUint32,
|
||||
command_status: math.MaxUint32,
|
||||
sequence_number: math.MaxUint32,
|
||||
Command_length: math.MaxUint32,
|
||||
Command_id: math.MaxUint32,
|
||||
Command_status: math.MaxUint32,
|
||||
Sequence_number: math.MaxUint32,
|
||||
}
|
||||
buf := make([]uint8, 16)
|
||||
err := p.EncodeInto(&buf)
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||
innerbuf := buf.Bytes()
|
||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 0,
|
||||
command_id: 0,
|
||||
command_status: 0,
|
||||
sequence_number: 0,
|
||||
Command_length: 0,
|
||||
Command_id: 0,
|
||||
Command_status: 0,
|
||||
Sequence_number: 0,
|
||||
}
|
||||
buf := make([]uint8, 16)
|
||||
err := p.EncodeInto(&buf)
|
||||
buf := bytes.NewBuffer(make([]byte, 16))
|
||||
buf.Reset()
|
||||
err := p.Encode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
|
||||
innerbuf := buf.Bytes()
|
||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.Command_length {
|
||||
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
|
||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.Command_id {
|
||||
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
|
||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.Command_status {
|
||||
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||
}
|
||||
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
|
||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.Sequence_number {
|
||||
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||
}
|
||||
}
|
||||
|
||||
// region decode
|
||||
func TestDecodeHandlesShortByteSlice(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := []uint8{0, 0, 0, 10}
|
||||
data := []byte{0, 0, 0, 10}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("Decode panicked with short byte slice")
|
||||
}
|
||||
}()
|
||||
err := p.Decode(data)
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
@@ -309,36 +300,36 @@ func TestDecodeHandlesShortByteSlice(t *testing.T) {
|
||||
|
||||
func TestDecodeParsesValidByteSlice(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||
err := p.Decode(data)
|
||||
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if p.command_length != 16 {
|
||||
t.Errorf("Expected command_length to be 16, got %d", p.command_length)
|
||||
if p.Command_length != 16 {
|
||||
t.Errorf("Expected command_length to be 16, got %d", p.Command_length)
|
||||
}
|
||||
if p.command_id != 1 {
|
||||
t.Errorf("Expected command_id to be 1, got %d", p.command_id)
|
||||
if p.Command_id != 1 {
|
||||
t.Errorf("Expected command_id to be 1, got %d", p.Command_id)
|
||||
}
|
||||
if p.command_status != 2 {
|
||||
t.Errorf("Expected command_status to be 2, got %d", p.command_status)
|
||||
if p.Command_status != 2 {
|
||||
t.Errorf("Expected command_status to be 2, got %d", p.Command_status)
|
||||
}
|
||||
if p.sequence_number != 3 {
|
||||
t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number)
|
||||
if p.Sequence_number != 3 {
|
||||
t.Errorf("Expected sequence_number to be 3, got %d", p.Sequence_number)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := make([]uint8, 20)
|
||||
data := make([]byte, 20)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("Decode panicked with long byte slice")
|
||||
}
|
||||
}()
|
||||
err := p.Decode(data)
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
@@ -348,54 +339,54 @@ func TestDecodeHandlesNilDataInput(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
err := p.Decode(nil)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error, got none")
|
||||
}
|
||||
|
||||
if p.command_length != 0 {
|
||||
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
||||
if p.Command_length != 0 {
|
||||
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
||||
}
|
||||
if p.command_id != 0 {
|
||||
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
||||
if p.Command_id != 0 {
|
||||
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
||||
}
|
||||
if p.command_status != 0 {
|
||||
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
|
||||
if p.Command_status != 0 {
|
||||
t.Errorf("Expected command_status to be 0, got %d", p.Command_status)
|
||||
}
|
||||
if p.sequence_number != 0 {
|
||||
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
|
||||
if p.Sequence_number != 0 {
|
||||
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := []uint8{}
|
||||
err := p.Decode(data)
|
||||
data := []byte{}
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if p.command_length != 0 {
|
||||
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
||||
if p.Command_length != 0 {
|
||||
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
||||
}
|
||||
if p.command_id != 0 {
|
||||
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
||||
if p.Command_id != 0 {
|
||||
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
||||
}
|
||||
if p.command_status != 0 {
|
||||
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
|
||||
if p.Command_status != 0 {
|
||||
t.Errorf("Expected command_status to be 0, got %d", p.Command_status)
|
||||
}
|
||||
if p.sequence_number != 0 {
|
||||
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
|
||||
if p.Sequence_number != 0 {
|
||||
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||
originalData := make([]uint8, len(data))
|
||||
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||
originalData := make([]byte, len(data))
|
||||
copy(originalData, data)
|
||||
|
||||
err := p.Decode(data)
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
@@ -410,35 +401,35 @@ func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
|
||||
|
||||
func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := []uint8{255, 255, 255, 255, 255, 255, 255, 255}
|
||||
err := p.Decode(data)
|
||||
data := []byte{255, 255, 255, 255, 255, 255, 255, 255}
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if p.command_length != math.MaxUint32 {
|
||||
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
|
||||
if p.Command_length != math.MaxUint32 {
|
||||
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.Command_length)
|
||||
}
|
||||
if p.command_id != math.MaxUint32 {
|
||||
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id)
|
||||
if p.Command_id != math.MaxUint32 {
|
||||
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.Command_id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
|
||||
var p PDU_HEADER
|
||||
data := []uint8{0, 0, 0, 0, 0, 0, 0, 0}
|
||||
err := p.Decode(data)
|
||||
data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
|
||||
err := p.Decode(bytes.NewBuffer(data))
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if p.command_length != 0 {
|
||||
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
||||
if p.Command_length != 0 {
|
||||
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
||||
}
|
||||
if p.command_id != 0 {
|
||||
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
||||
if p.Command_id != 0 {
|
||||
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,3 +440,47 @@ func TestSizeReturns16(t *testing.T) {
|
||||
t.Errorf("Expected size to be 16, got %d", p.Size())
|
||||
}
|
||||
}
|
||||
|
||||
// region benchmarks
|
||||
// With buffer pool
|
||||
func BenchmarkEncodeWithBufferPool(b *testing.B) {
|
||||
p := &PDU_HEADER{
|
||||
Command_length: 16,
|
||||
Command_id: 1,
|
||||
Command_status: 0,
|
||||
Sequence_number: 12345,
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := ByteBufferPool.Get(p.Size())
|
||||
p.Encode(buf)
|
||||
ByteBufferPool.Put(buf)
|
||||
}
|
||||
}
|
||||
|
||||
// Without buffer pool
|
||||
func BenchmarkEncodeWithoutBufferPool(b *testing.B) {
|
||||
p := &PDU_HEADER{
|
||||
Command_length: 16,
|
||||
Command_id: 1,
|
||||
Command_status: 0,
|
||||
Sequence_number: 12345,
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := bytes.Buffer{}
|
||||
p.Encode(&buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecodeBufferPool(b *testing.B) {
|
||||
p := &PDU_HEADER{}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||
buf := ByteBufferPool.Get(len(data))
|
||||
buf.Write(data)
|
||||
p.Decode(buf)
|
||||
ByteBufferPool.Put(buf)
|
||||
}
|
||||
}
|
||||
|
176
pdu/submit.go
176
pdu/submit.go
@@ -1,104 +1,154 @@
|
||||
package pdu
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"smpptester/encoding"
|
||||
)
|
||||
|
||||
type (
|
||||
SUBMIT_SM struct {
|
||||
header PDU_HEADER
|
||||
service_type string
|
||||
source_addr_ton uint8
|
||||
source_addr_npi uint8
|
||||
source_addr string
|
||||
dest_addr_ton uint8
|
||||
dest_addr_npi uint8
|
||||
destination_addr string
|
||||
esm_class uint8
|
||||
protocol_id uint8
|
||||
priority_flag uint8
|
||||
schedule_delivery_time string
|
||||
validity_period string
|
||||
registered_delivery uint8
|
||||
replace_if_present uint8
|
||||
data_coding uint8
|
||||
sm_default_msg_id uint8
|
||||
sm_length uint8
|
||||
short_message string
|
||||
Header *PDU_HEADER
|
||||
Service_type string
|
||||
Source_addr_ton byte
|
||||
Source_addr_npi byte
|
||||
Source_addr string
|
||||
Dest_addr_ton byte
|
||||
Dest_addr_npi byte
|
||||
Destination_addr string
|
||||
Esm_class byte
|
||||
Protocol_id byte
|
||||
Priority_flag byte
|
||||
Schedule_delivery_time string
|
||||
Validity_period string
|
||||
Registered_delivery byte
|
||||
Replace_if_present byte
|
||||
Data_coding byte
|
||||
Sm_default_msg_id byte
|
||||
Sm_length byte
|
||||
Short_message string
|
||||
// user_message_reference uint16
|
||||
// source_port uint16
|
||||
// source_addr_subunit uint8
|
||||
// source_addr_subunit byte
|
||||
// destination_port uint16
|
||||
// dest_addr_subunit uint8
|
||||
// dest_addr_subunit byte
|
||||
// sar_msg_ref_num uint16
|
||||
// sar_total_segments uint8
|
||||
// sar_segment_seqnum uint8
|
||||
// more_messages_to_send uint8
|
||||
// payload_type uint8
|
||||
// sar_total_segments byte
|
||||
// sar_segment_seqnum byte
|
||||
// more_messages_to_send byte
|
||||
// payload_type byte
|
||||
// message_payload string
|
||||
// privacy_indicator uint8
|
||||
// privacy_indicator byte
|
||||
// callback_num string
|
||||
// callback_num_pres uint8
|
||||
// callback_num_pres byte
|
||||
// callback_num_atag string
|
||||
// source_subaddress string
|
||||
// dest_subaddress string
|
||||
// user_response_code uint8
|
||||
// display_time uint8
|
||||
// sms_signal uint8
|
||||
// ms_validity uint8
|
||||
// ms_msg_wait_facilities uint8
|
||||
// number_of_messages uint8
|
||||
// alert_on_message_delivery uint8
|
||||
// language_indicator uint8
|
||||
// its_reply_type uint8
|
||||
// its_session_info uint8
|
||||
// ussd_service_op uint8
|
||||
// user_response_code byte
|
||||
// display_time byte
|
||||
// sms_signal byte
|
||||
// ms_validity byte
|
||||
// ms_msg_wait_facilities byte
|
||||
// number_of_messages byte
|
||||
// alert_on_message_delivery byte
|
||||
// language_indicator byte
|
||||
// its_reply_type byte
|
||||
// its_session_info byte
|
||||
// ussd_service_op byte
|
||||
}
|
||||
SUBMIT_SM_RESP struct {
|
||||
header PDU_HEADER
|
||||
message_id string
|
||||
Header *PDU_HEADER
|
||||
Message_id string
|
||||
}
|
||||
SUBMIT_MULTI struct{}
|
||||
SUBMIT_MULTI_RESP struct{}
|
||||
)
|
||||
|
||||
func (p *SUBMIT_SM) Encode() ([]byte, error) {
|
||||
buf := make([]byte, p.Size())
|
||||
err := p.EncodeInto(&buf)
|
||||
return buf, err
|
||||
}
|
||||
func (p *SUBMIT_SM) EncodeInto(buf *[]byte) error {
|
||||
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
|
||||
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
|
||||
|
||||
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot encode SUBMIT_SM, buffer is nil")
|
||||
return fmt.Errorf("cannot encode into nil buffer")
|
||||
}
|
||||
if len(*buf) < int(p.Size()) {
|
||||
return fmt.Errorf("cannot encode SUBMIT_SM, buffer too small (%d, required %d)", len(*buf), p.Size())
|
||||
messageEncoder := p.GetEncoder()
|
||||
|
||||
p.Header.Encode(buf)
|
||||
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
||||
buf.WriteString(p.Service_type)
|
||||
buf.Write(NULL_ARR)
|
||||
binary.Write(buf, binary.BigEndian, p.Source_addr_ton)
|
||||
binary.Write(buf, binary.BigEndian, p.Source_addr_npi)
|
||||
buf.WriteString(p.Source_addr)
|
||||
buf.Write(NULL_ARR)
|
||||
binary.Write(buf, binary.BigEndian, p.Dest_addr_ton)
|
||||
binary.Write(buf, binary.BigEndian, p.Dest_addr_npi)
|
||||
buf.WriteString(p.Destination_addr)
|
||||
buf.Write(NULL_ARR)
|
||||
binary.Write(buf, binary.BigEndian, p.Esm_class)
|
||||
binary.Write(buf, binary.BigEndian, p.Protocol_id)
|
||||
binary.Write(buf, binary.BigEndian, p.Priority_flag)
|
||||
buf.WriteString(p.Schedule_delivery_time)
|
||||
buf.Write(NULL_ARR)
|
||||
buf.WriteString(p.Validity_period)
|
||||
buf.Write(NULL_ARR)
|
||||
binary.Write(buf, binary.BigEndian, p.Registered_delivery)
|
||||
binary.Write(buf, binary.BigEndian, p.Replace_if_present)
|
||||
binary.Write(buf, binary.BigEndian, p.Data_coding)
|
||||
binary.Write(buf, binary.BigEndian, p.Sm_default_msg_id)
|
||||
binary.Write(buf, binary.BigEndian, p.Sm_length)
|
||||
err := messageEncoder.Encode(&p.Short_message, buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.header.EncodeInto(buf)
|
||||
|
||||
return nil
|
||||
}
|
||||
func (p *SUBMIT_SM) Decode(data []byte) {
|
||||
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot decode nil buffer")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (p *SUBMIT_SM) Size() uint32 {
|
||||
var size uint32
|
||||
size += p.header.Size()
|
||||
size += uint32(len(p.service_type) * 1)
|
||||
func (p *SUBMIT_SM) Size() int {
|
||||
var size int
|
||||
size += p.Header.Size()
|
||||
size += 1 + len(p.Service_type)
|
||||
size += 1 // source_addr_ton
|
||||
size += 1 // source_addr_npi
|
||||
size += uint32(len(p.source_addr) * 1)
|
||||
size += 1 + len(p.Source_addr)
|
||||
size += 1 // dest_addr_ton
|
||||
size += 1 // dest_addr_npi
|
||||
size += uint32(len(p.destination_addr) * 1)
|
||||
size += 1 + len(p.Destination_addr)
|
||||
size += 1 // esm_class
|
||||
size += 1 // protocol_id
|
||||
size += 1 // priority_flag
|
||||
size += uint32(len(p.schedule_delivery_time) * 1)
|
||||
size += uint32(len(p.validity_period) * 1)
|
||||
size += 1 + len(p.Schedule_delivery_time)
|
||||
size += 1 + len(p.Validity_period)
|
||||
size += 1 // registered_delivery
|
||||
size += 1 // replace_if_present
|
||||
size += 1 // data_coding
|
||||
size += 1 // sm_default_msg_id
|
||||
size += 1 // sm_length
|
||||
size += uint32(len(p.short_message) * 1)
|
||||
size += p.GetEncoder().EncodesInto(&p.Short_message)
|
||||
return size
|
||||
}
|
||||
func (p *SUBMIT_SM) UpdateSize() {
|
||||
p.Header.Command_length = uint32(p.Size())
|
||||
p.Sm_length = byte(p.GetEncoder().EncodesInto(&p.Short_message))
|
||||
}
|
||||
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
|
||||
switch p.Data_coding {
|
||||
case 0b00000000: // GSM7
|
||||
return &encoding.GSM7Coder{}
|
||||
case 0b00000001: // ASCII
|
||||
return &encoding.ASCIICoder{}
|
||||
// case 0b00000011: // LATIN1
|
||||
// return &encoding.LATIN1Coder{}
|
||||
case 0b00001000: // UCS2
|
||||
return &encoding.UCS2Coder{}
|
||||
default:
|
||||
return &encoding.ASCIICoder{}
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,119 @@
|
||||
package pdu
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// region encode
|
||||
// See examples: https://www.openmarket.com/docs/Content/apis/v4smpp/mt-examples.htm
|
||||
func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
|
||||
p := &SUBMIT_SM{
|
||||
Header: &PDU_HEADER{
|
||||
Command_length: 0,
|
||||
Command_id: 4,
|
||||
Command_status: 0,
|
||||
Sequence_number: 378019,
|
||||
},
|
||||
Service_type: "OMV4",
|
||||
Source_addr_ton: 3,
|
||||
Source_addr_npi: 1,
|
||||
Source_addr: "80362",
|
||||
Dest_addr_ton: 1,
|
||||
Dest_addr_npi: 1,
|
||||
Destination_addr: "812345001000",
|
||||
Esm_class: 0,
|
||||
Protocol_id: 0,
|
||||
Priority_flag: 0,
|
||||
Schedule_delivery_time: "",
|
||||
Validity_period: "180105120000004+",
|
||||
Registered_delivery: 1,
|
||||
Data_coding: 1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
|
||||
Sm_default_msg_id: 0,
|
||||
Short_message: "Reply Yes to opt in or No to opt out.",
|
||||
}
|
||||
p.UpdateSize()
|
||||
buf := ByteBufferPool.Get(p.Size())
|
||||
err := p.Encode(buf)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
expected := []byte{0, 0, 0, 107, 0, 0, 0, 4, 0, 0, 0, 0, 0, 5, 196, 163, 79, 77, 86, 52, 0, 3, 1, 56, 48, 51, 54, 50, 0, 1, 1, 56, 49, 50, 51, 52, 53, 48, 48, 49, 48, 48, 48, 0, 0, 0, 0, 0, 49, 56, 48, 49, 48, 53, 49, 50, 48, 48, 48, 48, 48, 48, 52, 43, 0, 1, 0, 1, 0, 37, 82, 101, 112, 108, 121, 32, 89, 101, 115, 32, 116, 111, 32, 111, 112, 116, 32, 105, 110, 32, 111, 114, 32, 78, 111, 32, 116, 111, 32, 111, 112, 116, 32, 111, 117, 116, 46}
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
|
||||
p := &SUBMIT_SM{
|
||||
Header: &PDU_HEADER{
|
||||
Command_length: 0,
|
||||
Command_id: 4,
|
||||
Command_status: 0,
|
||||
Sequence_number: 378019,
|
||||
},
|
||||
Service_type: "OMV4",
|
||||
Source_addr_ton: 3,
|
||||
Source_addr_npi: 1,
|
||||
Source_addr: "80362",
|
||||
Dest_addr_ton: 1,
|
||||
Dest_addr_npi: 1,
|
||||
Destination_addr: "812345001000",
|
||||
Esm_class: 0,
|
||||
Protocol_id: 0,
|
||||
Priority_flag: 0,
|
||||
Schedule_delivery_time: "",
|
||||
Validity_period: "180105120000004+",
|
||||
Registered_delivery: 1,
|
||||
Data_coding: 0, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
|
||||
Sm_default_msg_id: 0,
|
||||
Short_message: "Reply Yes to opt in or No to opt out.",
|
||||
}
|
||||
p.UpdateSize()
|
||||
buf := ByteBufferPool.Get(p.Size())
|
||||
err := p.Encode(buf)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
expected := []byte{0, 0, 0, 103, 0, 0, 0, 4, 0, 0, 0, 0, 0, 5, 196, 163, 79, 77, 86, 52, 0, 3, 1, 56, 48, 51, 54, 50, 0, 1, 1, 56, 49, 50, 51, 52, 53, 48, 48, 49, 48, 48, 48, 0, 0, 0, 0, 0, 49, 56, 48, 49, 48, 53, 49, 50, 48, 48, 48, 48, 48, 48, 52, 43, 0, 1, 0, 0, 0, 33, 210, 50, 156, 157, 7, 101, 203, 115, 16, 253, 13, 122, 195, 233, 160, 180, 27, 244, 150, 131, 156, 111, 16, 253, 13, 122, 195, 233, 160, 119, 157, 238, 2}
|
||||
if !bytes.Equal(buf.Bytes(), expected) {
|
||||
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
// region decode
|
||||
|
||||
// region size
|
||||
func TestCalculateSizeTypicalInstance(t *testing.T) {
|
||||
p := &SUBMIT_SM{
|
||||
service_type: "test_service",
|
||||
source_addr: "12345",
|
||||
destination_addr: "67890",
|
||||
schedule_delivery_time: "",
|
||||
validity_period: "",
|
||||
short_message: "Hello, World!",
|
||||
Service_type: "test_service",
|
||||
Source_addr: "12345",
|
||||
Destination_addr: "67890",
|
||||
Schedule_delivery_time: "",
|
||||
Validity_period: "",
|
||||
Short_message: "Hello, World!",
|
||||
Data_coding: 1,
|
||||
}
|
||||
expectedSize := uint32(16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message))
|
||||
expectedSize := 68
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateSizeTypicalGSM7Instance(t *testing.T) {
|
||||
p := &SUBMIT_SM{
|
||||
Service_type: "test_service",
|
||||
Source_addr: "12345",
|
||||
Destination_addr: "67890",
|
||||
Schedule_delivery_time: "",
|
||||
Validity_period: "",
|
||||
Short_message: "Hello, World!",
|
||||
Data_coding: 0,
|
||||
}
|
||||
expectedSize := 67
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
@@ -22,14 +123,15 @@ func TestCalculateSizeTypicalInstance(t *testing.T) {
|
||||
func TestCalculateSizeMaxLengths(t *testing.T) {
|
||||
maxLen := 255
|
||||
p := &SUBMIT_SM{
|
||||
service_type: string(make([]byte, maxLen)),
|
||||
source_addr: string(make([]byte, maxLen)),
|
||||
destination_addr: string(make([]byte, maxLen)),
|
||||
schedule_delivery_time: string(make([]byte, maxLen)),
|
||||
validity_period: string(make([]byte, maxLen)),
|
||||
short_message: string(make([]byte, maxLen)),
|
||||
Service_type: string(make([]byte, maxLen)),
|
||||
Source_addr: string(make([]byte, maxLen)),
|
||||
Destination_addr: string(make([]byte, maxLen)),
|
||||
Schedule_delivery_time: string(make([]byte, maxLen)),
|
||||
Validity_period: string(make([]byte, maxLen)),
|
||||
Short_message: string(make([]byte, maxLen)),
|
||||
Data_coding: 1,
|
||||
}
|
||||
expectedSize := uint32(16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen)
|
||||
expectedSize := 1563
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
@@ -38,16 +140,18 @@ func TestCalculateSizeMaxLengths(t *testing.T) {
|
||||
|
||||
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
|
||||
p := &SUBMIT_SM{
|
||||
service_type: "",
|
||||
source_addr: "",
|
||||
destination_addr: "",
|
||||
schedule_delivery_time: "",
|
||||
validity_period: "",
|
||||
short_message: "",
|
||||
Service_type: "",
|
||||
Source_addr: "",
|
||||
Destination_addr: "",
|
||||
Schedule_delivery_time: "",
|
||||
Validity_period: "",
|
||||
Short_message: "",
|
||||
}
|
||||
expectedSize := uint32(16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message))
|
||||
expectedSize := 33
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
}
|
||||
}
|
||||
|
||||
// region benchmark
|
||||
|
@@ -1 +1,66 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type SMPPServer struct {
|
||||
Id int
|
||||
conns []*net.Conn
|
||||
listener net.Listener
|
||||
port string
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func NewSMPPServer(port string, id int) *SMPPServer {
|
||||
server := &SMPPServer{
|
||||
port: port,
|
||||
log: log.Logger{},
|
||||
Id: id,
|
||||
}
|
||||
server.log = *log.New(log.Writer(), "", log.LstdFlags)
|
||||
server.log.SetPrefix(fmt.Sprintf("SMPP server %d: ", server.Id))
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *SMPPServer) Listen() error {
|
||||
listener, err := net.Listen("tcp", s.port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to start SMPP server: %w", err)
|
||||
}
|
||||
// s.log.Printf("SMPP server %d started on %s", s.port)
|
||||
s.listener = listener
|
||||
return nil
|
||||
}
|
||||
|
||||
// package main
|
||||
|
||||
// import (
|
||||
// "net"
|
||||
// "fmt"
|
||||
// )
|
||||
|
||||
// func main() {
|
||||
// ln, err := net.Listen("tcp", ":8080")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
// defer ln.Close()
|
||||
|
||||
// for {
|
||||
// conn, err := ln.Accept()
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// continue
|
||||
// }
|
||||
// go handleConnection(conn)
|
||||
// }
|
||||
// }
|
||||
|
||||
// func handleConnection(conn net.Conn) {
|
||||
// defer conn.Close()
|
||||
// // Handle the connection
|
||||
// }
|
||||
|
36
utils/reactivevalue.go
Normal file
36
utils/reactivevalue.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package utils
|
||||
|
||||
type ReactiveValue[T any] struct {
|
||||
V T
|
||||
subscribers []chan *T
|
||||
}
|
||||
|
||||
func NewReactiveValue[T any](value T) *ReactiveValue[T] {
|
||||
return &ReactiveValue[T]{
|
||||
V: value,
|
||||
subscribers: make([]chan *T, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (rv *ReactiveValue[T]) Set(value T) {
|
||||
rv.V = value
|
||||
for _, subscriber := range rv.subscribers {
|
||||
subscriber <- &value
|
||||
}
|
||||
}
|
||||
|
||||
func (rv *ReactiveValue[T]) Subscribe() chan *T {
|
||||
subscriber := make(chan *T)
|
||||
rv.subscribers = append(rv.subscribers, subscriber)
|
||||
return subscriber
|
||||
}
|
||||
|
||||
func (rv *ReactiveValue[T]) Unsubscribe(subscriber chan *T) {
|
||||
for i, s := range rv.subscribers {
|
||||
if s == subscriber {
|
||||
rv.subscribers = append(rv.subscribers[:i], rv.subscribers[i+1:]...)
|
||||
close(subscriber)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user