aboutsummaryrefslogtreecommitdiff
path: root/hotline/transaction.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-17 20:30:49 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-17 20:30:49 -0700
commit153e2eac3b51a6a426556752fa2c532cfe53f026 (patch)
treedf83911537b02f61396ecbca5c4796fd0c3db958 /hotline/transaction.go
parentf8e4cd540b87de3e308ec18a2b040b284a741522 (diff)
Use fixed size array types in Transaction fields
Diffstat (limited to 'hotline/transaction.go')
-rw-r--r--hotline/transaction.go47
1 files changed, 22 insertions, 25 deletions
diff --git a/hotline/transaction.go b/hotline/transaction.go
index 39dcd81..0caf72f 100644
--- a/hotline/transaction.go
+++ b/hotline/transaction.go
@@ -73,14 +73,14 @@ const (
)
type Transaction struct {
- Flags byte // Reserved (should be 0)
- IsReply byte // Request (0) or reply (1)
- Type []byte // Requested operation (user defined)
- ID []byte // Unique transaction ID (must be != 0)
- ErrorCode []byte // Used in the reply (user defined, 0 = no error)
- TotalSize []byte // Total data size for the transaction (all parts)
- DataSize []byte // Size of data in this transaction part. This allows splitting large transactions into smaller parts.
- ParamCount []byte // Number of the parameters for this transaction
+ Flags byte // Reserved (should be 0)
+ IsReply byte // Request (0) or reply (1)
+ Type [2]byte // Requested operation (user defined)
+ ID [4]byte // Unique transaction ID (must be != 0)
+ ErrorCode [4]byte // Used in the reply (user defined, 0 = no error)
+ TotalSize [4]byte // Total data size for the transaction (all parts)
+ DataSize [4]byte // Size of data in this transaction part. This allows splitting large transactions into smaller parts.
+ ParamCount [2]byte // Number of the parameters for this transaction
Fields []Field
clientID *[]byte // Internal identifier for target client
@@ -95,13 +95,10 @@ func NewTransaction(t int, clientID *[]byte, fields ...Field) *Transaction {
binary.BigEndian.PutUint32(idSlice, rand.Uint32())
return &Transaction{
- clientID: clientID,
- Flags: 0x00,
- IsReply: 0x00,
- Type: typeSlice,
- ID: idSlice,
- ErrorCode: []byte{0, 0, 0, 0},
- Fields: fields,
+ clientID: clientID,
+ Type: [2]byte(typeSlice),
+ ID: [4]byte(idSlice),
+ Fields: fields,
}
}
@@ -133,12 +130,12 @@ func (t *Transaction) Write(p []byte) (n int, err error) {
t.Flags = p[0]
t.IsReply = p[1]
- t.Type = p[2:4]
- t.ID = p[4:8]
- t.ErrorCode = p[8:12]
- t.TotalSize = p[12:16]
- t.DataSize = p[16:20]
- t.ParamCount = p[20:22]
+ t.Type = [2]byte(p[2:4])
+ t.ID = [4]byte(p[4:8])
+ t.ErrorCode = [4]byte(p[8:12])
+ t.TotalSize = [4]byte(p[12:16])
+ t.DataSize = [4]byte(p[16:20])
+ t.ParamCount = [2]byte(p[20:22])
return len(p), err
}
@@ -223,9 +220,9 @@ func (t *Transaction) Read(p []byte) (int, error) {
buf := slices.Concat(
[]byte{t.Flags, t.IsReply},
- t.Type,
- t.ID,
- t.ErrorCode,
+ t.Type[:],
+ t.ID[:],
+ t.ErrorCode[:],
payloadSize,
payloadSize, // this is the dataSize field, but seeming the same as totalSize
fieldCount,
@@ -267,5 +264,5 @@ func (t *Transaction) GetField(id int) Field {
}
func (t *Transaction) IsError() bool {
- return bytes.Equal(t.ErrorCode, []byte{0, 0, 0, 1})
+ return t.ErrorCode == [4]byte{0, 0, 0, 1}
}