Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit f51df0f

Browse files
committed
Add reading of incomming RCON messages
1 parent 6dea5bc commit f51df0f

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/RconConnector.scala

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codeoverflow.chatoverflow.requirement.service.rcon
22

3-
import java.io.{InputStream, OutputStream}
3+
import java.io.{DataInputStream, InputStream, OutputStream}
44
import java.net.{Socket, SocketException}
55
import java.nio.{ByteBuffer, ByteOrder}
66
import java.util.Random
@@ -16,12 +16,19 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
1616
private var outputStream: OutputStream = _
1717
private var inputStream: InputStream = _
1818
private var requestId: Int = 0
19+
private var loggedIn = false
1920

2021
def sendCommand(command: String): String = {
22+
if (!loggedIn) {
23+
logger error "Could not execute RCON Command due to wrong password or no connection"
24+
return null
25+
}
2126
logger debug s"Sending $command to RCON"
2227
requestId += 1
23-
write(2, command.getBytes("ASCII"))
24-
""
28+
if (write(2, command.getBytes("ASCII"))) {
29+
return read()
30+
}
31+
null
2532
}
2633

2734

@@ -50,8 +57,14 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
5057
requestId = new Random().nextInt(Integer.MAX_VALUE)
5158
logger info "Logging RCON in..."
5259
val password = credentials.get.getValue("password").get
53-
write(3, password.getBytes("ASCII"))
54-
logger debug "RCON Login sent"
60+
if (write(3, password.getBytes("ASCII"))) {
61+
if (read() == null) {
62+
logger error "Could not log in to RCON Server. Password is Wrong!"
63+
} else {
64+
logger debug "Login to RCON was successful"
65+
loggedIn = true
66+
}
67+
}
5568
}
5669

5770
private def write(packageType: Int, payload: Array[Byte]): Boolean = {
@@ -82,6 +95,29 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
8295
true
8396
}
8497

98+
private def read(): String = {
99+
try {
100+
val header: Array[Byte] = Array.ofDim[Byte](4*3)
101+
inputStream.read(header)
102+
val headerBuffer: ByteBuffer = ByteBuffer.wrap(header)
103+
headerBuffer.order(ByteOrder.LITTLE_ENDIAN)
104+
val length = headerBuffer.getInt()
105+
val packageType = headerBuffer.getInt
106+
val payload: Array[Byte] = Array.ofDim[Byte](length - 4 - 4 - 2)
107+
val dataInputStream: DataInputStream = new DataInputStream(inputStream)
108+
dataInputStream.readFully(payload)
109+
dataInputStream.read(Array.ofDim[Byte](2))
110+
if (packageType == -1) {
111+
return null
112+
}
113+
new String(payload, "ASCII")
114+
} catch {
115+
case e: NegativeArraySizeException => null;
116+
}
117+
}
118+
119+
private[rcon] def isLoggedIn: Boolean = loggedIn
120+
85121
/**
86122
* This stops the activity of the connector, e.g. by closing the platform connection.
87123
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.rcon.impl
2+
3+
import org.codeoverflow.chatoverflow.WithLogger
4+
import org.codeoverflow.chatoverflow.api.io.input.RconInput
5+
import org.codeoverflow.chatoverflow.registry.Impl
6+
import org.codeoverflow.chatoverflow.requirement.InputImpl
7+
import org.codeoverflow.chatoverflow.requirement.service.rcon.RconConnector
8+
9+
@Impl(impl = classOf[RconInput], connector = classOf[RconConnector])
10+
class RconInputImpl extends InputImpl[RconConnector] with RconInput with WithLogger {
11+
override def getCommandOutput(command: String): String = sourceConnector.get.sendCommand(command)
12+
13+
/**
14+
* Start the input, called after source connector did init
15+
*
16+
* @return true if starting the input was successful, false if some problems occurred
17+
*/
18+
override def start(): Boolean = sourceConnector.get.isLoggedIn
19+
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/impl/RconOutputImpl.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import org.codeoverflow.chatoverflow.requirement.service.rcon.RconConnector
88

99
@Impl(impl = classOf[RconOutput], connector = classOf[RconConnector])
1010
class RconOutputImpl extends OutputImpl[RconConnector] with RconOutput with WithLogger {
11-
override def sendCommand(command: String): String = sourceConnector.get.sendCommand(command)
11+
override def sendCommand(command: String): Boolean = {
12+
sourceConnector.get.sendCommand(command) != null
13+
}
1214

1315
/**
1416
* Start the input, called after source connector did init
1517
*
1618
* @return true if starting the input was successful, false if some problems occurred
1719
*/
18-
override def start(): Boolean = true
20+
override def start(): Boolean = sourceConnector.get.isLoggedIn
1921
}

0 commit comments

Comments
 (0)