reset on error in echo
This commit is contained in:
parent
d2dd60a285
commit
6b6687f9e4
|
@ -134,8 +134,12 @@ func main() {
|
||||||
// a user-defined protocol name.
|
// a user-defined protocol name.
|
||||||
ha.SetStreamHandler("/echo/1.0.0", func(s net.Stream) {
|
ha.SetStreamHandler("/echo/1.0.0", func(s net.Stream) {
|
||||||
log.Println("Got a new stream!")
|
log.Println("Got a new stream!")
|
||||||
defer s.Close()
|
if err := doEcho(s); err != nil {
|
||||||
doEcho(s)
|
log.Println(err)
|
||||||
|
s.Reset()
|
||||||
|
} else {
|
||||||
|
s.Close()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if *target == "" {
|
if *target == "" {
|
||||||
|
@ -194,18 +198,14 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// doEcho reads a line of data a stream and writes it back
|
// 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)
|
buf := bufio.NewReader(s)
|
||||||
str, err := buf.ReadString('\n')
|
str, err := buf.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("read: %s\n", str)
|
log.Printf("read: %s\n", str)
|
||||||
_, err = s.Write([]byte(str))
|
_, err = s.Write([]byte(str))
|
||||||
if err != nil {
|
return err
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue