mirror of https://github.com/sbt/sbt.git
Merge pull request #5677 from adpi2/fix/exit-bsp-client
Exit BspClient after server socket is closed
This commit is contained in:
commit
2072eba0a9
|
|
@ -18,24 +18,47 @@ import scala.sys.process.Process
|
||||||
import scala.util.control.NonFatal
|
import scala.util.control.NonFatal
|
||||||
|
|
||||||
class BspClient private (sbtServer: Socket) {
|
class BspClient private (sbtServer: Socket) {
|
||||||
|
private val lock = new AnyRef
|
||||||
|
private var terminated = false
|
||||||
|
|
||||||
private def transferTo(input: InputStream, output: OutputStream): Unit = {
|
private def transferTo(input: InputStream, output: OutputStream): Thread = {
|
||||||
val buffer = Array.ofDim[Byte](1024)
|
val thread = new Thread {
|
||||||
while (true) {
|
override def run(): Unit = {
|
||||||
val size = input.read(buffer)
|
val buffer = Array.ofDim[Byte](1024)
|
||||||
output.write(buffer, 0, size)
|
try {
|
||||||
output.flush()
|
while (!terminated) {
|
||||||
|
val size = input.read(buffer)
|
||||||
|
if (size == -1) {
|
||||||
|
terminated = true
|
||||||
|
} else {
|
||||||
|
output.write(buffer, 0, size)
|
||||||
|
output.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
input.close()
|
||||||
|
output.close()
|
||||||
|
} catch {
|
||||||
|
case NonFatal(_) => ()
|
||||||
|
} finally {
|
||||||
|
lock.synchronized {
|
||||||
|
terminated = true
|
||||||
|
lock.notify()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
thread.setDaemon(true)
|
||||||
|
thread
|
||||||
}
|
}
|
||||||
|
|
||||||
private def run(): Exit = {
|
private def run(): Exit = {
|
||||||
try {
|
try {
|
||||||
val redirection = new Thread {
|
transferTo(sbtServer.getInputStream, System.out).start()
|
||||||
override def run(): Unit = transferTo(sbtServer.getInputStream, System.out)
|
transferTo(System.in, sbtServer.getOutputStream).start()
|
||||||
}
|
|
||||||
|
|
||||||
redirection.start()
|
lock.synchronized {
|
||||||
transferTo(System.in, sbtServer.getOutputStream)
|
while (!terminated) lock.wait()
|
||||||
|
}
|
||||||
|
|
||||||
Exit(0)
|
Exit(0)
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue