Compare commits
5 Commits
benchmark
...
500cb11235
Author | SHA1 | Date | |
---|---|---|---|
500cb11235 | |||
ec04fa1fb6 | |||
a12c22587d | |||
f30680c26f | |||
bed69fbfd3 |
1
main.go
1
main.go
@@ -136,6 +136,7 @@ func main() {
|
||||
// go handleConnection(conn)
|
||||
// }
|
||||
// }()
|
||||
log.Println(createSubmitSMPDU())
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
@@ -6,15 +6,15 @@ type (
|
||||
system_id string
|
||||
password string
|
||||
system_type string
|
||||
interface_version uint8
|
||||
addr_ton uint8
|
||||
addr_npi uint8
|
||||
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
|
||||
sc_interface_version byte
|
||||
}
|
||||
BIND_RECVEIVER struct {
|
||||
BIND
|
||||
|
@@ -1,21 +1,30 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type BufferPoolManager struct {
|
||||
pools map[uint]*sync.Pool
|
||||
pools map[int]*sync.Pool
|
||||
mu sync.RWMutex
|
||||
debug bool
|
||||
}
|
||||
|
||||
func (bpm *BufferPoolManager) logf(format string, args ...interface{}) {
|
||||
if bpm.debug {
|
||||
log.Printf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func NewBufferPoolManager() *BufferPoolManager {
|
||||
return &BufferPoolManager{
|
||||
pools: make(map[uint]*sync.Pool),
|
||||
pools: make(map[int]*sync.Pool),
|
||||
}
|
||||
}
|
||||
|
||||
func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
|
||||
func (bpm *BufferPoolManager) Get(size int) *bytes.Buffer {
|
||||
bpm.mu.RLock()
|
||||
pool, exists := bpm.pools[size]
|
||||
bpm.mu.RUnlock()
|
||||
@@ -25,10 +34,13 @@ func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
|
||||
// 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
|
||||
@@ -36,23 +48,23 @@ func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
|
||||
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) Put(buf *[]uint8) {
|
||||
size := uint(len(*buf))
|
||||
func (bpm *BufferPoolManager) Put(buf *bytes.Buffer) {
|
||||
size := buf.Cap()
|
||||
bpm.mu.RLock()
|
||||
pool, exists := bpm.pools[size]
|
||||
bpm.mu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
bpm.logf("Cannot return %p, No pool for size %d\n", buf, size)
|
||||
return
|
||||
}
|
||||
|
||||
// Clear buffer
|
||||
for i := range *buf {
|
||||
(*buf)[i] = 0
|
||||
}
|
||||
|
||||
buf.Reset()
|
||||
pool.Put(buf)
|
||||
bpm.logf("Returned buffer of size %d: %p\n", size, buf)
|
||||
}
|
||||
|
@@ -10,14 +10,14 @@ func TestRetrieveBufferOfRequestedSize(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,14 +25,14 @@ func TestRequestBufferSizeZero(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 0
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,12 +47,12 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -64,14 +64,14 @@ func TestGetBufferLockUnlock(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,14 +79,14 @@ func TestVerifyPoolCreationForNewSizes(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 512
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,14 +94,14 @@ func TestBufferPoolManagerGetBuffer(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
size := 1024
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,14 +110,14 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
|
||||
|
||||
sizes := []int{512, 1024, 2048}
|
||||
for _, size := range sizes {
|
||||
buffer := bpm.Get(uint(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())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
|
||||
func TestGetBufferIsAlwaysZero(t *testing.T) {
|
||||
bpm := NewBufferPoolManager()
|
||||
|
||||
var size uint = 1024 * 64
|
||||
size := 1024 * 64
|
||||
for i := 0; i < 1000; i++ {
|
||||
buffer := bpm.Get(size)
|
||||
|
||||
@@ -133,11 +133,11 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
|
||||
t.Fatalf("Expected buffer for size %d, got nil", size)
|
||||
}
|
||||
|
||||
if uint(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())
|
||||
}
|
||||
|
||||
for _, b := range *buffer {
|
||||
for _, b := range buffer.Bytes() {
|
||||
if b != 0 {
|
||||
t.Errorf("Expected buffer to be zero, got %d", b)
|
||||
}
|
||||
@@ -147,9 +147,26 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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 := uint(128 * 1024) // a PDU should not be larger than this... Even this is way too large
|
||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
@@ -163,7 +180,7 @@ func BenchmarkBufferPoolManager(b *testing.B) {
|
||||
|
||||
func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
|
||||
bpm := NewBufferPoolManager()
|
||||
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is vway too large
|
||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
@@ -186,17 +203,17 @@ func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
|
||||
|
||||
func BenchmarkBufferPoolManager_Memory(b *testing.B) {
|
||||
bpm := NewBufferPoolManager()
|
||||
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is vway too large
|
||||
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
|
||||
b.ResetTimer()
|
||||
|
||||
var i uint8
|
||||
var i byte
|
||||
buf := bpm.Get(bufSize)
|
||||
b.StopTimer()
|
||||
|
||||
// Simulate some work
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
for k := range *buf {
|
||||
(*buf)[k] = i % 255
|
||||
for range buf.Bytes() {
|
||||
buf.WriteByte(i % 255)
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
|
@@ -5,11 +5,11 @@ type (
|
||||
header PDU_HEADER
|
||||
service_type string
|
||||
message_id string
|
||||
source_addr_ton uint8
|
||||
source_addr_npi uint8
|
||||
source_addr_ton byte
|
||||
source_addr_npi byte
|
||||
source_addr string
|
||||
dest_addr_ton uint8
|
||||
dest_addr_npi uint8
|
||||
dest_addr_ton byte
|
||||
dest_addr_npi byte
|
||||
destination_addr string
|
||||
}
|
||||
CANCEL_SM_RESP struct {
|
||||
|
52
pdu/pdu.go
52
pdu/pdu.go
@@ -1,17 +1,17 @@
|
||||
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
|
||||
}
|
||||
|
||||
PDU_HEADER struct {
|
||||
@@ -57,41 +57,23 @@ type (
|
||||
// What are the other 0s?
|
||||
// Don't know
|
||||
|
||||
func (p *PDU_HEADER) Encode() (*[]uint8, error) {
|
||||
buf := ByteBufferPool.Get(uint(p.Size()))
|
||||
err := p.EncodeInto(buf)
|
||||
return buf, err
|
||||
func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
|
||||
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) EncodeInto(buf *[]uint8) error {
|
||||
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot encode PDU_HEADER, buffer is nil")
|
||||
return fmt.Errorf("cannot decode nil buffer")
|
||||
}
|
||||
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)
|
||||
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) 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])
|
||||
|
||||
}
|
||||
if len(data) >= 16 {
|
||||
p.sequence_number = binary.BigEndian.Uint32(data[12:16])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (p *PDU_HEADER) Size() uint32 {
|
||||
func (p *PDU_HEADER) Size() int {
|
||||
return 16
|
||||
}
|
||||
|
300
pdu/pdu_test.go
300
pdu/pdu_test.go
@@ -1,6 +1,7 @@
|
||||
package pdu
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"sync"
|
||||
@@ -15,13 +16,15 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,15 +35,17 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -53,16 +58,18 @@ func TestEncodeEncodesProperly(t *testing.T) {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -75,16 +82,18 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -97,54 +106,26 @@ func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,24 +136,26 @@ func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,24 +166,26 @@ func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,28 +196,30 @@ func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,24 +230,26 @@ func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,38 +260,83 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
// func TestRealScenario(t *testing.T) {
|
||||
// expected := []byte{0, 0, 0, 54, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 67, 77, 84, 0, 1, 1, 49, 50, 51, 52, 53, 0, 1, 1, 54, 55, 56, 57, 48, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 72, 101, 108, 108, 111, 44, 32, 83, 77, 80, 80, 33}
|
||||
// p := &SUBMIT_SM{
|
||||
// header: PDU_HEADER{
|
||||
// command_length: 0,
|
||||
// command_id: 4,
|
||||
// command_status: 0,
|
||||
// sequence_number: 1,
|
||||
// },
|
||||
// service_type: "CMT",
|
||||
// source_addr_ton: 1,
|
||||
// source_addr_npi: 1,
|
||||
// source_addr: "12345",
|
||||
// dest_addr_ton: 1,
|
||||
// dest_addr_npi: 1,
|
||||
// destination_addr: "67890",
|
||||
// esm_class: 0,
|
||||
// protocol_id: 0,
|
||||
// priority_flag: 0,
|
||||
// schedule_delivery_time: "",
|
||||
// validity_period: "",
|
||||
// registered_delivery: 1,
|
||||
// data_coding: 0,
|
||||
// sm_default_msg_id: 0,
|
||||
// short_message: "Hello, SMPP!",
|
||||
// }
|
||||
// p.header.command_length = uint32(p.Size())
|
||||
// p.sm_length = byte(len(p.short_message))
|
||||
// buf := make([]byte, p.Size())
|
||||
// err := p.EncodeInto(&buf)
|
||||
// if err != nil {
|
||||
// t.Errorf("Expected no error, got %v", err)
|
||||
// }
|
||||
// if len(buf) != len(expected) {
|
||||
// t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf))
|
||||
// }
|
||||
// for i, v := range buf {
|
||||
// if v != expected[i] {
|
||||
// t.Errorf("Expected byte slice with values %v, got %v", expected, buf)
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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)
|
||||
}
|
||||
@@ -310,8 +344,8 @@ 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)
|
||||
@@ -333,13 +367,13 @@ func TestDecodeParsesValidByteSlice(t *testing.T) {
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -349,8 +383,8 @@ 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 {
|
||||
@@ -369,8 +403,8 @@ func TestDecodeHandlesNilDataInput(t *testing.T) {
|
||||
|
||||
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)
|
||||
@@ -392,11 +426,11 @@ func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
|
||||
|
||||
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)
|
||||
@@ -411,8 +445,8 @@ 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)
|
||||
@@ -428,8 +462,8 @@ func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
|
||||
|
||||
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)
|
||||
@@ -452,9 +486,8 @@ func TestSizeReturns16(t *testing.T) {
|
||||
}
|
||||
|
||||
// region benchmarks
|
||||
|
||||
// With buffer pool
|
||||
func BenchmarkEncode(b *testing.B) {
|
||||
func BenchmarkEncodeWithBufferPool(b *testing.B) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
@@ -463,12 +496,14 @@ func BenchmarkEncode(b *testing.B) {
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
p.Encode()
|
||||
buf := ByteBufferPool.Get(p.Size())
|
||||
p.Encode(buf)
|
||||
ByteBufferPool.Put(buf)
|
||||
}
|
||||
}
|
||||
|
||||
// Without buffer pool
|
||||
func BenchmarkEncodeInto(b *testing.B) {
|
||||
func BenchmarkEncodeWithoutBufferPool(b *testing.B) {
|
||||
p := &PDU_HEADER{
|
||||
command_length: 16,
|
||||
command_id: 1,
|
||||
@@ -477,16 +512,19 @@ func BenchmarkEncodeInto(b *testing.B) {
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := make([]uint8, 16)
|
||||
p.EncodeInto(&buf)
|
||||
buf := bytes.Buffer{}
|
||||
p.Encode(&buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecode(b *testing.B) {
|
||||
func BenchmarkDecodeBufferPool(b *testing.B) {
|
||||
p := &PDU_HEADER{}
|
||||
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
p.Decode(data)
|
||||
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)
|
||||
}
|
||||
}
|
123
pdu/submit.go
123
pdu/submit.go
@@ -1,56 +1,60 @@
|
||||
package pdu
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/ascii85"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
type (
|
||||
SUBMIT_SM struct {
|
||||
header PDU_HEADER
|
||||
service_type string
|
||||
source_addr_ton uint8
|
||||
source_addr_npi uint8
|
||||
source_addr_ton byte
|
||||
source_addr_npi byte
|
||||
source_addr string
|
||||
dest_addr_ton uint8
|
||||
dest_addr_npi uint8
|
||||
dest_addr_ton byte
|
||||
dest_addr_npi byte
|
||||
destination_addr string
|
||||
esm_class uint8
|
||||
protocol_id uint8
|
||||
priority_flag uint8
|
||||
esm_class byte
|
||||
protocol_id byte
|
||||
priority_flag byte
|
||||
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
|
||||
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
|
||||
@@ -60,45 +64,56 @@ type (
|
||||
SUBMIT_MULTI_RESP struct{}
|
||||
)
|
||||
|
||||
func (p *SUBMIT_SM) Encode() (*[]byte, error) {
|
||||
buf := ByteBufferPool.Get(uint(p.Size()))
|
||||
err := p.EncodeInto(buf)
|
||||
return buf, err
|
||||
}
|
||||
func (p *SUBMIT_SM) EncodeInto(buf *[]byte) error {
|
||||
if buf == nil {
|
||||
return fmt.Errorf("cannot encode SUBMIT_SM, buffer is nil")
|
||||
}
|
||||
if len(*buf) < int(p.Size()) {
|
||||
return fmt.Errorf("cannot encode SUBMIT_SM, buffer too small (%d, required %d)", len(*buf), p.Size())
|
||||
}
|
||||
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
||||
p.header.Encode(buf)
|
||||
n := ascii85.Encode(buf.Bytes(), []byte(p.service_type))
|
||||
buf.Truncate(n)
|
||||
binary.Write(buf, binary.BigEndian, byte(len(p.service_type)))
|
||||
|
||||
p.header.EncodeInto(buf)
|
||||
// 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
|
||||
|
||||
return nil
|
||||
}
|
||||
func (p *SUBMIT_SM) Decode(data []byte) {
|
||||
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
||||
return nil
|
||||
}
|
||||
func (p *SUBMIT_SM) Size() uint32 {
|
||||
var size uint32
|
||||
func (p *SUBMIT_SM) Size() int {
|
||||
var size int
|
||||
size += p.header.Size()
|
||||
size += uint32(len(p.service_type) * 1)
|
||||
size += len(p.service_type) * 1
|
||||
size += 1 // source_addr_ton
|
||||
size += 1 // source_addr_npi
|
||||
size += uint32(len(p.source_addr) * 1)
|
||||
size += len(p.source_addr) * 1
|
||||
size += 1 // dest_addr_ton
|
||||
size += 1 // dest_addr_npi
|
||||
size += uint32(len(p.destination_addr) * 1)
|
||||
size += len(p.destination_addr) * 1
|
||||
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 += len(p.schedule_delivery_time) * 1
|
||||
size += len(p.validity_period) * 1
|
||||
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 += len(p.short_message) * 1
|
||||
return size
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ func TestCalculateSizeTypicalInstance(t *testing.T) {
|
||||
validity_period: "",
|
||||
short_message: "Hello, World!",
|
||||
}
|
||||
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 := 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)
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
@@ -31,7 +31,7 @@ func TestCalculateSizeMaxLengths(t *testing.T) {
|
||||
validity_period: string(make([]byte, maxLen)),
|
||||
short_message: string(make([]byte, maxLen)),
|
||||
}
|
||||
expectedSize := uint32(16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen)
|
||||
expectedSize := 16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
@@ -47,7 +47,7 @@ func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
|
||||
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 := 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)
|
||||
actualSize := p.Size()
|
||||
if actualSize != expectedSize {
|
||||
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
|
||||
|
Reference in New Issue
Block a user