Refactor protocol scanner

This commit is contained in:
Leonid Bugaev
2015-08-18 15:52:23 +03:00
parent a971d8a843
commit 7790b33cea
3 changed files with 20 additions and 21 deletions
+1 -18
View File
@@ -4,7 +4,6 @@ import (
"log"
"os"
"bufio"
"bytes"
"strconv"
"time"
)
@@ -51,28 +50,12 @@ func (i *FileInput) String() string {
return "File input: " + i.path
}
func scanSeparator(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte(fileSeparator)); i >= 0 {
// We have a full newline-terminated line.
return i + len(fileSeparator), data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
func (i *FileInput) emit() {
var lastTime int64
// reader := bufio.NewReader(conn)
scanner := bufio.NewScanner(i.file)
scanner.Split(scanSeparator)
scanner.Split(payloadScanner)
for scanner.Scan() {
buf := scanner.Bytes()
+1 -3
View File
@@ -31,15 +31,13 @@ func (o *FileOutput) init(path string) {
}
}
var fileSeparator = "\n🐵🙈🙉\n"
func (o *FileOutput) Write(data []byte) (n int, err error) {
if !isOriginPayload(data) {
return len(data), nil
}
o.file.Write(data)
o.file.Write([]byte(fileSeparator))
o.file.Write([]byte(payloadSeparator))
return len(data), nil
}
+18
View File
@@ -23,6 +23,24 @@ func uuid() []byte {
return uuid
}
var payloadSeparator = "\n🐵🙈🙉\n"
func payloadScanner(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte(payloadSeparator)); i >= 0 {
// We have a full newline-terminated line.
return i + len([]byte(payloadSeparator)), data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
// Timing is request start or round-trip time, depending on payloadType
func payloadHeader(payloadType int, uuid []byte, timing int64) (header []byte) {