Skip to content

Commit

Permalink
Revert "feat: avoid port collision on Socket creation of the TcpForwa…
Browse files Browse the repository at this point in the history
…rder (#69)"

This reverts commit c3d300b.
  • Loading branch information
igorsmotto committed Sep 4, 2024
1 parent 780b2a5 commit f2385a6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
18 changes: 5 additions & 13 deletions dadb/src/main/kotlin/dadb/Dadb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package dadb

import dadb.adbserver.AdbServer
import dadb.forwarding.TcpForwarder
import dadb.forwarding.TcpForwardDescriptor
import java.io.File
import java.io.InputStream
import java.nio.file.Files
import okio.*

Expand Down Expand Up @@ -240,19 +240,11 @@ interface Dadb : AutoCloseable {
}

@Throws(InterruptedException::class)
fun tcpForward(hostPort: Int, targetPort: Int): TcpForwardDescriptor {
val forwarder = TcpForwarder(this, targetPort, hostPort)
val localPort = forwarder.start()
fun tcpForward(hostPort: Int, targetPort: Int): AutoCloseable {
val forwarder = TcpForwarder(this, hostPort, targetPort)
forwarder.start()

return TcpForwardDescriptor(forwarder, localPort)
}

@Throws(InterruptedException::class)
fun tcpForward(targetPort: Int): TcpForwardDescriptor {
val forwarder = TcpForwarder(this, targetPort)
val localPort = forwarder.start()

return TcpForwardDescriptor(forwarder, localPort)
return forwarder
}

companion object {
Expand Down
14 changes: 3 additions & 11 deletions dadb/src/main/kotlin/dadb/forwarding/TcpForwarder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,18 @@ import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlin.concurrent.thread

data class TcpForwardDescriptor(private val resource: AutoCloseable, val localPort: Int): AutoCloseable {
override fun close() {
resource.close()
}
}

internal class TcpForwarder(
private val dadb: Dadb,
private val hostPort: Int,
private val targetPort: Int,
private val hostPort: Int? = null,
) : AutoCloseable {

private var state: State = State.STOPPED
private var serverThread: Thread? = null
private var server: ServerSocket? = null
private var clientExecutor: ExecutorService? = null

fun start(): Int {
fun start() {
check(state == State.STOPPED) { "Forwarder is already started at port $hostPort" }

moveToState(State.STARTING)
Expand All @@ -55,12 +49,10 @@ internal class TcpForwarder(
waitFor(10, 5000) {
state == State.STARTED
}

return server!!.localPort
}

private fun handleForwarding() {
val serverRef = ServerSocket(hostPort ?: 0)
val serverRef = ServerSocket(hostPort)
server = serverRef

moveToState(State.STARTED)
Expand Down

0 comments on commit f2385a6

Please sign in to comment.