Tcp communication should use same protocol as file based

This commit is contained in:
Leonid Bugaev
2015-08-18 16:02:03 +03:00
parent 7790b33cea
commit 8da4e54cbb
4 changed files with 11 additions and 29 deletions
+2 -10
View File
@@ -2,7 +2,6 @@ package main
import (
"bufio"
"encoding/hex"
"fmt"
"log"
"net"
@@ -10,7 +9,6 @@ import (
)
// TCPInput used for internal communication
// It expected hex encoded data
type TCPInput struct {
data chan []byte
address string
@@ -62,16 +60,10 @@ func (i *TCPInput) handleConnection(conn net.Conn) {
reader := bufio.NewReader(conn)
scanner := bufio.NewScanner(reader)
scanner.Split(payloadScanner)
for scanner.Scan() {
encodedPayload := scanner.Bytes()
// Hex encoding always 2x number of bytes
decoded := make([]byte, len(encodedPayload)/2)
_, err := hex.Decode(decoded, encodedPayload)
if err != nil {
log.Println("[TCPInput] failed to hex decode TCP payload:", err)
}
i.data <- decoded
i.data <- scanner.Bytes()
}
if err := scanner.Err(); err != nil {
+3 -6
View File
@@ -1,7 +1,6 @@
package main
import (
"encoding/hex"
"io"
"log"
"net"
@@ -35,14 +34,12 @@ func TestTCPInput(t *testing.T) {
log.Fatal(err)
}
msg := []byte("GET / HTTP/1.1\r\n\r\n")
msg := []byte("1 1 1\nGET / HTTP/1.1\r\n\r\n")
for i := 0; i < 100; i++ {
wg.Add(1)
encoded := make([]byte, len(msg)*2)
hex.Encode(encoded, msg)
conn.Write(append(encoded, '\n'))
conn.Write(msg)
conn.Write([]byte(payloadSeparator))
}
wg.Wait()
+4 -7
View File
@@ -1,7 +1,6 @@
package main
import (
"encoding/hex"
"fmt"
"io"
"log"
@@ -47,7 +46,9 @@ func (o *TCPOutput) worker() {
defer conn.Close()
for {
_, err := conn.Write(<-o.buf)
conn.Write(<-o.buf)
_, err := conn.Write([]byte(payloadSeparator))
if err != nil {
log.Println("Worker failed on write, exitings and starting new worker")
go o.worker()
@@ -61,11 +62,7 @@ func (o *TCPOutput) Write(data []byte) (n int, err error) {
return len(data), nil
}
// Hex encoding always 2x number of bytes
encoded := make([]byte, len(data)*2+1)
hex.Encode(encoded, data)
encoded[len(encoded)-1] = '\n'
o.buf <- encoded
o.buf <- data
if Settings.outputTCPStats {
o.bufStats.Write(len(o.buf))
+2 -6
View File
@@ -2,7 +2,6 @@ package main
import (
"bufio"
"encoding/hex"
"io"
"log"
"net"
@@ -50,13 +49,10 @@ func startTCP(cb func([]byte)) net.Listener {
go func() {
reader := bufio.NewReader(conn)
scanner := bufio.NewScanner(reader)
scanner.Split(payloadScanner)
for scanner.Scan() {
encodedPayload := scanner.Bytes()
// Hex encoding always 2x number of bytes
decoded := make([]byte, len(encodedPayload)/2)
hex.Decode(decoded, encodedPayload)
cb(decoded)
cb(scanner.Bytes())
}
}()
}