1
1
package org .codeoverflow .chatoverflow .requirement .service .rcon
2
2
3
- import java .io .{InputStream , OutputStream }
3
+ import java .io .{DataInputStream , InputStream , OutputStream }
4
4
import java .net .{Socket , SocketException }
5
5
import java .nio .{ByteBuffer , ByteOrder }
6
6
import java .util .Random
@@ -16,12 +16,19 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
16
16
private var outputStream : OutputStream = _
17
17
private var inputStream : InputStream = _
18
18
private var requestId : Int = 0
19
+ private var loggedIn = false
19
20
20
21
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
+ }
21
26
logger debug s " Sending $command to RCON "
22
27
requestId += 1
23
- write(2 , command.getBytes(" ASCII" ))
24
- " "
28
+ if (write(2 , command.getBytes(" ASCII" ))) {
29
+ return read()
30
+ }
31
+ null
25
32
}
26
33
27
34
@@ -50,8 +57,14 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
50
57
requestId = new Random ().nextInt(Integer .MAX_VALUE )
51
58
logger info " Logging RCON in..."
52
59
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
+ }
55
68
}
56
69
57
70
private def write (packageType : Int , payload : Array [Byte ]): Boolean = {
@@ -82,6 +95,29 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
82
95
true
83
96
}
84
97
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
+
85
121
/**
86
122
* This stops the activity of the connector, e.g. by closing the platform connection.
87
123
*/
0 commit comments