-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
45 deletions.
There are no files selected for viewing
78 changes: 36 additions & 42 deletions
78
core/src/main/kotlin/com/jasonernst/kanonproxy/BidirectionalByteChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,69 @@ | ||
package com.jasonernst.kanonproxy | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.takeWhile | ||
import kotlinx.coroutines.runBlocking | ||
import org.slf4j.LoggerFactory | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.ByteChannel | ||
import kotlin.math.min | ||
|
||
class BidirectionalByteChannel : ByteChannel { | ||
private val logger = LoggerFactory.getLogger(javaClass) | ||
private val buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE) | ||
private var isOpen = true | ||
private val readyToWrite = MutableStateFlow(true) | ||
private val readyToRead = MutableStateFlow(false) | ||
// private val readyToRead = MutableStateFlow(false) | ||
|
||
override fun isOpen(): Boolean = isOpen | ||
|
||
override fun close() { | ||
this.isOpen = false | ||
readyToRead.value = true | ||
// readyToRead.value = true | ||
} | ||
|
||
override fun write(src: ByteBuffer): Int { | ||
if (readyToWrite.value.not()) { | ||
runBlocking { | ||
readyToWrite.takeWhile { !it }.collect {} | ||
} | ||
logger.debug("Waiting to write: ${src.limit()} bytes") | ||
synchronized(buffer) { | ||
val availableBytes = min(buffer.remaining(), src.remaining()) | ||
buffer.put(src.array(), src.position(), availableBytes) | ||
src.position(src.position() + availableBytes) | ||
// readyToRead.value = true | ||
logger.debug("Wrote $availableBytes bytes") | ||
return availableBytes | ||
} | ||
readyToRead.value = false | ||
val availableBytes = min(buffer.remaining(), src.remaining()) | ||
buffer.put(src.array(), src.position(), availableBytes) | ||
src.position(src.position() + availableBytes) | ||
readyToRead.value = true | ||
return availableBytes | ||
} | ||
|
||
override fun read(dst: ByteBuffer): Int { | ||
// when this function is called, we expect the buffer is pointing to the end of what was written to it | ||
// if its at zero, there is nothing to read | ||
if (buffer.position() == 0) { | ||
runBlocking { | ||
readyToRead.takeWhile { !it }.collect {} | ||
} | ||
} | ||
// if (buffer.position() == 0) { | ||
// runBlocking { | ||
// readyToRead.takeWhile { !it }.collect {} | ||
// } | ||
// } | ||
if (!isOpen) { | ||
return 0 | ||
} | ||
readyToWrite.value = false | ||
// just in case its flipped in the time it took us to get here | ||
if (readyToRead.value.not()) { | ||
readyToWrite.value = true | ||
return 0 | ||
} | ||
|
||
// flip the buffer to get it from write mode to read mode | ||
buffer.flip() | ||
val availableBytes = min(buffer.remaining(), dst.remaining()) | ||
dst.put(buffer.array(), buffer.position(), availableBytes) | ||
buffer.position(buffer.position() + availableBytes) | ||
if (!buffer.hasRemaining()) { | ||
readyToRead.value = false | ||
synchronized(buffer) { | ||
// flip the buffer to get it from write mode to read mode | ||
buffer.flip() | ||
val availableBytes = min(buffer.remaining(), dst.remaining()) | ||
dst.put(buffer.array(), buffer.position(), availableBytes) | ||
buffer.position(buffer.position() + availableBytes) | ||
// if (!buffer.hasRemaining()) { | ||
// readyToRead.value = false | ||
// } | ||
// compact to get us back into read mode | ||
buffer.compact() | ||
return availableBytes | ||
} | ||
// compact to get us back into read mode | ||
buffer.compact() | ||
readyToWrite.value = true | ||
return availableBytes | ||
} | ||
|
||
fun available(): Int = | ||
if (readyToRead.value.not()) { | ||
0 | ||
} else { | ||
buffer.position() // because we haven't flipped yet, this will be how many bytes there are to read | ||
// if (readyToRead.value.not()) { | ||
// 0 | ||
// } else { | ||
// buffer.position() // because we haven't flipped yet, this will be how many bytes there are to read | ||
// } | ||
synchronized(buffer) { | ||
return buffer.position() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters