|
@@ -15,6 +15,7 @@ import kotlinx.coroutines.channels.ReceiveChannel
|
15
|
15
|
import kotlinx.coroutines.channels.produce
|
16
|
16
|
import kotlinx.coroutines.io.ByteReadChannel
|
17
|
17
|
import kotlinx.coroutines.io.ByteWriteChannel
|
|
18
|
+import kotlinx.coroutines.sync.Mutex
|
18
|
19
|
import java.net.InetSocketAddress
|
19
|
20
|
import java.security.SecureRandom
|
20
|
21
|
import javax.net.ssl.X509TrustManager
|
|
@@ -45,6 +46,7 @@ internal class KtorLineBufferedSocket(private val host: String, private val port
|
45
|
46
|
var tlsTrustManager: X509TrustManager? = null
|
46
|
47
|
|
47
|
48
|
private val log by logger()
|
|
49
|
+ private val writeLock = Mutex()
|
48
|
50
|
|
49
|
51
|
private lateinit var socket: Socket
|
50
|
52
|
private lateinit var readChannel: ByteReadChannel
|
|
@@ -68,12 +70,17 @@ internal class KtorLineBufferedSocket(private val host: String, private val port
|
68
|
70
|
}
|
69
|
71
|
|
70
|
72
|
override suspend fun sendLine(line: ByteArray, offset: Int, length: Int) {
|
71
|
|
- with (writeChannel) {
|
72
|
|
- log.fine { ">>> ${String(line, offset, length)}" }
|
73
|
|
- writeAvailable(line, offset, length)
|
74
|
|
- writeByte(CARRIAGE_RETURN)
|
75
|
|
- writeByte(LINE_FEED)
|
76
|
|
- flush()
|
|
73
|
+ writeLock.lock()
|
|
74
|
+ try {
|
|
75
|
+ with(writeChannel) {
|
|
76
|
+ log.fine { ">>> ${String(line, offset, length)}" }
|
|
77
|
+ writeAvailable(line, offset, length)
|
|
78
|
+ writeByte(CARRIAGE_RETURN)
|
|
79
|
+ writeByte(LINE_FEED)
|
|
80
|
+ flush()
|
|
81
|
+ }
|
|
82
|
+ } finally {
|
|
83
|
+ writeLock.unlock()
|
77
|
84
|
}
|
78
|
85
|
}
|
79
|
86
|
|