Commit 35793998 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Catch IO errors in IO operations

parent dd731f54
......@@ -70,6 +70,7 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
private var proxyThreadActive:Boolean = false
private var cleanupFinalized:Boolean = false
private val clearLock = ReentrantReadWriteLock()
private var ioErrors = 0
constructor(packetProxy: PacketProxy,
trafficStats: TrafficStats = TrafficStats(0, 0),
......@@ -175,9 +176,9 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
logger?.info("Device filed descriptors became invalid")
shouldRun = false
} else {
if (pollableAnswers.second.isNotEmpty()) processFutureAnswers(pollableAnswers.second, true)
if (deviceOutputPipe.actualEvent(OsConstants.POLLOUT)) fromLoopbackToDevice(deviceOutput) // There is data in loopback
if (deviceInputPipe.actualEvent(OsConstants.POLLIN)) fromDeviceToPacketProxy(deviceInput)
if (pollableAnswers.second.isNotEmpty()) tryIO { processFutureAnswers(pollableAnswers.second, true) }
if (deviceOutputPipe.actualEvent(OsConstants.POLLOUT)) tryIO {fromLoopbackToDevice(deviceOutput) } // There is data in loopback
if (deviceInputPipe.actualEvent(OsConstants.POLLIN)) tryIO { fromDeviceToPacketProxy(deviceInput) }
Thread.sleep(1)
}
} else if (shouldRun) {
......@@ -236,6 +237,20 @@ open class VPNTunnelProxy(val packetProxy: PacketProxy,
logger?.info("Done with run().")
}
private fun tryIO(block:() -> Unit) {
try {
block()
} catch (ex:IOException) {
if(ex is InterruptedIOException) throw ex
// Continued errors might indicate an underlying problem.
// either way, 100 is a lot and takes a lot of time to reach
// (besides, this error is again caught in another try)
if(++ioErrors >= 100) {
throw ex
}
}
}
private val packetFromDevice = ByteArray(32767)
fun fromDeviceToPacketProxy(deviceInput:InputStream) {
val readSize = deviceInput.read(packetFromDevice)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment