]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction.go
Merge pull request #55 from jhalter/add_client_error_messaging
[rbdr/mobius] / hotline / transaction.go
index ece2924da5969f6441ba1bb75188f7c6504d6f9f..9b0ac407f81e9c694f2d710f10416392b2eccc6b 100644 (file)
@@ -1,6 +1,7 @@
 package hotline
 
 import (
 package hotline
 
 import (
+       "bytes"
        "encoding/binary"
        "errors"
        "fmt"
        "encoding/binary"
        "errors"
        "fmt"
@@ -131,23 +132,24 @@ func ReadTransaction(buf []byte) (*Transaction, int, error) {
        }, tranLen, nil
 }
 
        }, 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
 }
 
 const minFieldLen = 4
@@ -237,3 +239,7 @@ func (t *Transaction) GetField(id int) Field {
 
        return Field{}
 }
 
        return Field{}
 }
+
+func (t *Transaction) IsError() bool {
+       return bytes.Compare(t.ErrorCode, []byte{0, 0, 0, 1}) == 0
+}