mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-12-25 01:07:49 +00:00
eliminate some more copying between slices
This commit is contained in:
parent
f52955ee0f
commit
75b931f37e
@ -54,13 +54,13 @@ func (s *tunConn) reader() (err error) {
|
|||||||
s.tun.log.Debugln("Starting conn reader for", s.conn.String())
|
s.tun.log.Debugln("Starting conn reader for", s.conn.String())
|
||||||
defer s.tun.log.Debugln("Stopping conn reader for", s.conn.String())
|
defer s.tun.log.Debugln("Stopping conn reader for", s.conn.String())
|
||||||
var n int
|
var n int
|
||||||
b := make([]byte, 65535)
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-s.stop:
|
case <-s.stop:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
b := util.ResizeBytes(util.GetBytes(), 65535)
|
||||||
if n, err = s.conn.Read(b); err != nil {
|
if n, err = s.conn.Read(b); err != nil {
|
||||||
if e, eok := err.(yggdrasil.ConnError); eok && !e.Temporary() {
|
if e, eok := err.(yggdrasil.ConnError); eok && !e.Temporary() {
|
||||||
if e.Closed() {
|
if e.Closed() {
|
||||||
@ -71,9 +71,10 @@ func (s *tunConn) reader() (err error) {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
} else if n > 0 {
|
} else if n > 0 {
|
||||||
bs := append(util.GetBytes(), b[:n]...)
|
s.tun.send <- b[:n]
|
||||||
s.tun.send <- bs
|
|
||||||
s.stillAlive()
|
s.stillAlive()
|
||||||
|
} else {
|
||||||
|
util.PutBytes(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,8 +139,10 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Shift forward to avoid leaking bytes off the front of the slide when we eventually store it
|
if offset != 0 {
|
||||||
bs = append(recvd[:0], bs...)
|
// Shift forward to avoid leaking bytes off the front of the slice when we eventually store it
|
||||||
|
bs = append(recvd[:0], bs...)
|
||||||
|
}
|
||||||
// From the IP header, work out what our source and destination addresses
|
// From the IP header, work out what our source and destination addresses
|
||||||
// and node IDs are. We will need these in order to work out where to send
|
// and node IDs are. We will need these in order to work out where to send
|
||||||
// the packet
|
// the packet
|
||||||
@ -277,11 +279,12 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tun *TunAdapter) reader() error {
|
func (tun *TunAdapter) reader() error {
|
||||||
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
|
|
||||||
toWorker := make(chan []byte, 32)
|
toWorker := make(chan []byte, 32)
|
||||||
defer close(toWorker)
|
defer close(toWorker)
|
||||||
go tun.readerPacketHandler(toWorker)
|
go tun.readerPacketHandler(toWorker)
|
||||||
for {
|
for {
|
||||||
|
// Get a slice to store the packet in
|
||||||
|
recvd := util.ResizeBytes(util.GetBytes(), 65535+tun_ETHER_HEADER_LENGTH)
|
||||||
// Wait for a packet to be delivered to us through the TUN/TAP adapter
|
// Wait for a packet to be delivered to us through the TUN/TAP adapter
|
||||||
n, err := tun.iface.Read(recvd)
|
n, err := tun.iface.Read(recvd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -291,9 +294,10 @@ func (tun *TunAdapter) reader() error {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
|
util.PutBytes(recvd)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bs := append(util.GetBytes(), recvd[:n]...)
|
// Send the packet to the worker
|
||||||
toWorker <- bs
|
toWorker <- recvd[:n]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user