Browse Source

Fix concurrency issue writing lines, bump to 0.3.1

tags/v0.3.1
Chris Smith 5 years ago
parent
commit
4d1b35fa8a
3 changed files with 17 additions and 7 deletions
  1. 3
    0
      CHANGELOG
  2. 1
    1
      build.gradle.kts
  3. 13
    6
      src/main/kotlin/com/dmdirc/ktirc/io/LineBufferedSocket.kt

+ 3
- 0
CHANGELOG View File

@@ -1,6 +1,9 @@
1 1
 vNEXT (in development)
2 2
 
3
+v0.3.1
4
+
3 5
   * Added more documentation to public methods/classes
6
+  * Fixed exception when sending multiple lines at once (e.g. when connecting!)
4 7
 
5 8
 v0.3.0
6 9
 

+ 1
- 1
build.gradle.kts View File

@@ -1,6 +1,6 @@
1 1
 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2 2
 
3
-version = "0.3.0"
3
+version = "0.3.1"
4 4
 group = "com.dmdirc.ktirc"
5 5
 
6 6
 plugins {

+ 13
- 6
src/main/kotlin/com/dmdirc/ktirc/io/LineBufferedSocket.kt View File

@@ -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
 

Loading…
Cancel
Save