"fmt"
"github.com/jhalter/mobius/concat"
"math/rand"
- "net"
)
const (
tranNotifyChatSubject = 119
tranSetChatSubject = 120
tranAgreed = 121
+ tranServerBanner = 122
tranGetFileNameList = 200
tranDownloadFile = 202
tranUploadFile = 203
tranGetFileInfo = 206
tranSetFileInfo = 207
tranMoveFile = 208
- tranMakeFileAlias = 209 // TODO: implement file alias command
+ tranMakeFileAlias = 209
tranDownloadFldr = 210
// tranDownloadInfo = 211 TODO: implement file transfer queue
- // tranDownloadBanner = 212 TODO: figure out what this is used for
+ tranDownloadBanner = 212
tranUploadFldr = 213
tranGetUserNameList = 300
tranNotifyChangeUser = 301
}, tranLen, nil
}
-func readN(conn net.Conn, n int) ([]Transaction, error) {
- buf := make([]byte, 1400)
- i := 0
- for {
- readLen, err := conn.Read(buf)
- if err != nil {
- return nil, err
- }
-
- transactions, _, err := readTransactions(buf[:readLen])
- // spew.Fdump(os.Stderr, transactions)
- if err != nil {
- return nil, err
- }
+const tranHeaderLen = 20 // fixed length of transaction fields before the variable length fields
- i += len(transactions)
-
- if n == i {
- return transactions, nil
- }
+// 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
}
-}
-
-func readTransactions(buf []byte) ([]Transaction, int, error) {
- var transactions []Transaction
- bufLen := len(buf)
+ totalSize := binary.BigEndian.Uint32(data[12:16])
- var bytesRead = 0
- for bytesRead < bufLen {
- t, tReadLen, err := ReadTransaction(buf[bytesRead:])
- if err != nil {
- return transactions, bytesRead, err
- }
- bytesRead += tReadLen
-
- 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