mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
Refactor protocol scanner
This commit is contained in:
+1
-18
@@ -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
@@ -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
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user