diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2022-06-24 10:41:37 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2022-06-24 10:41:37 -0700 |
| commit | 3178ae580a3fe97d6a1167b4346d209f04e9b7e3 (patch) | |
| tree | da306047792334bbb97e07707d713d3fa582753a /hotline/transaction.go | |
| parent | a1ac9a6f60c6881bb9fe97425f6aaa524b910035 (diff) | |
Implement bufio.Scanner for transaction parsing
Diffstat (limited to 'hotline/transaction.go')
| -rw-r--r-- | hotline/transaction.go | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/hotline/transaction.go b/hotline/transaction.go index ece2924..88e17df 100644 --- a/hotline/transaction.go +++ b/hotline/transaction.go @@ -131,23 +131,24 @@ func ReadTransaction(buf []byte) (*Transaction, int, error) { }, tranLen, nil } -func readTransactions(buf []byte) ([]Transaction, int, error) { - var transactions []Transaction +const tranHeaderLen = 20 // fixed length of transaction fields before the variable length fields - bufLen := len(buf) +// transactionScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens +func transactionScanner(data []byte, _ bool) (advance int, token []byte, err error) { + // The bytes that contain the size of a transaction are from 12:16, so we need at least 16 bytes + if len(data) < 16 { + return 0, nil, nil + } - var bytesRead = 0 - for bytesRead < bufLen { - t, tReadLen, err := ReadTransaction(buf[bytesRead:]) - if err != nil { - return transactions, bytesRead, err - } - bytesRead += tReadLen + totalSize := binary.BigEndian.Uint32(data[12:16]) - transactions = append(transactions, *t) + // tranLen represents the length of bytes that are part of the transaction + tranLen := int(tranHeaderLen + totalSize) + if tranLen > len(data) { + return 0, nil, nil } - return transactions, bytesRead, nil + return tranLen, data[0:tranLen], nil } const minFieldLen = 4 |