means (#3)
Co-authored-by: Evan Burkey <dev@fputs.com> Reviewed-on: https://git.fputs.com/fputs/protohackers/pulls/3
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user