Co-authored-by: Evan Burkey <dev@fputs.com>
Reviewed-on: https://git.fputs.com/fputs/protohackers/pulls/3
This commit is contained in:
Evan Burkey
2022-11-08 00:02:36 +01:00
parent bc17b6befc
commit 3bd3a895b5
3 changed files with 78 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package conn
import (
"bufio"
"fmt"
"net"
)
@ -30,3 +31,19 @@ func StartSimple(handler func(conn net.Conn)) error {
}
}
}
func ReadAndRespond(c net.Conn, process func([]byte) []byte) error {
defer c.Close()
sc := bufio.NewScanner(c)
for sc.Scan() {
response := process(sc.Bytes())
_, err := c.Write(response)
if err != nil {
return err
}
}
if err := sc.Err(); err != nil {
return err
}
return nil
}