Skip to content

Commit da9437e

Browse files
committed
Fix bug with unix socket reads
1 parent 603e04e commit da9437e

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main/kotlin/info/p2sh/clightning/rpc/RpcClient.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package info.p2sh.clightning.rpc
1818

1919
import com.google.gson.Gson
20+
import com.google.gson.JsonParser
2021
import info.p2sh.clightning.http.JsonRpcError
2122
import info.p2sh.clightning.http.JsonRpcRequest
2223
import info.p2sh.clightning.http.JsonRpcResponse
@@ -29,6 +30,8 @@ import java.nio.CharBuffer
2930
import java.nio.channels.Channels
3031

3132
class RpcClient(private val path: String, private val gson: Gson) {
33+
private val jsonParser = JsonParser()
34+
3235
fun sendRequest(request: JsonRpcRequest): JsonRpcResponse {
3336
val socketFile = File(path)
3437
val address = UnixSocketAddress(socketFile)
@@ -39,16 +42,23 @@ class RpcClient(private val path: String, private val gson: Gson) {
3942
it.flush()
4043

4144
InputStreamReader(Channels.newInputStream(channel)).use {
42-
val builder = StringBuilder()
45+
var result = ""
46+
var isValidJson = false
4347

4448
do {
45-
val result = CharBuffer.allocate(8192)
46-
it.read(result)
47-
result.flip()
48-
builder.append(result)
49-
} while (result.length == 8192)
49+
val chunk = CharBuffer.allocate(8192)
50+
it.read(chunk)
51+
chunk.flip()
52+
result += chunk.toString()
53+
54+
try {
55+
jsonParser.parse(result)
56+
isValidJson = true
57+
} catch (e: Exception) {
58+
}
59+
} while (chunk.isNotEmpty() && !isValidJson)
5060

51-
builder.toString()
61+
result
5262
}
5363
}
5464

0 commit comments

Comments
 (0)