From dd8817f50e5657259b7ed3e1af3f8c370525cf2f Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 1 Jul 2019 15:23:24 +0200 Subject: device: receive: simplify flush loop --- device/receive.go | 47 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) (limited to 'device') diff --git a/device/receive.go b/device/receive.go index 62b5ef4..eb6e3b2 100644 --- a/device/receive.go +++ b/device/receive.go @@ -485,33 +485,6 @@ func (device *Device) RoutineHandshake() { } } -func (peer *Peer) elementStopOrFlush(shouldFlush *bool) (stop bool, elemOk bool, elem *QueueInboundElement) { - if !*shouldFlush { - select { - case <-peer.routines.stop: - stop = true - return - case elem, elemOk = <-peer.queue.inbound: - return - } - } else { - select { - case <-peer.routines.stop: - stop = true - return - case elem, elemOk = <-peer.queue.inbound: - return - default: - *shouldFlush = false - err := peer.device.tun.device.Flush() - if err != nil { - peer.device.log.Error.Printf("Unable to flush packets: %v", err) - } - return peer.elementStopOrFlush(shouldFlush) - } - } -} - func (peer *Peer) RoutineSequentialReceiver() { device := peer.device @@ -520,10 +493,6 @@ func (peer *Peer) RoutineSequentialReceiver() { logDebug := device.log.Debug var elem *QueueInboundElement - var ok bool - var stop bool - - shouldFlush := false defer func() { logDebug.Println(peer, "- Routine: sequential receiver - stopped") @@ -549,9 +518,14 @@ func (peer *Peer) RoutineSequentialReceiver() { elem = nil } - stop, ok, elem = peer.elementStopOrFlush(&shouldFlush) - if stop || !ok { + var elemOk bool + select { + case <-peer.routines.stop: return + case elem, elemOk = <-peer.queue.inbound: + if !elemOk { + return + } } // wait for decryption @@ -660,8 +634,11 @@ func (peer *Peer) RoutineSequentialReceiver() { offset := MessageTransportOffsetContent _, err := device.tun.device.Write(elem.buffer[:offset+len(elem.packet)], offset) - if err == nil { - shouldFlush = true + if len(peer.queue.inbound) == 0 { + err = device.tun.device.Flush() + if err != nil { + peer.device.log.Error.Printf("Unable to flush packets: %v", err) + } } if err != nil && !device.isClosed.Get() { logError.Println("Failed to write packet to TUN device:", err) -- cgit v1.2.3