@@ -11,8 +11,6 @@ import io.github.oshai.kotlinlogging.KotlinLogging
1111import io.github.oshai.kotlinlogging.withLoggingContext
1212import java.io.Closeable
1313import java.io.IOException
14- import java.util.concurrent.ArrayBlockingQueue
15- import java.util.concurrent.BlockingQueue
1614import java.util.concurrent.ScheduledExecutorService
1715import java.util.concurrent.TimeUnit
1816
@@ -36,7 +34,6 @@ class RemotePeerOrchestrator(
3634 " RemotePeerOrchestrator(localPlayerId=$localPlayerId ,remotePlayerId=$remotePlayerId ,isOfferer=$isOfferer ,forceRelay=$forceRelay ,...)"
3735
3836 val udpBridgePort: Int? get() = udpSocketBridge?.bridgePort
39- private val toRemoteQueue: BlockingQueue <ProtocolPacket > = ArrayBlockingQueue (32 , true )
4037
4138 private val objectLock = Object ()
4239 private var iceState: IceState = IceState .NEW
@@ -63,7 +60,7 @@ class RemotePeerOrchestrator(
6360
6461 udpSocketBridge = UdpSocketBridge (
6562 lobbyPort = lobbyPort,
66- forwardToIce = { toRemoteQueue.put (GameDataPacket (it)) },
63+ forwardToIce = { sendToRemotePlayer (GameDataPacket (it)) },
6764 name = " player-$remotePlayerId " ,
6865 ).apply { start() }
6966
@@ -93,8 +90,6 @@ class RemotePeerOrchestrator(
9390 connected = true
9491 remoteListenerThread = Thread { readFromRemotePlayerLoop() }
9592 .apply { start() }
96- remoteSenderThread = Thread { sendToRemotePlayerLoop() }
97- .apply { start() }
9893
9994 logger.info { " $this connected: ice port ${this .agent?.port} <-> udp bridge port ${this .udpBridgePort} " }
10095 }
@@ -117,7 +112,7 @@ class RemotePeerOrchestrator(
117112 }
118113
119114 override fun sendEcho () {
120- toRemoteQueue.put (EchoPacket ())
115+ sendToRemotePlayer (EchoPacket ())
121116 }
122117
123118 override fun onConnectionLost () {
@@ -196,37 +191,6 @@ class RemotePeerOrchestrator(
196191 logger.debug { " No longer listening for messages from ICE" }
197192 }
198193
199- private fun sendToRemotePlayerLoop () {
200- while (! closing) {
201- if (! connected) {
202- Thread .sleep(500 )
203- continue
204- }
205-
206- when {
207- closing -> break
208- ! connected -> {
209- logger.info { " sendToRemotePlayerLoop shutdown due to connection lost" }
210- break
211- }
212-
213- else -> {
214- val message: ProtocolPacket ? = toRemoteQueue.peek()
215- val success = if (message == null ) false else sendToRemotePlayer(message)
216- if (success) {
217- // in case we could send successfully, discard it
218- toRemoteQueue.remove()
219- } else {
220- // otherwise, we keep it and wait for better times (reconnect)
221- Thread .sleep(500 )
222- }
223- }
224- }
225- }
226-
227- logger.info { " sendToRemotePlayerLoop shutdown due to closing" }
228- }
229-
230194 private fun sendToRemotePlayer (data : ProtocolPacket ): Boolean =
231195 try {
232196 checkNotNull(agent).send(data.buildPrefixedWireData())
0 commit comments