package main import ( "bufio" "bytes" "fmt" "io" "log" "net" "os" ) // TCPInput used for internal communication type TCPInput struct { data chan []byte address string listener net.Listener } // NewTCPInput constructor for TCPInput, accepts address with port func NewTCPInput(address string) (i *TCPInput) { i = new(TCPInput) i.data = make(chan []byte, 1000) i.address = address i.listen(address) return } func (i *TCPInput) Read(data []byte) (int, error) { buf := <-i.data copy(data, buf) return len(buf), nil } func (i *TCPInput) listen(address string) { listener, err := net.Listen("tcp", address) i.listener = listener if err != nil { log.Fatal("Can't start:", err) } go func() { for { conn, err := listener.Accept() if err != nil { log.Println("Error while Accept()", err) continue } go i.handleConnection(conn) } }() } func (i *TCPInput) handleConnection(conn net.Conn) { defer conn.Close() payloadSeparatorAsBytes := []byte(payloadSeparator) reader := bufio.NewReader(conn) var buffer bytes.Buffer for { line, err := reader.ReadBytes('\n') if err != nil { if err != io.EOF { fmt.Fprintln(os.Stderr, "Unexpected error in input tcp connection:", err) } break } if bytes.Equal(payloadSeparatorAsBytes[1:], line) { asBytes := buffer.Bytes() buffer.Reset() newBuf := make([]byte, len(asBytes)-1) copy(newBuf, asBytes) i.data <- newBuf } else { buffer.Write(line) } } } func (i *TCPInput) String() string { return "TCP input: " + i.address }