[lsp] refactoring, separate withBaseProtocolFraming and tcpServer implementations to different files

LSP-166

GitOrigin-RevId: a0ac03f9ddc44e1395c9365dd01d1648ff374d20
This commit is contained in:
Ilya Kirillov
2025-06-18 09:39:04 +00:00
committed by intellij-monorepo-bot
parent 98961a84f1
commit 5c11c924ba
2 changed files with 40 additions and 28 deletions
@@ -3,9 +3,7 @@ package com.jetbrains.lsp.implementation
import com.jetbrains.lsp.protocol.Initialize
import com.jetbrains.lsp.protocol.InitializeResult
import com.jetbrains.lsp.protocol.*
import fleet.util.logging.logger
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.Channel
@@ -14,33 +12,7 @@ import kotlinx.serialization.json.JsonElement
import java.io.InputStream
import java.io.OutputStream
import java.io.ByteArrayOutputStream
import java.net.ServerSocket
suspend fun tcpServer(port: Int = 0, server: suspend CoroutineScope.(InputStream, OutputStream) -> Unit) {
ServerSocket(port).use { serverSocket ->
LOG.info("Server is listening on port ${serverSocket.localPort}")
supervisorScope {
while (true) {
val clientSocket = runInterruptible(Dispatchers.IO) {
serverSocket.accept()
}
LOG.info("A new client connected ${clientSocket.inetAddress}:${clientSocket.port}")
launch(start = CoroutineStart.ATOMIC) {
clientSocket.use {
val input = clientSocket.getInputStream()
val output = clientSocket.getOutputStream()
coroutineScope {
server(input, output)
}
}
LOG.info("Client disconnected ${clientSocket.inetAddress}:${clientSocket.port}")
}
}
}
}
}
private val LOG = logger<LspClient>()
private fun InputStream.readLine(): String {
val buffer = ByteArrayOutputStream()
@@ -0,0 +1,40 @@
package com.jetbrains.lsp.implementation
import fleet.util.logging.logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.supervisorScope
import java.io.InputStream
import java.io.OutputStream
import java.net.ServerSocket
import kotlin.io.use
suspend fun tcpServer(port: Int = 0, server: suspend CoroutineScope.(InputStream, OutputStream) -> Unit) {
ServerSocket(port).use { serverSocket ->
LOG.info("Server is listening on port ${serverSocket.localPort}")
supervisorScope {
while (true) {
val clientSocket = runInterruptible(Dispatchers.IO) {
serverSocket.accept()
}
LOG.info("A new client connected ${clientSocket.inetAddress}:${clientSocket.port}")
launch(start = CoroutineStart.ATOMIC) {
clientSocket.use {
val input = clientSocket.getInputStream()
val output = clientSocket.getOutputStream()
coroutineScope {
server(input, output)
}
}
LOG.info("Client disconnected ${clientSocket.inetAddress}:${clientSocket.port}")
}
}
}
}
}
private val LOG = logger<LspClient>()