New connection logic, a little more intuitive.
This commit is contained in:
parent
c15ab6595e
commit
e39446cd47
@ -9,6 +9,7 @@ import io.ktor.server.plugins.statuspages.*
|
|||||||
import io.ktor.server.request.*
|
import io.ktor.server.request.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import yenon.squadcompanion.logreader.LogReaderTail
|
import yenon.squadcompanion.logreader.LogReaderTail
|
||||||
import java.io.PrintWriter
|
import java.io.PrintWriter
|
||||||
import java.io.StringWriter
|
import java.io.StringWriter
|
||||||
@ -37,8 +38,12 @@ object Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val connection = RconConnection("195.201.62.238", 21114, "ThisIsJustATestServer")
|
val abstraction = runBlocking {
|
||||||
val abstraction = RconAbstraction(connection)
|
val connection = RconConnection("195.201.62.238", 21114, "ThisIsJustATestServer")
|
||||||
|
connection.connect()
|
||||||
|
RconAbstraction(connection)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
embeddedServer(Netty, 8080) {
|
embeddedServer(Netty, 8080) {
|
||||||
install(StatusPages) {
|
install(StatusPages) {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package yenon.squadcompanion
|
package yenon.squadcompanion
|
||||||
|
|
||||||
import io.ktor.utils.io.core.*
|
import io.ktor.utils.io.core.*
|
||||||
|
import io.ktor.utils.io.errors.*
|
||||||
import io.ktor.utils.io.nio.*
|
import io.ktor.utils.io.nio.*
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
@ -60,7 +61,12 @@ class RconConnection(private val host: String, private val port: Int, password:
|
|||||||
private val sameIdPacketList = LinkedList<RconPacket>()
|
private val sameIdPacketList = LinkedList<RconPacket>()
|
||||||
private val packetsFlow = MutableSharedFlow<RconPacket>(extraBufferCapacity = 8)
|
private val packetsFlow = MutableSharedFlow<RconPacket>(extraBufferCapacity = 8)
|
||||||
|
|
||||||
init {
|
suspend fun connect(): ConnectionStatus {
|
||||||
|
val status = reconnect()
|
||||||
|
if (status != ConnectionStatus.CONNECTED) {
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
delay(5000)
|
delay(5000)
|
||||||
@ -108,17 +114,30 @@ class RconConnection(private val host: String, private val port: Int, password:
|
|||||||
sameIdPacketList.clear()
|
sameIdPacketList.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ConnectionStatus.CONNECTED
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun reconnect(): Boolean = withContext(Dispatchers.IO) {
|
enum class ConnectionStatus {
|
||||||
|
CONNECTED, WRONG_PASSWORD, TIMEOUT
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun reconnect(): ConnectionStatus = withContext(Dispatchers.IO) {
|
||||||
socketChannel.close()
|
socketChannel.close()
|
||||||
socketChannel = SocketChannel.open().apply {
|
try {
|
||||||
connect(InetSocketAddress(host, port))
|
socketChannel = SocketChannel.open().apply {
|
||||||
finishConnect()
|
connect(InetSocketAddress(host, port))
|
||||||
println("Socket is open.")
|
finishConnect()
|
||||||
|
println("Socket is open.")
|
||||||
|
}
|
||||||
|
} catch (ex: IOException) {
|
||||||
|
return@withContext ConnectionStatus.TIMEOUT
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate()
|
return@withContext if (authenticate()) {
|
||||||
|
ConnectionStatus.CONNECTED
|
||||||
|
} else {
|
||||||
|
ConnectionStatus.WRONG_PASSWORD
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun authenticate(): Boolean {
|
private suspend fun authenticate(): Boolean {
|
||||||
@ -163,15 +182,6 @@ class RconConnection(private val host: String, private val port: Int, password:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun readPacket(): RconPacket {
|
|
||||||
var packet = readRawPacket()
|
|
||||||
while (packet.id == 0 && packet.type == 1) {
|
|
||||||
println("Console ${packet.data.size}: " + String(packet.data))
|
|
||||||
packet = readRawPacket()
|
|
||||||
}
|
|
||||||
return packet
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun readRawPacket(): RconPacket {
|
private fun readRawPacket(): RconPacket {
|
||||||
print("Reading new packet")
|
print("Reading new packet")
|
||||||
val size = socketChannel.readPacketExact(4L).readIntLittleEndian()
|
val size = socketChannel.readPacketExact(4L).readIntLittleEndian()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user