5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-23 00:51:35 +00:00

Merge pull request #162 from cathugger/master

More verbose disconnect messages
This commit is contained in:
Arceliar 2018-07-18 22:55:45 -05:00 committed by GitHub
commit 1baafdd17d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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
if err == nil {
iface.core.log.Println("Disconnected:", themString) 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