More verbose disconnect messages

This commit is contained in:
cathugger 2018-07-19 01:03:24 +00:00
parent 059fe24526
commit f4bb2aaaeb

View File

@ -17,6 +17,7 @@ package yggdrasil
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"math/rand" "math/rand"
"net" "net"
"sync" "sync"
@ -304,15 +305,19 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
themAddrString := net.IP(themAddr[:]).String() themAddrString := net.IP(themAddr[:]).String()
themString := fmt.Sprintf("%s@%s", themAddrString, them) themString := fmt.Sprintf("%s@%s", themAddrString, them)
iface.core.log.Println("Connected:", themString) iface.core.log.Println("Connected:", themString)
iface.reader(sock, in) // In this goroutine, because of defers err = iface.reader(sock, in) // In this goroutine, because of defers
iface.core.log.Println("Disconnected:", themString) if err == nil {
iface.core.log.Println("Disconnected:", themString)
} else {
iface.core.log.Println("Disconnected:", themString, "with error:", err)
}
return return
} }
// This reads from the socket into a []byte buffer for incomping messages. // This reads from the socket into a []byte buffer for incomping messages.
// It copies completed messages out of the cache into a new slice, and passes them to the peer struct via the provided `in func([]byte)` argument. // It copies completed messages out of the cache into a new slice, and passes them to the peer struct via the provided `in func([]byte)` argument.
// Then it shifts the incomplete fragments of data forward so future reads won't overwrite it. // Then it shifts the incomplete fragments of data forward so future reads won't overwrite it.
func (iface *tcpInterface) reader(sock net.Conn, in func([]byte)) { func (iface *tcpInterface) reader(sock net.Conn, in func([]byte)) error {
bs := make([]byte, 2*tcp_msgSize) bs := make([]byte, 2*tcp_msgSize)
frag := bs[:0] frag := bs[:0]
for { for {
@ -320,13 +325,16 @@ func (iface *tcpInterface) reader(sock net.Conn, in func([]byte)) {
sock.SetReadDeadline(timeout) sock.SetReadDeadline(timeout)
n, err := sock.Read(bs[len(frag):]) n, err := sock.Read(bs[len(frag):])
if err != nil || n == 0 { if err != nil || n == 0 {
break if err != io.EOF {
return err
}
return nil
} }
frag = bs[:len(frag)+n] frag = bs[:len(frag)+n]
for { for {
msg, ok, err := tcp_chop_msg(&frag) msg, ok, err := tcp_chop_msg(&frag)
if err != nil { if err != nil {
return return fmt.Errorf("Message error: %v", err)
} }
if !ok { if !ok {
break break