mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-22 21:10:29 +00:00
rearrange tcp reading loop
according to documentation of io.Reader interface, "Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors."
This commit is contained in:
parent
f4bb2aaaeb
commit
91a374d698
@ -324,26 +324,29 @@ func (iface *tcpInterface) reader(sock net.Conn, in func([]byte)) error {
|
|||||||
timeout := time.Now().Add(tcp_timeout)
|
timeout := time.Now().Add(tcp_timeout)
|
||||||
sock.SetReadDeadline(timeout)
|
sock.SetReadDeadline(timeout)
|
||||||
n, err := sock.Read(bs[len(frag):])
|
n, err := sock.Read(bs[len(frag):])
|
||||||
|
if n > 0 {
|
||||||
|
frag = bs[:len(frag)+n]
|
||||||
|
for {
|
||||||
|
msg, ok, err2 := tcp_chop_msg(&frag)
|
||||||
|
if err2 != nil {
|
||||||
|
return fmt.Errorf("Message error: %v", err2)
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
// We didn't get the whole message yet
|
||||||
|
break
|
||||||
|
}
|
||||||
|
newMsg := append(util_getBytes(), msg...)
|
||||||
|
in(newMsg)
|
||||||
|
util_yield()
|
||||||
|
}
|
||||||
|
frag = append(bs[:0], frag...)
|
||||||
|
}
|
||||||
if err != nil || n == 0 {
|
if err != nil || n == 0 {
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
frag = bs[:len(frag)+n]
|
|
||||||
for {
|
|
||||||
msg, ok, err := tcp_chop_msg(&frag)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Message error: %v", err)
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
} // We didn't get the whole message yet
|
|
||||||
newMsg := append(util_getBytes(), msg...)
|
|
||||||
in(newMsg)
|
|
||||||
util_yield()
|
|
||||||
}
|
|
||||||
frag = append(bs[:0], frag...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user