Compare commits
8 Commits
76bdc5214b
...
master
Author | SHA1 | Date | |
---|---|---|---|
78fc53a9a4 | |||
0315a0877b | |||
e8b9eb934b | |||
797e1f025b | |||
4d7e13a304 | |||
38af7b146d | |||
f1eb6b065d | |||
4771ffa154 |
@@ -2,8 +2,7 @@ name: Benchmark BufferPool
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- '*'
|
- 'master'
|
||||||
- '!benchmark'
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
RunBenchmarks:
|
RunBenchmarks:
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
jstester/node_modules
|
jstester/node_modules
|
||||||
encoding/tmp
|
encoding/tmp
|
||||||
pdu/tmp
|
pdu/tmp
|
||||||
|
tmp
|
||||||
|
166
client/client.go
166
client/client.go
@@ -1 +1,165 @@
|
|||||||
package client
|
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
|
||||||
|
}
|
||||||
|
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module smpptester
|
|||||||
|
|
||||||
go 1.22.4
|
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 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
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=
|
||||||
|
71
main.go
71
main.go
@@ -4,12 +4,15 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"smpptester/client"
|
||||||
|
"smpptester/pdu"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetFlags(log.Lmicroseconds)
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to create a submit_sm PDU
|
// Function to create a submit_sm PDU
|
||||||
@@ -29,7 +32,7 @@ func createSubmitSMPDU() []byte {
|
|||||||
validityPeriod := ""
|
validityPeriod := ""
|
||||||
registeredDelivery := byte(0x01)
|
registeredDelivery := byte(0x01)
|
||||||
replaceIfPresentFlag := byte(0x00)
|
replaceIfPresentFlag := byte(0x00)
|
||||||
dataCoding := byte(0x00)
|
dataCoding := byte(0x01)
|
||||||
smDefaultMsgID := byte(0x00)
|
smDefaultMsgID := byte(0x00)
|
||||||
shortMessage := "Hello, SMPP!"
|
shortMessage := "Hello, SMPP!"
|
||||||
|
|
||||||
@@ -37,7 +40,6 @@ func createSubmitSMPDU() []byte {
|
|||||||
headerLength := 16
|
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)
|
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
|
totalLength := headerLength + bodyLength
|
||||||
log.Printf("Total length: %d", totalLength)
|
|
||||||
|
|
||||||
// Create a buffer to hold the PDU
|
// Create a buffer to hold the PDU
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
@@ -136,28 +138,55 @@ func main() {
|
|||||||
// go handleConnection(conn)
|
// go handleConnection(conn)
|
||||||
// }
|
// }
|
||||||
// }()
|
// }()
|
||||||
log.Println(createSubmitSMPDU())
|
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 := &sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
conn, err := net.Dial("tcp", "localhost:2775")
|
client := client.NewSMPPClient("localhost:2775", 1)
|
||||||
if err != nil {
|
client.Enable()
|
||||||
log.Printf("Failed to connect to SMPP server: %+v", err)
|
for {
|
||||||
return
|
if client.Connected {
|
||||||
}
|
// err := client.Send(submit)
|
||||||
log.Printf("Connected to SMPP server")
|
// if err != nil {
|
||||||
defer conn.Close()
|
// log.Printf("Failed to send PDU: %v", err)
|
||||||
|
// }
|
||||||
go func() {
|
go client.Send(submit)
|
||||||
conn.Write(createSubmitSMPDU())
|
// if rand.Int() % 1000 == 0 {
|
||||||
data, err := conn.Read(make([]byte, 1024))
|
// client.SendLimit.Set(rand.Int() % 1000)
|
||||||
if err != nil {
|
// }
|
||||||
log.Printf("Failed to read from connection: %+v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
log.Printf("Received data: %#v", data)
|
time.Sleep(500 * time.Millisecond)
|
||||||
}()
|
}
|
||||||
|
log.Println("Are we done")
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
26
pdu/bind.go
26
pdu/bind.go
@@ -2,19 +2,19 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
BIND struct {
|
BIND struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
system_id string
|
System_id string
|
||||||
password string
|
Password string
|
||||||
system_type string
|
System_type string
|
||||||
interface_version byte
|
Interface_version byte
|
||||||
addr_ton byte
|
Addr_ton byte
|
||||||
addr_npi byte
|
Addr_npi byte
|
||||||
address_range string
|
Address_range string
|
||||||
}
|
}
|
||||||
BIND_RESP struct {
|
BIND_RESP struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
system_id string
|
System_id string
|
||||||
sc_interface_version byte
|
Sc_interface_version byte
|
||||||
}
|
}
|
||||||
BIND_RECVEIVER struct {
|
BIND_RECVEIVER struct {
|
||||||
BIND
|
BIND
|
||||||
@@ -36,9 +36,9 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
UNBIND struct {
|
UNBIND struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
}
|
}
|
||||||
UNBIND_RESP struct {
|
UNBIND_RESP struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -2,17 +2,17 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
CANCEL_SM struct {
|
CANCEL_SM struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
service_type string
|
Service_type string
|
||||||
message_id string
|
Message_id string
|
||||||
source_addr_ton byte
|
Source_addr_ton byte
|
||||||
source_addr_npi byte
|
Source_addr_npi byte
|
||||||
source_addr string
|
Source_addr string
|
||||||
dest_addr_ton byte
|
Dest_addr_ton byte
|
||||||
dest_addr_npi byte
|
Dest_addr_npi byte
|
||||||
destination_addr string
|
Destination_addr string
|
||||||
}
|
}
|
||||||
CANCEL_SM_RESP struct {
|
CANCEL_SM_RESP struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -2,11 +2,11 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
DELIVER_SM struct {
|
DELIVER_SM struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
SUBMIT_SM
|
SUBMIT_SM
|
||||||
}
|
}
|
||||||
DELIVER_SM_RESP struct {
|
DELIVER_SM_RESP struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
SUBMIT_SM_RESP
|
SUBMIT_SM_RESP
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@@ -2,9 +2,9 @@ package pdu
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
ENQUIRE_LINK struct {
|
ENQUIRE_LINK struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
}
|
}
|
||||||
ENQUIRE_LINK_RESP struct {
|
ENQUIRE_LINK_RESP struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
30
pdu/pdu.go
30
pdu/pdu.go
@@ -16,14 +16,14 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
PDU_HEADER struct {
|
PDU_HEADER struct {
|
||||||
command_length uint32
|
Command_length uint32
|
||||||
command_id uint32
|
Command_id uint32
|
||||||
command_status uint32
|
Command_status uint32
|
||||||
sequence_number uint32
|
Sequence_number uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
GENERIC_NACK struct {
|
GENERIC_NACK struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,25 +62,25 @@ func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
|
|||||||
if buf == nil {
|
if buf == nil {
|
||||||
return fmt.Errorf("cannot encode into nil buffer")
|
return fmt.Errorf("cannot encode into nil buffer")
|
||||||
}
|
}
|
||||||
binary.Write(buf, binary.BigEndian, p.command_length)
|
binary.Write(buf, binary.BigEndian, p.Command_length)
|
||||||
binary.Write(buf, binary.BigEndian, p.command_id)
|
binary.Write(buf, binary.BigEndian, p.Command_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.command_status)
|
binary.Write(buf, binary.BigEndian, p.Command_status)
|
||||||
binary.Write(buf, binary.BigEndian, p.sequence_number)
|
binary.Write(buf, binary.BigEndian, p.Sequence_number)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
|
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
return fmt.Errorf("cannot decode nil buffer")
|
return fmt.Errorf("cannot decode nil buffer")
|
||||||
}
|
}
|
||||||
binary.Read(buf, binary.BigEndian, &p.command_length)
|
binary.Read(buf, binary.BigEndian, &p.Command_length)
|
||||||
binary.Read(buf, binary.BigEndian, &p.command_id)
|
binary.Read(buf, binary.BigEndian, &p.Command_id)
|
||||||
binary.Read(buf, binary.BigEndian, &p.command_status)
|
binary.Read(buf, binary.BigEndian, &p.Command_status)
|
||||||
binary.Read(buf, binary.BigEndian, &p.sequence_number)
|
binary.Read(buf, binary.BigEndian, &p.Sequence_number)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *PDU_HEADER) Size() int {
|
func (p *PDU_HEADER) Size() int {
|
||||||
return 16
|
return 16
|
||||||
}
|
}
|
||||||
func (p *PDU_HEADER) UpdateSize() {
|
func (p *PDU_HEADER) UpdateSize() {
|
||||||
p.command_length = uint32(p.Size())
|
p.Command_length = uint32(p.Size())
|
||||||
}
|
}
|
||||||
|
256
pdu/pdu_test.go
256
pdu/pdu_test.go
@@ -11,10 +11,10 @@ import (
|
|||||||
// region encode
|
// region encode
|
||||||
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 1,
|
Command_length: 1,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 1,
|
Command_status: 1,
|
||||||
sequence_number: 1,
|
Sequence_number: 1,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -30,10 +30,10 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeHandlesZeroValues(t *testing.T) {
|
func TestEncodeHandlesZeroValues(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 0,
|
Command_length: 0,
|
||||||
command_id: 0,
|
Command_id: 0,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 0,
|
Sequence_number: 0,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -53,10 +53,10 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeEncodesProperly(t *testing.T) {
|
func TestEncodeEncodesProperly(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 1,
|
Command_length: 1,
|
||||||
command_id: 2,
|
Command_id: 2,
|
||||||
command_status: 3,
|
Command_status: 3,
|
||||||
sequence_number: 4,
|
Sequence_number: 4,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -77,10 +77,10 @@ func TestEncodeEncodesProperly(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 13426724,
|
Command_length: 13426724,
|
||||||
command_id: 254352,
|
Command_id: 254352,
|
||||||
command_status: 35634264,
|
Command_status: 35634264,
|
||||||
sequence_number: 476543523,
|
Sequence_number: 476543523,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -101,10 +101,10 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 16,
|
Command_length: 16,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 12345,
|
Sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -115,26 +115,26 @@ func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
innerbuf := buf.Bytes()
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
|
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]))
|
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
|
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]))
|
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
|
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]))
|
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
|
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]))
|
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 16,
|
Command_length: 16,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 12345,
|
Sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size
|
buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -145,26 +145,26 @@ func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
innerbuf := buf.Bytes()
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
|
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]))
|
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
|
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]))
|
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
|
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]))
|
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
|
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]))
|
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 16,
|
Command_length: 16,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 12345,
|
Sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -175,26 +175,26 @@ func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
innerbuf := buf.Bytes()
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
|
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]))
|
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
|
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]))
|
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
|
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]))
|
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
|
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]))
|
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 16,
|
Command_length: 16,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 12345,
|
Sequence_number: 12345,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -209,26 +209,26 @@ func TestEncodeIntoConcurrencySafety(t *testing.T) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
innerbuf := buf.Bytes()
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
|
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]))
|
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
|
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]))
|
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
|
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]))
|
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
|
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]))
|
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: math.MaxUint32,
|
Command_length: math.MaxUint32,
|
||||||
command_id: math.MaxUint32,
|
Command_id: math.MaxUint32,
|
||||||
command_status: math.MaxUint32,
|
Command_status: math.MaxUint32,
|
||||||
sequence_number: math.MaxUint32,
|
Sequence_number: math.MaxUint32,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -239,26 +239,26 @@ func TestEncodeIntoWithMaximumValues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
innerbuf := buf.Bytes()
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
|
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]))
|
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
|
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]))
|
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
|
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]))
|
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
|
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]))
|
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 0,
|
Command_length: 0,
|
||||||
command_id: 0,
|
Command_id: 0,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 0,
|
Sequence_number: 0,
|
||||||
}
|
}
|
||||||
buf := bytes.NewBuffer(make([]byte, 16))
|
buf := bytes.NewBuffer(make([]byte, 16))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
@@ -269,17 +269,17 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerbuf := buf.Bytes()
|
innerbuf := buf.Bytes()
|
||||||
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
|
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]))
|
t.Errorf("Expected command_length %d, got %d", p.Command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
|
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]))
|
t.Errorf("Expected command_id %d, got %d", p.Command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
|
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]))
|
t.Errorf("Expected command_status %d, got %d", p.Command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
|
||||||
}
|
}
|
||||||
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
|
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]))
|
t.Errorf("Expected sequence_number %d, got %d", p.Sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,17 +307,17 @@ func TestDecodeParsesValidByteSlice(t *testing.T) {
|
|||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.command_length != 16 {
|
if p.Command_length != 16 {
|
||||||
t.Errorf("Expected command_length to be 16, got %d", p.command_length)
|
t.Errorf("Expected command_length to be 16, got %d", p.Command_length)
|
||||||
}
|
}
|
||||||
if p.command_id != 1 {
|
if p.Command_id != 1 {
|
||||||
t.Errorf("Expected command_id to be 1, got %d", p.command_id)
|
t.Errorf("Expected command_id to be 1, got %d", p.Command_id)
|
||||||
}
|
}
|
||||||
if p.command_status != 2 {
|
if p.Command_status != 2 {
|
||||||
t.Errorf("Expected command_status to be 2, got %d", p.command_status)
|
t.Errorf("Expected command_status to be 2, got %d", p.Command_status)
|
||||||
}
|
}
|
||||||
if p.sequence_number != 3 {
|
if p.Sequence_number != 3 {
|
||||||
t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number)
|
t.Errorf("Expected sequence_number to be 3, got %d", p.Sequence_number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,17 +343,17 @@ func TestDecodeHandlesNilDataInput(t *testing.T) {
|
|||||||
t.Errorf("Expected error, got none")
|
t.Errorf("Expected error, got none")
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.command_length != 0 {
|
if p.Command_length != 0 {
|
||||||
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
||||||
}
|
}
|
||||||
if p.command_id != 0 {
|
if p.Command_id != 0 {
|
||||||
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
||||||
}
|
}
|
||||||
if p.command_status != 0 {
|
if p.Command_status != 0 {
|
||||||
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
|
t.Errorf("Expected command_status to be 0, got %d", p.Command_status)
|
||||||
}
|
}
|
||||||
if p.sequence_number != 0 {
|
if p.Sequence_number != 0 {
|
||||||
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
|
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,17 +366,17 @@ func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
|
|||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.command_length != 0 {
|
if p.Command_length != 0 {
|
||||||
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
||||||
}
|
}
|
||||||
if p.command_id != 0 {
|
if p.Command_id != 0 {
|
||||||
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
||||||
}
|
}
|
||||||
if p.command_status != 0 {
|
if p.Command_status != 0 {
|
||||||
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
|
t.Errorf("Expected command_status to be 0, got %d", p.Command_status)
|
||||||
}
|
}
|
||||||
if p.sequence_number != 0 {
|
if p.Sequence_number != 0 {
|
||||||
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
|
t.Errorf("Expected sequence_number to be 0, got %d", p.Sequence_number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,11 +408,11 @@ func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
|
|||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.command_length != math.MaxUint32 {
|
if p.Command_length != math.MaxUint32 {
|
||||||
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
|
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.Command_length)
|
||||||
}
|
}
|
||||||
if p.command_id != math.MaxUint32 {
|
if p.Command_id != math.MaxUint32 {
|
||||||
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id)
|
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.Command_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -425,11 +425,11 @@ func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
|
|||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.command_length != 0 {
|
if p.Command_length != 0 {
|
||||||
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
|
t.Errorf("Expected command_length to be 0, got %d", p.Command_length)
|
||||||
}
|
}
|
||||||
if p.command_id != 0 {
|
if p.Command_id != 0 {
|
||||||
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
|
t.Errorf("Expected command_id to be 0, got %d", p.Command_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,10 +445,10 @@ func TestSizeReturns16(t *testing.T) {
|
|||||||
// With buffer pool
|
// With buffer pool
|
||||||
func BenchmarkEncodeWithBufferPool(b *testing.B) {
|
func BenchmarkEncodeWithBufferPool(b *testing.B) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 16,
|
Command_length: 16,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 12345,
|
Sequence_number: 12345,
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
@@ -461,10 +461,10 @@ func BenchmarkEncodeWithBufferPool(b *testing.B) {
|
|||||||
// Without buffer pool
|
// Without buffer pool
|
||||||
func BenchmarkEncodeWithoutBufferPool(b *testing.B) {
|
func BenchmarkEncodeWithoutBufferPool(b *testing.B) {
|
||||||
p := &PDU_HEADER{
|
p := &PDU_HEADER{
|
||||||
command_length: 16,
|
Command_length: 16,
|
||||||
command_id: 1,
|
Command_id: 1,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 12345,
|
Sequence_number: 12345,
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
101
pdu/submit.go
101
pdu/submit.go
@@ -10,25 +10,25 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
SUBMIT_SM struct {
|
SUBMIT_SM struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
service_type string
|
Service_type string
|
||||||
source_addr_ton byte
|
Source_addr_ton byte
|
||||||
source_addr_npi byte
|
Source_addr_npi byte
|
||||||
source_addr string
|
Source_addr string
|
||||||
dest_addr_ton byte
|
Dest_addr_ton byte
|
||||||
dest_addr_npi byte
|
Dest_addr_npi byte
|
||||||
destination_addr string
|
Destination_addr string
|
||||||
esm_class byte
|
Esm_class byte
|
||||||
protocol_id byte
|
Protocol_id byte
|
||||||
priority_flag byte
|
Priority_flag byte
|
||||||
schedule_delivery_time string
|
Schedule_delivery_time string
|
||||||
validity_period string
|
Validity_period string
|
||||||
registered_delivery byte
|
Registered_delivery byte
|
||||||
replace_if_present byte
|
Replace_if_present byte
|
||||||
data_coding byte
|
Data_coding byte
|
||||||
sm_default_msg_id byte
|
Sm_default_msg_id byte
|
||||||
sm_length byte
|
Sm_length byte
|
||||||
short_message string
|
Short_message string
|
||||||
// user_message_reference uint16
|
// user_message_reference uint16
|
||||||
// source_port uint16
|
// source_port uint16
|
||||||
// source_addr_subunit byte
|
// source_addr_subunit byte
|
||||||
@@ -59,12 +59,13 @@ type (
|
|||||||
// ussd_service_op byte
|
// ussd_service_op byte
|
||||||
}
|
}
|
||||||
SUBMIT_SM_RESP struct {
|
SUBMIT_SM_RESP struct {
|
||||||
header *PDU_HEADER
|
Header *PDU_HEADER
|
||||||
message_id string
|
Message_id string
|
||||||
}
|
}
|
||||||
SUBMIT_MULTI struct{}
|
SUBMIT_MULTI struct{}
|
||||||
SUBMIT_MULTI_RESP struct{}
|
SUBMIT_MULTI_RESP struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
|
// 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
|
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
|
||||||
|
|
||||||
@@ -74,31 +75,31 @@ func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
|||||||
}
|
}
|
||||||
messageEncoder := p.GetEncoder()
|
messageEncoder := p.GetEncoder()
|
||||||
|
|
||||||
p.header.Encode(buf)
|
p.Header.Encode(buf)
|
||||||
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
||||||
buf.WriteString(p.service_type)
|
buf.WriteString(p.Service_type)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.source_addr_ton)
|
binary.Write(buf, binary.BigEndian, p.Source_addr_ton)
|
||||||
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
|
binary.Write(buf, binary.BigEndian, p.Source_addr_npi)
|
||||||
buf.WriteString(p.source_addr)
|
buf.WriteString(p.Source_addr)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
|
binary.Write(buf, binary.BigEndian, p.Dest_addr_ton)
|
||||||
binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
|
binary.Write(buf, binary.BigEndian, p.Dest_addr_npi)
|
||||||
buf.WriteString(p.destination_addr)
|
buf.WriteString(p.Destination_addr)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.esm_class)
|
binary.Write(buf, binary.BigEndian, p.Esm_class)
|
||||||
binary.Write(buf, binary.BigEndian, p.protocol_id)
|
binary.Write(buf, binary.BigEndian, p.Protocol_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.priority_flag)
|
binary.Write(buf, binary.BigEndian, p.Priority_flag)
|
||||||
buf.WriteString(p.schedule_delivery_time)
|
buf.WriteString(p.Schedule_delivery_time)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
buf.WriteString(p.validity_period)
|
buf.WriteString(p.Validity_period)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.registered_delivery)
|
binary.Write(buf, binary.BigEndian, p.Registered_delivery)
|
||||||
binary.Write(buf, binary.BigEndian, p.replace_if_present)
|
binary.Write(buf, binary.BigEndian, p.Replace_if_present)
|
||||||
binary.Write(buf, binary.BigEndian, p.data_coding)
|
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_default_msg_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.sm_length)
|
binary.Write(buf, binary.BigEndian, p.Sm_length)
|
||||||
err := messageEncoder.Encode(&p.short_message, buf)
|
err := messageEncoder.Encode(&p.Short_message, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -112,33 +113,33 @@ func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
|||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) Size() int {
|
func (p *SUBMIT_SM) Size() int {
|
||||||
var size int
|
var size int
|
||||||
size += p.header.Size()
|
size += p.Header.Size()
|
||||||
size += 1 + len(p.service_type)
|
size += 1 + len(p.Service_type)
|
||||||
size += 1 // source_addr_ton
|
size += 1 // source_addr_ton
|
||||||
size += 1 // source_addr_npi
|
size += 1 // source_addr_npi
|
||||||
size += 1 + len(p.source_addr)
|
size += 1 + len(p.Source_addr)
|
||||||
size += 1 // dest_addr_ton
|
size += 1 // dest_addr_ton
|
||||||
size += 1 // dest_addr_npi
|
size += 1 // dest_addr_npi
|
||||||
size += 1 + len(p.destination_addr)
|
size += 1 + len(p.Destination_addr)
|
||||||
size += 1 // esm_class
|
size += 1 // esm_class
|
||||||
size += 1 // protocol_id
|
size += 1 // protocol_id
|
||||||
size += 1 // priority_flag
|
size += 1 // priority_flag
|
||||||
size += 1 + len(p.schedule_delivery_time)
|
size += 1 + len(p.Schedule_delivery_time)
|
||||||
size += 1 + len(p.validity_period)
|
size += 1 + len(p.Validity_period)
|
||||||
size += 1 // registered_delivery
|
size += 1 // registered_delivery
|
||||||
size += 1 // replace_if_present
|
size += 1 // replace_if_present
|
||||||
size += 1 // data_coding
|
size += 1 // data_coding
|
||||||
size += 1 // sm_default_msg_id
|
size += 1 // sm_default_msg_id
|
||||||
size += 1 // sm_length
|
size += 1 // sm_length
|
||||||
size += p.GetEncoder().EncodesInto(&p.short_message)
|
size += p.GetEncoder().EncodesInto(&p.Short_message)
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) UpdateSize() {
|
func (p *SUBMIT_SM) UpdateSize() {
|
||||||
p.header.command_length = uint32(p.Size())
|
p.Header.Command_length = uint32(p.Size())
|
||||||
p.sm_length = byte(p.GetEncoder().EncodesInto(&p.short_message))
|
p.Sm_length = byte(p.GetEncoder().EncodesInto(&p.Short_message))
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
|
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
|
||||||
switch p.data_coding {
|
switch p.Data_coding {
|
||||||
case 0b00000000: // GSM7
|
case 0b00000000: // GSM7
|
||||||
return &encoding.GSM7Coder{}
|
return &encoding.GSM7Coder{}
|
||||||
case 0b00000001: // ASCII
|
case 0b00000001: // ASCII
|
||||||
|
@@ -9,28 +9,28 @@ import (
|
|||||||
// See examples: https://www.openmarket.com/docs/Content/apis/v4smpp/mt-examples.htm
|
// See examples: https://www.openmarket.com/docs/Content/apis/v4smpp/mt-examples.htm
|
||||||
func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
|
func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
header: &PDU_HEADER{
|
Header: &PDU_HEADER{
|
||||||
command_length: 0,
|
Command_length: 0,
|
||||||
command_id: 4,
|
Command_id: 4,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 378019,
|
Sequence_number: 378019,
|
||||||
},
|
},
|
||||||
service_type: "OMV4",
|
Service_type: "OMV4",
|
||||||
source_addr_ton: 3,
|
Source_addr_ton: 3,
|
||||||
source_addr_npi: 1,
|
Source_addr_npi: 1,
|
||||||
source_addr: "80362",
|
Source_addr: "80362",
|
||||||
dest_addr_ton: 1,
|
Dest_addr_ton: 1,
|
||||||
dest_addr_npi: 1,
|
Dest_addr_npi: 1,
|
||||||
destination_addr: "812345001000",
|
Destination_addr: "812345001000",
|
||||||
esm_class: 0,
|
Esm_class: 0,
|
||||||
protocol_id: 0,
|
Protocol_id: 0,
|
||||||
priority_flag: 0,
|
Priority_flag: 0,
|
||||||
schedule_delivery_time: "",
|
Schedule_delivery_time: "",
|
||||||
validity_period: "180105120000004+",
|
Validity_period: "180105120000004+",
|
||||||
registered_delivery: 1,
|
Registered_delivery: 1,
|
||||||
data_coding: 1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
|
Data_coding: 1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
|
||||||
sm_default_msg_id: 0,
|
Sm_default_msg_id: 0,
|
||||||
short_message: "Reply Yes to opt in or No to opt out.",
|
Short_message: "Reply Yes to opt in or No to opt out.",
|
||||||
}
|
}
|
||||||
p.UpdateSize()
|
p.UpdateSize()
|
||||||
buf := ByteBufferPool.Get(p.Size())
|
buf := ByteBufferPool.Get(p.Size())
|
||||||
@@ -47,28 +47,28 @@ func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
|
|||||||
|
|
||||||
func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
|
func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
header: &PDU_HEADER{
|
Header: &PDU_HEADER{
|
||||||
command_length: 0,
|
Command_length: 0,
|
||||||
command_id: 4,
|
Command_id: 4,
|
||||||
command_status: 0,
|
Command_status: 0,
|
||||||
sequence_number: 378019,
|
Sequence_number: 378019,
|
||||||
},
|
},
|
||||||
service_type: "OMV4",
|
Service_type: "OMV4",
|
||||||
source_addr_ton: 3,
|
Source_addr_ton: 3,
|
||||||
source_addr_npi: 1,
|
Source_addr_npi: 1,
|
||||||
source_addr: "80362",
|
Source_addr: "80362",
|
||||||
dest_addr_ton: 1,
|
Dest_addr_ton: 1,
|
||||||
dest_addr_npi: 1,
|
Dest_addr_npi: 1,
|
||||||
destination_addr: "812345001000",
|
Destination_addr: "812345001000",
|
||||||
esm_class: 0,
|
Esm_class: 0,
|
||||||
protocol_id: 0,
|
Protocol_id: 0,
|
||||||
priority_flag: 0,
|
Priority_flag: 0,
|
||||||
schedule_delivery_time: "",
|
Schedule_delivery_time: "",
|
||||||
validity_period: "180105120000004+",
|
Validity_period: "180105120000004+",
|
||||||
registered_delivery: 1,
|
Registered_delivery: 1,
|
||||||
data_coding: 0, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
|
Data_coding: 0, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
|
||||||
sm_default_msg_id: 0,
|
Sm_default_msg_id: 0,
|
||||||
short_message: "Reply Yes to opt in or No to opt out.",
|
Short_message: "Reply Yes to opt in or No to opt out.",
|
||||||
}
|
}
|
||||||
p.UpdateSize()
|
p.UpdateSize()
|
||||||
buf := ByteBufferPool.Get(p.Size())
|
buf := ByteBufferPool.Get(p.Size())
|
||||||
@@ -88,13 +88,13 @@ func TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(t *testing.T) {
|
|||||||
// region size
|
// region size
|
||||||
func TestCalculateSizeTypicalInstance(t *testing.T) {
|
func TestCalculateSizeTypicalInstance(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
service_type: "test_service",
|
Service_type: "test_service",
|
||||||
source_addr: "12345",
|
Source_addr: "12345",
|
||||||
destination_addr: "67890",
|
Destination_addr: "67890",
|
||||||
schedule_delivery_time: "",
|
Schedule_delivery_time: "",
|
||||||
validity_period: "",
|
Validity_period: "",
|
||||||
short_message: "Hello, World!",
|
Short_message: "Hello, World!",
|
||||||
data_coding: 1,
|
Data_coding: 1,
|
||||||
}
|
}
|
||||||
expectedSize := 68
|
expectedSize := 68
|
||||||
actualSize := p.Size()
|
actualSize := p.Size()
|
||||||
@@ -105,13 +105,13 @@ func TestCalculateSizeTypicalInstance(t *testing.T) {
|
|||||||
|
|
||||||
func TestCalculateSizeTypicalGSM7Instance(t *testing.T) {
|
func TestCalculateSizeTypicalGSM7Instance(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
service_type: "test_service",
|
Service_type: "test_service",
|
||||||
source_addr: "12345",
|
Source_addr: "12345",
|
||||||
destination_addr: "67890",
|
Destination_addr: "67890",
|
||||||
schedule_delivery_time: "",
|
Schedule_delivery_time: "",
|
||||||
validity_period: "",
|
Validity_period: "",
|
||||||
short_message: "Hello, World!",
|
Short_message: "Hello, World!",
|
||||||
data_coding: 0,
|
Data_coding: 0,
|
||||||
}
|
}
|
||||||
expectedSize := 67
|
expectedSize := 67
|
||||||
actualSize := p.Size()
|
actualSize := p.Size()
|
||||||
@@ -123,13 +123,13 @@ func TestCalculateSizeTypicalGSM7Instance(t *testing.T) {
|
|||||||
func TestCalculateSizeMaxLengths(t *testing.T) {
|
func TestCalculateSizeMaxLengths(t *testing.T) {
|
||||||
maxLen := 255
|
maxLen := 255
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
service_type: string(make([]byte, maxLen)),
|
Service_type: string(make([]byte, maxLen)),
|
||||||
source_addr: string(make([]byte, maxLen)),
|
Source_addr: string(make([]byte, maxLen)),
|
||||||
destination_addr: string(make([]byte, maxLen)),
|
Destination_addr: string(make([]byte, maxLen)),
|
||||||
schedule_delivery_time: string(make([]byte, maxLen)),
|
Schedule_delivery_time: string(make([]byte, maxLen)),
|
||||||
validity_period: string(make([]byte, maxLen)),
|
Validity_period: string(make([]byte, maxLen)),
|
||||||
short_message: string(make([]byte, maxLen)),
|
Short_message: string(make([]byte, maxLen)),
|
||||||
data_coding: 1,
|
Data_coding: 1,
|
||||||
}
|
}
|
||||||
expectedSize := 1563
|
expectedSize := 1563
|
||||||
actualSize := p.Size()
|
actualSize := p.Size()
|
||||||
@@ -140,12 +140,12 @@ func TestCalculateSizeMaxLengths(t *testing.T) {
|
|||||||
|
|
||||||
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
|
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
|
||||||
p := &SUBMIT_SM{
|
p := &SUBMIT_SM{
|
||||||
service_type: "",
|
Service_type: "",
|
||||||
source_addr: "",
|
Source_addr: "",
|
||||||
destination_addr: "",
|
Destination_addr: "",
|
||||||
schedule_delivery_time: "",
|
Schedule_delivery_time: "",
|
||||||
validity_period: "",
|
Validity_period: "",
|
||||||
short_message: "",
|
Short_message: "",
|
||||||
}
|
}
|
||||||
expectedSize := 33
|
expectedSize := 33
|
||||||
actualSize := p.Size()
|
actualSize := p.Size()
|
||||||
|
@@ -1 +1,66 @@
|
|||||||
package server
|
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