Meshtastic-Android/app/src/main/java/com/geeksville/mesh/service/TCPInterface.kt

102 lines
3.1 KiB
Kotlin
Raw Normal View History

2021-03-29 19:47:24 +08:00
package com.geeksville.mesh.service
2021-03-29 20:20:38 +08:00
import com.geeksville.android.Logging
2021-03-30 23:19:05 +08:00
import com.geeksville.util.Exceptions
2021-03-29 20:33:06 +08:00
import java.io.BufferedOutputStream
2021-03-30 23:19:05 +08:00
import java.io.IOException
2021-03-29 20:33:06 +08:00
import java.io.InputStream
import java.io.OutputStream
2021-03-29 19:47:24 +08:00
import java.net.InetAddress
import java.net.Socket
2021-03-30 23:19:05 +08:00
import java.net.SocketTimeoutException
2021-03-29 19:47:24 +08:00
import kotlin.concurrent.thread
class TCPInterface(service: RadioInterfaceService, private val address: String) :
StreamInterface(service) {
2021-03-29 20:20:38 +08:00
companion object : Logging, InterfaceFactory('t') {
override fun createInterface(
service: RadioInterfaceService,
rest: String
): IRadioInterface = TCPInterface(service, rest)
init {
registerFactory()
}
}
2021-03-29 20:33:06 +08:00
var socket: Socket? = null
2021-03-29 19:47:24 +08:00
lateinit var outStream: OutputStream
lateinit var inStream: InputStream
init {
connect()
}
override fun sendBytes(p: ByteArray) {
2021-03-29 20:33:06 +08:00
outStream.write(p)
2021-03-29 19:47:24 +08:00
}
override fun flushBytes() {
outStream.flush()
}
override fun onDeviceDisconnect(waitForStopped: Boolean) {
val s = socket
2021-03-29 20:33:06 +08:00
if (s != null) {
2021-03-30 23:19:05 +08:00
debug("Closing TCP socket")
2021-03-29 19:47:24 +08:00
socket = null
outStream.close()
inStream.close()
s.close()
}
super.onDeviceDisconnect(waitForStopped)
}
override fun connect() {
//here you must put your computer's IP address.
//here you must put your computer's IP address.
// No need to keep a reference to this thread - it will exit when we close inStream
thread(start = true, isDaemon = true, name = "TCP reader") {
try {
val a = InetAddress.getByName(address)
debug("TCP connecting to $address")
//create a socket to make the connection with the server
val port = 4403
val s = Socket(a, port)
s.tcpNoDelay = true
2021-03-30 23:19:05 +08:00
s.soTimeout = 500
socket = s
outStream = BufferedOutputStream(s.getOutputStream())
inStream = s.getInputStream()
// Note: we call the super method FROM OUR NEW THREAD
super.connect()
2021-03-29 20:33:06 +08:00
while (true) {
2021-03-30 23:19:05 +08:00
try {
val c = inStream.read()
if (c == -1) {
warn("Got EOF on TCP stream")
onDeviceDisconnect(false)
break
} else
readChar(c.toByte())
} catch (ex: SocketTimeoutException) {
// Ignore and start another read
}
2021-03-29 19:47:24 +08:00
}
2021-03-30 23:19:05 +08:00
} catch (ex: IOException) {
errormsg("IOException in TCP reader: $ex") // FIXME, show message to user
onDeviceDisconnect(false)
2021-03-29 20:33:06 +08:00
} catch (ex: Throwable) {
2021-03-30 23:19:05 +08:00
Exceptions.report(ex, "Exception in TCP reader")
2021-03-29 19:47:24 +08:00
onDeviceDisconnect(false)
}
debug("Exiting TCP reader")
}
}
}