reset on error in echo

This commit is contained in:
Steven Allen 2017-09-13 15:56:55 -07:00
parent d2dd60a285
commit 6b6687f9e4
1 changed files with 9 additions and 9 deletions

View File

@ -134,8 +134,12 @@ func main() {
// a user-defined protocol name.
ha.SetStreamHandler("/echo/1.0.0", func(s net.Stream) {
log.Println("Got a new stream!")
defer s.Close()
doEcho(s)
if err := doEcho(s); err != nil {
log.Println(err)
s.Reset()
} else {
s.Close()
}
})
if *target == "" {
@ -194,18 +198,14 @@ func main() {
}
// doEcho reads a line of data a stream and writes it back
func doEcho(s net.Stream) {
func doEcho(s net.Stream) error {
buf := bufio.NewReader(s)
str, err := buf.ReadString('\n')
if err != nil {
log.Println(err)
return
return err
}
log.Printf("read: %s\n", str)
_, err = s.Write([]byte(str))
if err != nil {
log.Println(err)
return
}
return err
}