11package org .codeoverflow .chatoverflow .requirement .service .rcon
22
3- import java .io .{InputStream , OutputStream }
3+ import java .io .{DataInputStream , InputStream , OutputStream }
44import java .net .{Socket , SocketException }
55import java .nio .{ByteBuffer , ByteOrder }
66import 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 */
0 commit comments