TCP link basically works

pull/276/head^2
Kevin Hester 2021-03-30 23:19:05 +08:00
rodzic 04f4a2a342
commit d9a2a469bc
1 zmienionych plików z 20 dodań i 7 usunięć

Wyświetl plik

@ -1,12 +1,14 @@
package com.geeksville.mesh.service package com.geeksville.mesh.service
import com.geeksville.android.Logging import com.geeksville.android.Logging
import java.io.BufferedInputStream import com.geeksville.util.Exceptions
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
import java.net.InetAddress import java.net.InetAddress
import java.net.Socket import java.net.Socket
import java.net.SocketTimeoutException
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -43,6 +45,7 @@ class TCPInterface(service: RadioInterfaceService, private val address: String)
override fun onDeviceDisconnect(waitForStopped: Boolean) { override fun onDeviceDisconnect(waitForStopped: Boolean) {
val s = socket val s = socket
if (s != null) { if (s != null) {
debug("Closing TCP socket")
socket = null socket = null
outStream.close() outStream.close()
inStream.close() inStream.close()
@ -65,6 +68,7 @@ class TCPInterface(service: RadioInterfaceService, private val address: String)
val port = 4403 val port = 4403
val s = Socket(a, port) val s = Socket(a, port)
s.tcpNoDelay = true s.tcpNoDelay = true
s.soTimeout = 500
socket = s socket = s
outStream = BufferedOutputStream(s.getOutputStream()) outStream = BufferedOutputStream(s.getOutputStream())
inStream = s.getInputStream() inStream = s.getInputStream()
@ -73,14 +77,23 @@ class TCPInterface(service: RadioInterfaceService, private val address: String)
super.connect() super.connect()
while (true) { while (true) {
val c = inStream.read() try {
if (c == -1) val c = inStream.read()
break if (c == -1) {
else warn("Got EOF on TCP stream")
readChar(c.toByte()) onDeviceDisconnect(false)
break
} else
readChar(c.toByte())
} catch (ex: SocketTimeoutException) {
// Ignore and start another read
}
} }
} catch (ex: IOException) {
errormsg("IOException in TCP reader: $ex") // FIXME, show message to user
onDeviceDisconnect(false)
} catch (ex: Throwable) { } catch (ex: Throwable) {
errormsg("Exception in TCP reader: $ex") Exceptions.report(ex, "Exception in TCP reader")
onDeviceDisconnect(false) onDeviceDisconnect(false)
} }
debug("Exiting TCP reader") debug("Exiting TCP reader")