This commit is contained in:
Arceliar 2018-02-23 13:04:52 -06:00
parent bb3ae8b39b
commit 0470f6f1c1
5 changed files with 1 additions and 80 deletions

View file

@ -74,8 +74,6 @@ func (iface *udpInterface) init(core *Core, addr string) {
if err != nil {
panic(err)
}
//iface.sock.SetReadBuffer(1048576)
//iface.sock.SetWriteBuffer(1048576)
iface.conns = make(map[connAddr]*connInfo)
go iface.reader()
}
@ -99,7 +97,6 @@ func (iface *udpInterface) startConn(info *connInfo) {
defer ticker.Stop()
defer func() {
// Cleanup
// FIXME this still leaks a peer struct
iface.mutex.Lock()
delete(iface.conns, info.addr)
iface.mutex.Unlock()
@ -178,51 +175,18 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
if ifce != nil && err == nil {
conn.chunkSize = uint16(ifce.MTU) - 60 - 8 - 3
}
//conn.chunkSize = 65535 - 8 - 3
}
/*
conn.in = func (msg []byte) { conn.peer.handlePacket(msg, conn.linkIn) }
conn.peer.out = func (msg []byte) {
start := time.Now()
iface.sock.WriteToUDP(msg, udpAddr)
timed := time.Since(start)
conn.peer.updateBandwidth(len(msg), timed)
util_putBytes(msg)
} // Old version, always one syscall per packet
//*/
/*
conn.peer.out = func (msg []byte) {
defer func() { recover() }()
select {
case conn.out<-msg:
default: util_putBytes(msg)
}
}
go func () {
for msg := range conn.out {
start := time.Now()
iface.sock.WriteToUDP(msg, udpAddr)
timed := time.Since(start)
conn.peer.updateBandwidth(len(msg), timed)
util_putBytes(msg)
}
}()
//*/
//*
var inChunks uint8
var inBuf []byte
conn.in = func(bs []byte) {
//defer util_putBytes(bs)
chunks, chunk, count, payload := udp_decode(bs)
//iface.core.log.Println("DEBUG:", addr, chunks, chunk, count, len(payload))
//iface.core.log.Println("DEBUG: payload:", payload)
if count != conn.countIn {
inChunks = 0
inBuf = inBuf[:0]
conn.countIn = count
}
if chunk <= chunks && chunk == inChunks+1 {
//iface.core.log.Println("GOING:", addr, chunks, chunk, count, len(payload))
inChunks += 1
inBuf = append(inBuf, payload...)
if chunks != chunk {
@ -230,7 +194,6 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
}
msg := append(util_getBytes(), inBuf...)
conn.peer.handlePacket(msg, conn.linkIn)
//iface.core.log.Println("DONE:", addr, chunks, chunk, count, len(payload))
}
}
conn.peer.out = func(msg []byte) {
@ -251,7 +214,6 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
chunks, bs = append(chunks, bs[:conn.chunkSize]), bs[conn.chunkSize:]
}
chunks = append(chunks, bs)
//iface.core.log.Println("DEBUG: out chunks:", len(chunks), len(msg))
if len(chunks) > 255 {
continue
}
@ -266,7 +228,6 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
conn.countOut += 1
conn.peer.updateBandwidth(len(msg), timed)
util_putBytes(msg)
//iface.core.log.Println("DEBUG: sent:", len(msg))
}
}()
//*/
@ -298,16 +259,12 @@ func (iface *udpInterface) handlePacket(msg []byte, addr connAddr) {
func (iface *udpInterface) reader() {
bs := make([]byte, 65536) // This needs to be large enough for everything...
for {
//iface.core.log.Println("Starting read")
n, udpAddr, err := iface.sock.ReadFromUDP(bs)
//iface.core.log.Println("Read", n, udpAddr.String(), err)
//iface.core.log.Println("DEBUG: read:", bs[0], bs[1], bs[2], n)
if err != nil {
panic(err)
break
}
//iface.core.log.Println("DEBUG: recv len:", n)
//msg := append(util_getBytes(), bs[:n]...)
msg := bs[:n]
var addr connAddr
addr.fromUDPAddr(udpAddr)
@ -330,9 +287,6 @@ func (iface *udpInterface) reader() {
////////////////////////////////////////////////////////////////////////////////
//const udp_chunkSize = 508 // Apparently the maximum guaranteed safe IPv4 size
//const udp_chunkSize = 65535 - 3 - 8
func udp_decode(bs []byte) (chunks, chunk, count uint8, payload []byte) {
if len(bs) >= 3 {
chunks, chunk, count, payload = bs[0], bs[1], bs[2], bs[3:]