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

Commit 6dea5bc

Browse files
committed
Cache connection errors. Wait 5 secounds till login to cause less connection errors.
1 parent 296d451 commit 6dea5bc

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

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

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

33
import java.io.{InputStream, OutputStream}
4-
import java.net.Socket
4+
import java.net.{Socket, SocketException}
55
import java.nio.{ByteBuffer, ByteOrder}
66
import java.util.Random
77

@@ -38,9 +38,11 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
3838
}
3939
}
4040
socket = new Socket(credentials.get.getValue("address").get, port)
41+
socket.setKeepAlive(true)
4142
outputStream = socket.getOutputStream
4243
inputStream = socket.getInputStream
4344
login()
45+
Thread.sleep(5000)
4446
true
4547
}
4648

@@ -53,19 +55,30 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
5355
}
5456

5557
private def write(packageType: Int, payload: Array[Byte]): Boolean = {
56-
val length = 4 + 4 + payload.length + 1 + 1
57-
var byteBuffer: ByteBuffer = ByteBuffer.allocate(length + 4)
58-
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
58+
try {
59+
val length = 4 + 4 + payload.length + 1 + 1
60+
var byteBuffer: ByteBuffer = ByteBuffer.allocate(length + 4)
61+
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
5962

60-
byteBuffer.putInt(length)
61-
byteBuffer.putInt(requestId)
62-
byteBuffer.putInt(packageType)
63-
byteBuffer.put(payload)
64-
byteBuffer.put(0x00.toByte)
65-
byteBuffer.put(0x00.toByte)
63+
byteBuffer.putInt(length)
64+
byteBuffer.putInt(requestId)
65+
byteBuffer.putInt(packageType)
66+
byteBuffer.put(payload)
67+
byteBuffer.put(0x00.toByte)
68+
byteBuffer.put(0x00.toByte)
6669

67-
outputStream.write(byteBuffer.array())
68-
outputStream.flush()
70+
outputStream.write(byteBuffer.array())
71+
outputStream.flush()
72+
} catch {
73+
case e: NullPointerException => {
74+
logger error "There was and is no Connection to the RCON Server, please try restarting."
75+
return false
76+
}
77+
case e: SocketException => {
78+
logger error "Connection Error to RCON Server. This request will not be sended!"
79+
return false
80+
}
81+
}
6982
true
7083
}
7184

0 commit comments

Comments
 (0)