Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 5 | @file:Suppress("UsePropertyAccessSyntax") // for ByteBuffer.getShort/getInt/etc |
| 6 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 7 | package kotlinx.coroutines.experimental.io |
| 8 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 9 | import kotlinx.atomicfu.* |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 10 | import kotlinx.coroutines.experimental.* |
| 11 | import kotlinx.coroutines.experimental.channels.* |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 12 | import kotlinx.coroutines.experimental.io.internal.* |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 13 | import kotlinx.coroutines.experimental.io.packet.* |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 14 | import kotlinx.io.core.* |
| 15 | import kotlinx.io.core.ByteReadPacket |
| 16 | import kotlinx.io.pool.* |
| 17 | import java.io.EOFException |
| 18 | import java.nio.* |
| 19 | import java.util.concurrent.atomic.* |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 20 | import kotlin.coroutines.experimental.* |
| 21 | import kotlin.coroutines.experimental.intrinsics.* |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 22 | |
Roman Elizarov | 339ccf3 | 2017-09-27 17:44:00 +0300 | [diff] [blame] | 23 | internal const val DEFAULT_CLOSE_MESSAGE = "Byte channel was closed" |
| 24 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 25 | // implementation for ByteChannel |
| 26 | internal class ByteBufferChannel( |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 27 | override val autoFlush: Boolean, |
| 28 | private val pool: ObjectPool<ReadWriteBufferState.Initial> = BufferObjectPool, |
| 29 | private val reservedSize: Int = RESERVED_SIZE |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 30 | ) : ByteChannel, LookAheadSuspendSession { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 31 | // internal constructor for reading of byte buffers |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 32 | constructor(content: ByteBuffer) : this(false, BufferObjectNoPool, 0) { |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 33 | state = ReadWriteBufferState.Initial(content.slice(), 0).apply { |
| 34 | capacity.resetForRead() |
| 35 | }.startWriting() |
| 36 | restoreStateAfterWrite() |
| 37 | close() |
| 38 | tryTerminate() |
| 39 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 40 | |
| 41 | @Volatile |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 42 | private var state: ReadWriteBufferState = ReadWriteBufferState.IdleEmpty |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 43 | |
| 44 | @Volatile |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 45 | private var closed: ClosedElement? = null |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 46 | |
| 47 | @Volatile |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 48 | private var joining: JoiningState? = null |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 49 | |
| 50 | @Volatile |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 51 | private var readOp: Continuation<Boolean>? = null |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 52 | |
| 53 | @Volatile |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 54 | private var writeOp: Continuation<Unit>? = null |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 55 | |
| 56 | private var readPosition = 0 |
| 57 | private var writePosition = 0 |
| 58 | |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 59 | @Volatile |
| 60 | private var attachedJob: Job? = null |
| 61 | |
| 62 | internal fun attachJob(job: Job) { |
| 63 | attachedJob?.cancel() |
| 64 | attachedJob = job |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 65 | job.invokeOnCompletion(onCancelling = true) { cause -> |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 66 | attachedJob = null |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 67 | if (cause != null) cancel(cause) |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 71 | override var readByteOrder: ByteOrder = ByteOrder.BIG_ENDIAN |
| 72 | override var writeByteOrder: ByteOrder = ByteOrder.BIG_ENDIAN |
Sergey Mashkov | 8af8937 | 2017-12-05 12:35:56 +0300 | [diff] [blame] | 73 | set(newOrder) { |
| 74 | if (field != newOrder) { |
| 75 | field = newOrder |
| 76 | joining?.delegatedTo?.writeByteOrder = newOrder |
| 77 | } |
| 78 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 79 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 80 | override val availableForRead: Int |
| 81 | get() = state.capacity.availableForRead |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 82 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 83 | override val availableForWrite: Int |
| 84 | get() = state.capacity.availableForWrite |
| 85 | |
| 86 | override val isClosedForRead: Boolean |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 87 | get() = state === ReadWriteBufferState.Terminated && closed != null |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 88 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 89 | override val isClosedForWrite: Boolean |
| 90 | get() = closed != null |
| 91 | |
Sergey Mashkov | aae062d | 2017-09-07 15:12:50 +0300 | [diff] [blame] | 92 | @Volatile |
| 93 | override var totalBytesRead: Long = 0L |
| 94 | private set |
| 95 | |
| 96 | @Volatile |
| 97 | override var totalBytesWritten: Long = 0L |
| 98 | private set |
| 99 | |
Sergey Mashkov | 8fbdff4 | 2018-02-02 17:17:55 +0300 | [diff] [blame] | 100 | override val closedCause: Throwable? |
| 101 | get() = closed?.cause |
| 102 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 103 | override fun close(cause: Throwable?): Boolean { |
| 104 | if (closed != null) return false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 105 | val newClosed = if (cause == null) ClosedElement.EmptyCause else ClosedElement(cause) |
Sergey Mashkov | ad68c06 | 2017-10-04 21:34:18 +0300 | [diff] [blame] | 106 | state.capacity.flush() |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 107 | if (!Closed.compareAndSet(this, null, newClosed)) return false |
Sergey Mashkov | 8e13241 | 2017-09-11 12:39:46 +0300 | [diff] [blame] | 108 | state.capacity.flush() |
Sergey Mashkov | 24b040f | 2017-08-25 11:06:59 +0300 | [diff] [blame] | 109 | if (state.capacity.isEmpty() || cause != null) tryTerminate() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 110 | resumeClosed(cause) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 111 | |
| 112 | if (state === ReadWriteBufferState.Terminated) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 113 | joining?.let { ensureClosedJoined(it) } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 114 | } |
| 115 | |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 116 | if (cause != null) attachedJob?.cancel(cause) |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 117 | // readSuspendContinuationCache.close() |
| 118 | // writeSuspendContinuationCache.close() |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 119 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 120 | return true |
| 121 | } |
| 122 | |
Sergey Mashkov | a26b790 | 2017-12-05 16:06:03 +0300 | [diff] [blame] | 123 | override fun cancel(cause: Throwable?): Boolean { |
Sergey Mashkov | eee3aaf | 2017-12-05 15:51:55 +0300 | [diff] [blame] | 124 | return close(cause ?: CancellationException("Channel has been cancelled")) |
| 125 | } |
| 126 | |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 127 | private fun flushImpl(minReadSize: Int, minWriteSize: Int) { |
Sergey Mashkov | 33d5d13 | 2017-11-10 10:28:57 +0300 | [diff] [blame] | 128 | joining?.delegatedTo?.flush() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 129 | |
Sergey Mashkov | 625c1fe | 2017-11-08 06:15:39 +0300 | [diff] [blame] | 130 | val avw: Int |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 131 | val avr: Int |
Sergey Mashkov | 8e9138c | 2017-11-09 21:18:05 +0300 | [diff] [blame] | 132 | |
Sergey Mashkov | 625c1fe | 2017-11-08 06:15:39 +0300 | [diff] [blame] | 133 | while (true) { |
| 134 | val s = state |
Sergey Mashkov | 33d5d13 | 2017-11-10 10:28:57 +0300 | [diff] [blame] | 135 | if (s === ReadWriteBufferState.Terminated) return |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 136 | s.capacity.flush() |
Sergey Mashkov | 625c1fe | 2017-11-08 06:15:39 +0300 | [diff] [blame] | 137 | if (s === state) { |
| 138 | avw = s.capacity.availableForWrite |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 139 | avr = s.capacity.availableForRead |
Sergey Mashkov | 625c1fe | 2017-11-08 06:15:39 +0300 | [diff] [blame] | 140 | break |
| 141 | } |
| 142 | } |
| 143 | |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 144 | if (avr >= minReadSize) resumeReadOp() |
Sergey Mashkov | ec2af2f | 2018-01-15 17:02:25 +0300 | [diff] [blame] | 145 | val joining = joining |
| 146 | if (avw >= minWriteSize && (joining == null || state === ReadWriteBufferState.Terminated)) resumeWriteOp() |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 147 | } |
Sergey Mashkov | 33d5d13 | 2017-11-10 10:28:57 +0300 | [diff] [blame] | 148 | |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 149 | override fun flush() { |
| 150 | flushImpl(1, 1) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private fun ByteBuffer.prepareBuffer(order: ByteOrder, position: Int, available: Int) { |
| 154 | require(position >= 0) |
| 155 | require(available >= 0) |
| 156 | |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 157 | val bufferLimit = capacity() - reservedSize |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 158 | val virtualLimit = position + available |
| 159 | |
Sergey Mashkov | f774f37 | 2017-10-09 12:09:16 +0300 | [diff] [blame] | 160 | order(order.nioOrder) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 161 | limit(virtualLimit.coerceAtMost(bufferLimit)) |
| 162 | position(position) |
| 163 | } |
| 164 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 165 | private fun setupStateForWrite(): ByteBuffer? { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 166 | writeOp?.let { existing -> |
| 167 | throw IllegalStateException("Write operation is already in progress: $existing") |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 168 | } |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 169 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 170 | var _allocated: ReadWriteBufferState.Initial? = null |
Sergey Mashkov | d7d9e2f | 2018-03-16 00:01:23 +0300 | [diff] [blame] | 171 | var old: ReadWriteBufferState? = null |
| 172 | |
| 173 | val newState = updateStateAndGet { state -> |
| 174 | old = state |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 175 | when { |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 176 | joining != null -> { |
| 177 | _allocated?.let { releaseBuffer(it) } |
| 178 | return null |
| 179 | } |
| 180 | closed != null -> { |
| 181 | _allocated?.let { releaseBuffer(it) } |
| 182 | throw closed!!.sendException |
| 183 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 184 | state === ReadWriteBufferState.IdleEmpty -> { |
| 185 | val allocated = _allocated ?: newBuffer().also { _allocated = it } |
| 186 | allocated.startWriting() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 187 | } |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 188 | state === ReadWriteBufferState.Terminated -> { |
| 189 | _allocated?.let { releaseBuffer(it) } |
| 190 | if (joining != null) return null |
| 191 | throw closed!!.sendException |
| 192 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 193 | else -> state.startWriting() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 194 | } |
| 195 | } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 196 | |
Sergey Mashkov | 18b28a2 | 2017-11-20 10:53:32 +0300 | [diff] [blame] | 197 | if (closed != null) { |
| 198 | restoreStateAfterWrite() |
| 199 | tryTerminate() |
| 200 | |
| 201 | throw closed!!.sendException |
| 202 | } |
| 203 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 204 | val buffer = newState.writeBuffer |
| 205 | |
| 206 | _allocated?.let { allocated -> |
| 207 | if (old !== ReadWriteBufferState.IdleEmpty) { |
| 208 | releaseBuffer(allocated) |
| 209 | } |
| 210 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 211 | return buffer.apply { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 212 | prepareBuffer(writeByteOrder, writePosition, newState.capacity.availableForWrite) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | private fun restoreStateAfterWrite() { |
| 217 | var toRelease: ReadWriteBufferState.IdleNonEmpty? = null |
| 218 | |
Sergey Mashkov | d7d9e2f | 2018-03-16 00:01:23 +0300 | [diff] [blame] | 219 | val newState = updateStateAndGet { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 220 | val writeStopped = it.stopWriting() |
| 221 | if (writeStopped is ReadWriteBufferState.IdleNonEmpty && writeStopped.capacity.isEmpty()) { |
| 222 | toRelease = writeStopped |
| 223 | ReadWriteBufferState.IdleEmpty |
| 224 | } else { |
| 225 | writeStopped |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (newState === ReadWriteBufferState.IdleEmpty) { |
| 230 | toRelease?.let { releaseBuffer(it.initial) } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private fun setupStateForRead(): ByteBuffer? { |
Sergey Mashkov | 39a8854 | 2017-12-19 18:41:34 +0300 | [diff] [blame] | 235 | val newState = updateStateAndGet { state -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 236 | when (state) { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 237 | ReadWriteBufferState.Terminated -> closed?.cause?.let { throw it } ?: return null |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 238 | ReadWriteBufferState.IdleEmpty -> closed?.cause?.let { throw it } ?: return null |
| 239 | else -> { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 240 | if (state.capacity.availableForRead == 0) return null |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 241 | state.startReading() |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return newState.readBuffer.apply { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 247 | prepareBuffer(readByteOrder, readPosition, newState.capacity.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | private fun restoreStateAfterRead() { |
| 252 | var toRelease: ReadWriteBufferState.IdleNonEmpty? = null |
| 253 | |
Sergey Mashkov | 39a8854 | 2017-12-19 18:41:34 +0300 | [diff] [blame] | 254 | val newState = updateStateAndGet { state -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 255 | toRelease?.let { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 256 | it.capacity.resetForWrite() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 257 | resumeWriteOp() |
| 258 | toRelease = null |
| 259 | } |
| 260 | |
| 261 | val readStopped = state.stopReading() |
| 262 | |
| 263 | if (readStopped is ReadWriteBufferState.IdleNonEmpty) { |
| 264 | if (this.state === state && readStopped.capacity.tryLockForRelease()) { |
| 265 | toRelease = readStopped |
| 266 | ReadWriteBufferState.IdleEmpty |
| 267 | } else { |
| 268 | readStopped |
| 269 | } |
| 270 | } else { |
| 271 | readStopped |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (newState === ReadWriteBufferState.IdleEmpty) { |
| 276 | toRelease?.let { releaseBuffer(it.initial) } |
| 277 | resumeWriteOp() |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 278 | } else { |
| 279 | if (newState is ReadWriteBufferState.IdleNonEmpty && newState.capacity.isEmpty()) { |
| 280 | if (newState.capacity.tryLockForRelease() && State.compareAndSet(this, newState, ReadWriteBufferState.IdleEmpty)) { |
| 281 | newState.capacity.resetForWrite() |
| 282 | releaseBuffer(newState.initial) |
| 283 | resumeWriteOp() |
| 284 | } |
| 285 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 289 | private fun setupDelegateTo(delegate: ByteBufferChannel, delegateClose: Boolean, delegateFlush: Boolean): JoiningState { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 290 | require(this !== delegate) |
| 291 | |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 292 | val joined = JoiningState(delegate, delegateClose, delegateFlush) |
Sergey Mashkov | 8af8937 | 2017-12-05 12:35:56 +0300 | [diff] [blame] | 293 | delegate.writeByteOrder = writeByteOrder |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 294 | this.joining = joined |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 295 | |
| 296 | val alreadyClosed = closed |
| 297 | if (alreadyClosed != null) { |
| 298 | if (alreadyClosed.cause != null) delegate.close(alreadyClosed.cause) |
Sergey Mashkov | 9e9929d | 2017-11-14 14:52:32 +0300 | [diff] [blame] | 299 | else if (delegateClose && state === ReadWriteBufferState.Terminated) delegate.close() |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 300 | else if (delegateFlush) delegate.flush() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 301 | } else { |
| 302 | flush() |
| 303 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 304 | |
| 305 | return joined |
| 306 | } |
| 307 | |
| 308 | private fun tryCompleteJoining(joined: JoiningState): Boolean { |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 309 | if (!tryReleaseBuffer(true)) return false |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 310 | ensureClosedJoined(joined) |
| 311 | |
Sergey Mashkov | 060f36a | 2017-11-23 12:39:27 +0300 | [diff] [blame] | 312 | resumeReadOp { IllegalStateException("Joining is in progress") } |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 313 | resumeWriteOp() // here we don't resume it with exception because it should resume and delegate writing |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 314 | |
| 315 | return true |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | private fun tryTerminate(): Boolean { |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 319 | if (closed == null) return false |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 320 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 321 | if (!tryReleaseBuffer(false)) return false |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 322 | |
| 323 | joining?.let { ensureClosedJoined(it) } |
| 324 | |
| 325 | resumeReadOp() |
| 326 | resumeWriteOp() |
| 327 | |
| 328 | return true |
| 329 | } |
| 330 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 331 | private fun tryReleaseBuffer(forceTermination: Boolean): Boolean { |
Sergey Mashkov | 24b040f | 2017-08-25 11:06:59 +0300 | [diff] [blame] | 332 | var toRelease: ReadWriteBufferState.Initial? = null |
| 333 | |
Sergey Mashkov | d7d9e2f | 2018-03-16 00:01:23 +0300 | [diff] [blame] | 334 | updateStateAndGet { state -> |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 335 | toRelease?.let { buffer -> |
| 336 | toRelease = null |
| 337 | buffer.capacity.resetForWrite() |
| 338 | resumeWriteOp() |
| 339 | } |
| 340 | val closed = closed |
| 341 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 342 | when { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 343 | state === ReadWriteBufferState.Terminated -> return true |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 344 | state === ReadWriteBufferState.IdleEmpty -> ReadWriteBufferState.Terminated |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 345 | closed != null && state is ReadWriteBufferState.IdleNonEmpty && (state.capacity.tryLockForRelease() || closed.cause != null) -> { |
| 346 | if (closed.cause != null) state.capacity.forceLockForRelease() |
Sergey Mashkov | 24b040f | 2017-08-25 11:06:59 +0300 | [diff] [blame] | 347 | toRelease = state.initial |
| 348 | ReadWriteBufferState.Terminated |
| 349 | } |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 350 | forceTermination && state is ReadWriteBufferState.IdleNonEmpty && state.capacity.tryLockForRelease() -> { |
| 351 | toRelease = state.initial |
| 352 | ReadWriteBufferState.Terminated |
| 353 | } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 354 | else -> return false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 355 | } |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 356 | } |
Sergey Mashkov | 24b040f | 2017-08-25 11:06:59 +0300 | [diff] [blame] | 357 | |
| 358 | toRelease?.let { buffer -> |
| 359 | if (state === ReadWriteBufferState.Terminated) { |
| 360 | releaseBuffer(buffer) |
| 361 | } |
| 362 | } |
| 363 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 364 | return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 365 | } |
| 366 | |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 367 | private fun ByteBuffer.carryIndex(idx: Int) = if (idx >= capacity() - reservedSize) idx - (capacity() - reservedSize) else idx |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 368 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 369 | private inline fun writing(block: ByteBufferChannel.(ByteBuffer, RingBufferCapacity) -> Unit) { |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 370 | val current = joining?.let { resolveDelegation(this, it) } ?: this |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 371 | val buffer = current.setupStateForWrite() ?: return |
| 372 | val capacity = current.state.capacity |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 373 | val before = current.totalBytesWritten |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 374 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 375 | try { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 376 | current.closed?.let { throw it.sendException } |
| 377 | block(current, buffer, capacity) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 378 | } finally { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 379 | if (capacity.isFull() || current.autoFlush) current.flush() |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 380 | if (current !== this) { |
| 381 | totalBytesWritten += current.totalBytesWritten - before |
| 382 | } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 383 | current.restoreStateAfterWrite() |
| 384 | current.tryTerminate() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | private inline fun reading(block: ByteBuffer.(RingBufferCapacity) -> Boolean): Boolean { |
| 389 | val buffer = setupStateForRead() ?: return false |
| 390 | val capacity = state.capacity |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 391 | try { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 392 | if (capacity.availableForRead == 0) return false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 393 | |
| 394 | return block(buffer, capacity) |
| 395 | } finally { |
| 396 | restoreStateAfterRead() |
| 397 | tryTerminate() |
| 398 | } |
| 399 | } |
| 400 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 401 | private fun readAsMuchAsPossible(dst: ByteBuffer): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 402 | var consumed = 0 |
| 403 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 404 | reading { state -> |
| 405 | val buffer = this |
| 406 | val bufferLimit = buffer.capacity() - reservedSize |
Sergey Mashkov | 0f7e922 | 2017-10-10 13:00:38 +0300 | [diff] [blame] | 407 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 408 | while (true) { |
| 409 | val dstRemaining = dst.remaining() |
| 410 | if (dstRemaining == 0) break |
| 411 | |
| 412 | val position = readPosition |
| 413 | val bufferRemaining = bufferLimit - position |
| 414 | |
| 415 | val part = state.tryReadAtMost(minOf(bufferRemaining, dstRemaining)) |
| 416 | if (part == 0) break |
| 417 | |
| 418 | buffer.limit(position + part) |
| 419 | buffer.position(position) |
| 420 | dst.put(buffer) |
| 421 | |
| 422 | bytesRead(state, part) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 423 | consumed += part |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 424 | } |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 425 | |
| 426 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 427 | } |
| 428 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 429 | return consumed |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 430 | } |
| 431 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 432 | private tailrec fun readAsMuchAsPossible(dst: BufferView, consumed0: Int = 0): Int { |
| 433 | var consumed = 0 |
| 434 | |
| 435 | val rc = reading { |
| 436 | val dstSize = dst.writeRemaining |
| 437 | val part = it.tryReadAtMost(minOf(remaining(), dstSize)) |
| 438 | if (part > 0) { |
| 439 | consumed += part |
| 440 | |
| 441 | if (dstSize < remaining()) { |
| 442 | limit(position() + dstSize) |
| 443 | } |
| 444 | dst.write(this) |
| 445 | |
| 446 | bytesRead(it, part) |
| 447 | true |
| 448 | } else { |
| 449 | false |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return if (rc && dst.canWrite() && state.capacity.availableForRead > 0) |
| 454 | readAsMuchAsPossible(dst, consumed0 + consumed) |
| 455 | else consumed + consumed0 |
| 456 | } |
| 457 | |
| 458 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 459 | private fun readAsMuchAsPossible(dst: ByteArray, offset: Int, length: Int): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 460 | var consumed = 0 |
| 461 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 462 | reading { state -> |
| 463 | val buffer = this |
| 464 | val bufferLimit = buffer.capacity() - reservedSize |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 465 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 466 | while (true) { |
| 467 | val lengthRemaining = length - consumed |
| 468 | if (lengthRemaining == 0) break |
| 469 | val position = readPosition |
| 470 | val bufferRemaining = bufferLimit - position |
| 471 | |
| 472 | val part = state.tryReadAtMost(minOf(bufferRemaining, lengthRemaining)) |
| 473 | if (part == 0) break |
| 474 | |
| 475 | buffer.limit(position + part) |
| 476 | buffer.position(position) |
| 477 | buffer.get(dst, offset + consumed, part) |
| 478 | |
| 479 | bytesRead(state, part) |
| 480 | consumed += part |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 481 | } |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 482 | |
| 483 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 484 | } |
| 485 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 486 | return consumed |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 487 | } |
| 488 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 489 | final override suspend fun readFully(dst: ByteArray, offset: Int, length: Int) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 490 | val consumed = readAsMuchAsPossible(dst, offset, length) |
| 491 | |
| 492 | if (consumed < length) { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 493 | return readFullySuspend(dst, offset + consumed, length - consumed) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 497 | final override suspend fun readFully(dst: ByteBuffer): Int { |
Sergey Mashkov | 6ca9a27 | 2017-07-25 20:10:27 +0300 | [diff] [blame] | 498 | val rc = readAsMuchAsPossible(dst) |
| 499 | if (!dst.hasRemaining()) return rc |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 500 | |
Sergey Mashkov | 6ca9a27 | 2017-07-25 20:10:27 +0300 | [diff] [blame] | 501 | return readFullySuspend(dst, rc) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 502 | } |
| 503 | |
Sergey Mashkov | 6ca9a27 | 2017-07-25 20:10:27 +0300 | [diff] [blame] | 504 | private suspend fun readFullySuspend(dst: ByteBuffer, rc0: Int): Int { |
| 505 | var copied = rc0 |
| 506 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 507 | while (dst.hasRemaining()) { |
| 508 | if (!readSuspend(1)) throw ClosedReceiveChannelException("Unexpected EOF: expected ${dst.remaining()} more bytes") |
Sergey Mashkov | 6ca9a27 | 2017-07-25 20:10:27 +0300 | [diff] [blame] | 509 | copied += readAsMuchAsPossible(dst) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 510 | } |
Sergey Mashkov | 6ca9a27 | 2017-07-25 20:10:27 +0300 | [diff] [blame] | 511 | |
| 512 | return copied |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 513 | } |
| 514 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 515 | private tailrec suspend fun readFullySuspend(dst: ByteArray, offset: Int, length: Int) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 516 | if (!readSuspend(1)) throw ClosedReceiveChannelException("Unexpected EOF: expected $length more bytes") |
| 517 | |
| 518 | val consumed = readAsMuchAsPossible(dst, offset, length) |
| 519 | |
| 520 | if (consumed < length) { |
| 521 | readFullySuspend(dst, offset + consumed, length - consumed) |
| 522 | } |
| 523 | } |
| 524 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 525 | suspend override fun readAvailable(dst: ByteArray, offset: Int, length: Int): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 526 | val consumed = readAsMuchAsPossible(dst, offset, length) |
| 527 | |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 528 | if (consumed == 0 && closed != null) { |
Sergey Mashkov | 4eccfba | 2017-10-10 13:54:47 +0300 | [diff] [blame] | 529 | if (state.capacity.flush()) { |
| 530 | return readAsMuchAsPossible(dst, offset, length) |
| 531 | } else { |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 532 | return -1 |
Sergey Mashkov | e4d3ce7 | 2017-09-11 10:31:08 +0300 | [diff] [blame] | 533 | } |
Sergey Mashkov | b87d636 | 2017-08-23 11:58:06 +0300 | [diff] [blame] | 534 | } |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 535 | else if (consumed > 0 || length == 0) return consumed |
| 536 | |
| 537 | return readAvailableSuspend(dst, offset, length) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 538 | } |
| 539 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 540 | suspend override fun readAvailable(dst: ByteBuffer): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 541 | val consumed = readAsMuchAsPossible(dst) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 542 | |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 543 | if (consumed == 0 && closed != null) { |
Sergey Mashkov | 4eccfba | 2017-10-10 13:54:47 +0300 | [diff] [blame] | 544 | if (state.capacity.flush()) { |
| 545 | return readAsMuchAsPossible(dst) |
| 546 | } else { |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 547 | return -1 |
Sergey Mashkov | e4d3ce7 | 2017-09-11 10:31:08 +0300 | [diff] [blame] | 548 | } |
Sergey Mashkov | b87d636 | 2017-08-23 11:58:06 +0300 | [diff] [blame] | 549 | } |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 550 | else if (consumed > 0 || !dst.hasRemaining()) return consumed |
| 551 | |
| 552 | return readAvailableSuspend(dst) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 555 | suspend override fun readAvailable(dst: BufferView): Int { |
| 556 | val consumed = readAsMuchAsPossible(dst) |
| 557 | |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 558 | if (consumed == 0 && closed != null) { |
Sergey Mashkov | 4eccfba | 2017-10-10 13:54:47 +0300 | [diff] [blame] | 559 | if (state.capacity.flush()) { |
| 560 | return readAsMuchAsPossible(dst) |
| 561 | } else { |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 562 | return -1 |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 563 | } |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 564 | } |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 565 | else if (consumed > 0 || !dst.canWrite()) return consumed |
| 566 | |
| 567 | return readAvailableSuspend(dst) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 568 | } |
| 569 | |
Sergey Mashkov | b87d636 | 2017-08-23 11:58:06 +0300 | [diff] [blame] | 570 | private suspend fun readAvailableSuspend(dst: ByteArray, offset: Int, length: Int): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 571 | if (!readSuspend(1)) return -1 |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 572 | return readAvailable(dst, offset, length) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 573 | } |
| 574 | |
Sergey Mashkov | b87d636 | 2017-08-23 11:58:06 +0300 | [diff] [blame] | 575 | private suspend fun readAvailableSuspend(dst: ByteBuffer): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 576 | if (!readSuspend(1)) return -1 |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 577 | return readAvailable(dst) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 578 | } |
| 579 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 580 | private suspend fun readAvailableSuspend(dst: BufferView): Int { |
| 581 | if (!readSuspend(1)) return -1 |
| 582 | return readAvailable(dst) |
| 583 | } |
| 584 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 585 | final override suspend fun readPacket(size: Int, headerSizeHint: Int): ByteReadPacket { |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 586 | closed?.cause?.let { throw it } |
| 587 | |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 588 | if (size == 0) return ByteReadPacket.Empty |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 589 | |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 590 | val builder = BytePacketBuilder(headerSizeHint) |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 591 | val buffer = BufferPool.borrow() |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 592 | var remaining = size |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 593 | |
| 594 | try { |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 595 | while (remaining > 0) { |
| 596 | buffer.clear() |
| 597 | if (buffer.remaining() > remaining) { |
| 598 | buffer.limit(remaining) |
| 599 | } |
| 600 | |
| 601 | val rc = readAsMuchAsPossible(buffer) |
| 602 | if (rc == 0) break |
| 603 | |
| 604 | buffer.flip() |
| 605 | builder.writeFully(buffer) |
| 606 | |
| 607 | remaining -= rc |
| 608 | } |
| 609 | } catch (t: Throwable) { |
| 610 | BufferPool.recycle(buffer) |
| 611 | builder.release() |
| 612 | throw t |
| 613 | } |
| 614 | |
| 615 | return if (remaining == 0) { |
| 616 | BufferPool.recycle(buffer) |
| 617 | builder.build() |
| 618 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 619 | return readPacketSuspend(remaining, builder, buffer) |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | private suspend fun readPacketSuspend(size: Int, builder: ByteWritePacketImpl, buffer: ByteBuffer): ByteReadPacket { |
| 624 | var remaining = size |
| 625 | |
| 626 | try { |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 627 | while (remaining > 0) { |
| 628 | buffer.clear() |
| 629 | if (buffer.remaining() > remaining) { |
| 630 | buffer.limit(remaining) |
| 631 | } |
| 632 | |
| 633 | val rc = readFully(buffer) |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 634 | |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 635 | buffer.flip() |
| 636 | builder.writeFully(buffer) |
| 637 | |
| 638 | remaining -= rc |
| 639 | } |
| 640 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 641 | |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 642 | return builder.build() |
| 643 | } catch (t: Throwable) { |
| 644 | builder.release() |
| 645 | throw t |
| 646 | } finally { |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 647 | BufferPool.recycle(buffer) |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 651 | final override suspend fun readByte(): Byte { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 652 | var b: Byte = 0 |
| 653 | |
| 654 | val rc = reading { |
| 655 | if (it.tryReadExact(1)) { |
| 656 | b = get() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 657 | bytesRead(it, 1) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 658 | true |
| 659 | } else false |
| 660 | } |
| 661 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 662 | if (rc) { |
| 663 | return b |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 664 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 665 | return readByteSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | private suspend fun readByteSuspend(): Byte { |
| 670 | if (!readSuspend(1)) throw ClosedReceiveChannelException("EOF: one byte required") |
| 671 | return readByte() |
| 672 | } |
| 673 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 674 | final override suspend fun readBoolean(): Boolean { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 675 | var b = false |
| 676 | |
| 677 | val rc = reading { |
| 678 | if (it.tryReadExact(1)) { |
| 679 | b = get() != 0.toByte() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 680 | bytesRead(it, 1) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 681 | true |
| 682 | } else false |
| 683 | } |
| 684 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 685 | if (rc) { |
| 686 | return b |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 687 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 688 | return readBooleanSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | private suspend fun readBooleanSuspend(): Boolean { |
| 693 | if (!readSuspend(1)) throw ClosedReceiveChannelException("EOF: one byte required") |
| 694 | return readBoolean() |
| 695 | } |
| 696 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 697 | final override suspend fun readShort(): Short { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 698 | var sh: Short = 0 |
| 699 | |
| 700 | val rc = reading { |
| 701 | if (it.tryReadExact(2)) { |
| 702 | if (remaining() < 2) rollBytes(2) |
| 703 | sh = getShort() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 704 | bytesRead(it, 2) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 705 | true |
| 706 | } else false |
| 707 | } |
| 708 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 709 | if (rc) { |
| 710 | return sh |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 711 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 712 | return readShortSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | private suspend fun readShortSuspend(): Short { |
| 717 | if (!readSuspend(2)) throw ClosedReceiveChannelException("EOF while byte expected") |
| 718 | return readShort() |
| 719 | } |
| 720 | |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 721 | final override suspend fun readInt(): Int { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 722 | var i = 0 |
| 723 | |
| 724 | val rc = reading { |
| 725 | if (it.tryReadExact(4)) { |
| 726 | if (remaining() < 4) rollBytes(4) |
| 727 | i = getInt() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 728 | bytesRead(it, 4) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 729 | true |
| 730 | } else false |
| 731 | } |
| 732 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 733 | if (rc) { |
| 734 | return i |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 735 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 736 | return readIntSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 740 | private suspend fun readIntSuspend(): Int { |
| 741 | if (!readSuspend(4)) throw ClosedReceiveChannelException("EOF while an int expected") |
| 742 | return readInt() |
| 743 | } |
| 744 | |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 745 | final override suspend fun readLong(): Long { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 746 | var i = 0L |
| 747 | |
| 748 | val rc = reading { |
| 749 | if (it.tryReadExact(8)) { |
| 750 | if (remaining() < 8) rollBytes(8) |
| 751 | i = getLong() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 752 | bytesRead(it, 8) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 753 | true |
| 754 | } else false |
| 755 | } |
| 756 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 757 | if (rc) { |
| 758 | return i |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 759 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 760 | return readLongSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | private suspend fun readLongSuspend(): Long { |
| 765 | if (!readSuspend(8)) throw ClosedReceiveChannelException("EOF while a long expected") |
| 766 | return readLong() |
| 767 | } |
| 768 | |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 769 | final override suspend fun readDouble(): Double { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 770 | var d = 0.0 |
| 771 | |
| 772 | val rc = reading { |
| 773 | if (it.tryReadExact(8)) { |
| 774 | if (remaining() < 8) rollBytes(8) |
| 775 | d = getDouble() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 776 | bytesRead(it, 8) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 777 | true |
| 778 | } else false |
| 779 | } |
| 780 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 781 | if (rc) { |
| 782 | return d |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 783 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 784 | return readDoubleSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | private suspend fun readDoubleSuspend(): Double { |
| 789 | if (!readSuspend(8)) throw ClosedReceiveChannelException("EOF while a double expected") |
| 790 | return readDouble() |
| 791 | } |
| 792 | |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 793 | final override suspend fun readFloat(): Float { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 794 | var f = 0.0f |
| 795 | |
| 796 | val rc = reading { |
| 797 | if (it.tryReadExact(4)) { |
| 798 | if (remaining() < 4) rollBytes(4) |
| 799 | f = getFloat() |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 800 | bytesRead(it, 4) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 801 | true |
| 802 | } else false |
| 803 | } |
| 804 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 805 | if (rc) { |
| 806 | return f |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 807 | } else { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 808 | return readFloatSuspend() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | private suspend fun readFloatSuspend(): Float { |
| 813 | if (!readSuspend(4)) throw ClosedReceiveChannelException("EOF while an int expected") |
| 814 | return readFloat() |
| 815 | } |
| 816 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 817 | private fun ByteBuffer.rollBytes(n: Int) { |
Sergey Mashkov | a432a82 | 2018-05-21 12:50:14 +0300 | [diff] [blame] | 818 | val rem = remaining() |
| 819 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 820 | limit(position() + n) |
Sergey Mashkov | a432a82 | 2018-05-21 12:50:14 +0300 | [diff] [blame] | 821 | for (i in 0 until n - rem) { |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 822 | put(capacity() + ReservedLongIndex + i, get(i)) |
| 823 | } |
| 824 | } |
| 825 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 826 | private fun ByteBuffer.carry() { |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 827 | val base = capacity() - reservedSize |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 828 | for (i in base until position()) { |
| 829 | put(i - base, get(i)) |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | private fun ByteBuffer.bytesWritten(c: RingBufferCapacity, n: Int) { |
| 834 | require(n >= 0) |
| 835 | |
| 836 | writePosition = carryIndex(writePosition + n) |
| 837 | c.completeWrite(n) |
Sergey Mashkov | aae062d | 2017-09-07 15:12:50 +0300 | [diff] [blame] | 838 | totalBytesWritten += n |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 839 | } |
| 840 | |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 841 | private fun ByteBuffer.bytesRead(c: RingBufferCapacity, n: Int) { |
| 842 | require(n >= 0) |
| 843 | |
| 844 | readPosition = carryIndex(readPosition + n) |
| 845 | c.completeRead(n) |
Sergey Mashkov | aae062d | 2017-09-07 15:12:50 +0300 | [diff] [blame] | 846 | totalBytesRead += n |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 847 | resumeWriteOp() |
| 848 | } |
| 849 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 850 | private tailrec fun resolveDelegation(current: ByteBufferChannel, joining: JoiningState): ByteBufferChannel? { |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 851 | if (current.state === ReadWriteBufferState.Terminated) { |
| 852 | val joinedTo = joining.delegatedTo |
| 853 | val nextJoining = joinedTo.joining ?: return joinedTo |
| 854 | return resolveDelegation(joinedTo, nextJoining) |
| 855 | } |
| 856 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 857 | return null |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 858 | } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 859 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 860 | private suspend fun delegateByte(b: Byte) { |
| 861 | val joined = joining!! |
| 862 | if (state === ReadWriteBufferState.Terminated) return joined.delegatedTo.writeByte(b) |
| 863 | return delegateSuspend(joined) { writeByte(b) } |
| 864 | } |
| 865 | |
| 866 | private suspend fun delegateSuspend(joined: JoiningState, block: suspend ByteBufferChannel.() -> Unit) { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 867 | while (true) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 868 | if (state === ReadWriteBufferState.Terminated) return block(joined.delegatedTo) |
| 869 | writeSuspend(1) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 873 | suspend override fun writeByte(b: Byte) { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 874 | joining?.let { resolveDelegation(this, it)?.let { return it.writeByte(b) } } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 875 | |
| 876 | val buffer = setupStateForWrite() ?: return delegateByte(b) |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 877 | val c = state.capacity |
| 878 | |
| 879 | return tryWriteByte(buffer, b, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 880 | } |
| 881 | |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 882 | private suspend fun tryWriteByte(buffer: ByteBuffer, b: Byte, c: RingBufferCapacity) { |
| 883 | if (!c.tryWriteExact(1)) { |
| 884 | return writeByteSuspend(buffer, b, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 885 | } |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 886 | |
| 887 | doWrite(buffer, b, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 888 | } |
| 889 | |
Sergey Mashkov | fde1a64 | 2017-10-10 14:23:34 +0300 | [diff] [blame] | 890 | private fun doWrite(buffer: ByteBuffer, b: Byte, c: RingBufferCapacity) { |
| 891 | buffer.put(b) |
| 892 | buffer.bytesWritten(c, 1) |
| 893 | if (c.isFull() || autoFlush) flush() |
| 894 | restoreStateAfterWrite() |
| 895 | } |
| 896 | |
| 897 | private suspend fun writeByteSuspend(buffer: ByteBuffer, b: Byte, c: RingBufferCapacity) { |
| 898 | try { |
| 899 | writeSuspend(1) |
| 900 | } catch (t: Throwable) { |
| 901 | restoreStateAfterWrite() |
| 902 | tryTerminate() |
| 903 | throw t |
| 904 | } |
| 905 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 906 | if (joining != null) { |
| 907 | restoreStateAfterWrite() |
| 908 | return delegateByte(b) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 909 | } |
| 910 | |
Sergey Mashkov | bab9f79 | 2017-11-07 19:42:15 +0300 | [diff] [blame] | 911 | buffer.prepareBuffer(writeByteOrder, writePosition, c.availableForWrite) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 912 | return tryWriteByte(buffer, b, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 913 | } |
| 914 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 915 | private suspend fun delegateShort(s: Short) { |
| 916 | val joined = joining!! |
| 917 | if (state === ReadWriteBufferState.Terminated) return joined.delegatedTo.writeShort(s) |
| 918 | return delegateSuspend(joined) { writeShort(s) } |
| 919 | } |
| 920 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 921 | suspend override fun writeShort(s: Short) { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 922 | joining?.let { resolveDelegation(this, it)?.let { return it.writeShort(s) } } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 923 | |
| 924 | val buffer = setupStateForWrite() ?: return delegateShort(s) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 925 | val c = state.capacity |
| 926 | |
| 927 | return tryWriteShort(buffer, s, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 928 | } |
| 929 | |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 930 | private fun doWrite(buffer: ByteBuffer, s: Short, c: RingBufferCapacity) { |
| 931 | buffer.apply { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 932 | if (remaining() < 2) { |
| 933 | limit(capacity()) |
| 934 | putShort(s) |
| 935 | carry() |
| 936 | } else { |
| 937 | putShort(s) |
| 938 | } |
| 939 | |
| 940 | bytesWritten(c, 2) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 941 | } |
| 942 | |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 943 | if (c.isFull() || autoFlush) flush() |
| 944 | restoreStateAfterWrite() |
| 945 | } |
| 946 | |
| 947 | private suspend fun tryWriteShort(buffer: ByteBuffer, s: Short, c: RingBufferCapacity) { |
| 948 | if (!c.tryWriteExact(2)) { |
| 949 | return writeShortSuspend(buffer, s, c) |
| 950 | } |
| 951 | |
| 952 | return doWrite(buffer, s, c) |
| 953 | } |
| 954 | |
| 955 | private suspend fun writeShortSuspend(buffer: ByteBuffer, s: Short, c: RingBufferCapacity) { |
| 956 | try { |
| 957 | writeSuspend(2) |
| 958 | } catch (t: Throwable) { |
| 959 | restoreStateAfterWrite() |
| 960 | tryTerminate() |
| 961 | throw t |
| 962 | } |
| 963 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 964 | if (joining != null) { |
| 965 | restoreStateAfterWrite() |
| 966 | return delegateShort(s) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 967 | } |
| 968 | |
Sergey Mashkov | bab9f79 | 2017-11-07 19:42:15 +0300 | [diff] [blame] | 969 | buffer.prepareBuffer(writeByteOrder, writePosition, c.availableForWrite) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 970 | return tryWriteShort(buffer, s, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 971 | } |
| 972 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 973 | private suspend fun delegateInt(i: Int) { |
| 974 | val joined = joining!! |
| 975 | if (state === ReadWriteBufferState.Terminated) return joined.delegatedTo.writeInt(i) |
| 976 | return delegateSuspend(joined) { writeInt(i) } |
| 977 | } |
| 978 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 979 | private fun ByteBuffer.tryWriteInt(i: Int, c: RingBufferCapacity): Boolean { |
| 980 | if (c.tryWriteExact(4)) { |
| 981 | if (remaining() < 4) { |
| 982 | limit(capacity()) |
| 983 | putInt(i) |
| 984 | carry() |
| 985 | } else { |
| 986 | putInt(i) |
| 987 | } |
| 988 | |
| 989 | bytesWritten(c, 4) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 990 | if (c.isFull() || autoFlush) flush() |
| 991 | restoreStateAfterWrite() |
| 992 | tryTerminate() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 993 | return true |
| 994 | } |
| 995 | |
| 996 | return false |
| 997 | } |
| 998 | |
| 999 | suspend override fun writeInt(i: Int) { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1000 | val buffer = setupStateForWrite() |
| 1001 | if (buffer == null) { |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1002 | val delegation = resolveDelegation(this, joining!!) |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1003 | @Suppress("SuspiciousEqualsCombination") |
| 1004 | if (delegation != null && delegation !== this) return delegation.writeInt(i) |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1005 | else return delegateSuspend(joining!!, { writeInt(i) }) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1006 | } |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1007 | val c = state.capacity |
| 1008 | // |
| 1009 | if (buffer.tryWriteInt(i, c)) { |
| 1010 | return |
| 1011 | } |
| 1012 | return buffer.writeIntSuspend(i, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1013 | } |
| 1014 | |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1015 | private tailrec suspend fun ByteBuffer.writeIntSuspend(i: Int, c: RingBufferCapacity) { |
| 1016 | try { |
| 1017 | writeSuspend(4) |
| 1018 | } catch (t: Throwable) { |
| 1019 | restoreStateAfterWrite() |
| 1020 | tryTerminate() |
| 1021 | throw t |
| 1022 | } |
| 1023 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1024 | if (joining != null) { |
| 1025 | restoreStateAfterWrite() |
| 1026 | return delegateInt(i) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1027 | } |
| 1028 | |
Sergey Mashkov | bab9f79 | 2017-11-07 19:42:15 +0300 | [diff] [blame] | 1029 | prepareBuffer(writeByteOrder, writePosition, c.availableForWrite) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1030 | if (!tryWriteInt(i, c)) { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1031 | return writeIntSuspend(i, c) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1032 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1033 | } |
| 1034 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1035 | private suspend fun delegateLong(l: Long) { |
| 1036 | val joined = joining!! |
| 1037 | if (state === ReadWriteBufferState.Terminated) return joined.delegatedTo.writeLong(l) |
| 1038 | return delegateSuspend(joined) { writeLong(l) } |
| 1039 | } |
| 1040 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1041 | private fun ByteBuffer.tryWriteLong(l: Long, c: RingBufferCapacity): Boolean { |
| 1042 | if (c.tryWriteExact(8)) { |
| 1043 | if (remaining() < 8) { |
| 1044 | limit(capacity()) |
| 1045 | putLong(l) |
| 1046 | carry() |
| 1047 | } else { |
| 1048 | putLong(l) |
| 1049 | } |
| 1050 | |
| 1051 | bytesWritten(c, 8) |
Sergey Mashkov | 8e9138c | 2017-11-09 21:18:05 +0300 | [diff] [blame] | 1052 | if (c.isFull() || autoFlush || joining != null) flush() |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1053 | restoreStateAfterWrite() |
| 1054 | tryTerminate() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1055 | return true |
| 1056 | } |
| 1057 | |
| 1058 | return false |
| 1059 | } |
| 1060 | |
| 1061 | suspend override fun writeLong(l: Long) { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1062 | joining?.let { resolveDelegation(this, it)?.let { return it.writeLong(l) } } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1063 | |
| 1064 | val buffer = setupStateForWrite() ?: return delegateLong(l) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1065 | val c = state.capacity |
| 1066 | |
| 1067 | if (!buffer.tryWriteLong(l, c)) { |
| 1068 | return buffer.writeLongSuspend(l, c) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1072 | private tailrec suspend fun ByteBuffer.writeLongSuspend(l: Long, c: RingBufferCapacity) { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1073 | try { |
| 1074 | writeSuspend(8) |
| 1075 | } catch (t: Throwable) { |
| 1076 | restoreStateAfterWrite() |
| 1077 | tryTerminate() |
| 1078 | throw t |
| 1079 | } |
| 1080 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1081 | if (joining != null) { |
| 1082 | restoreStateAfterWrite() |
| 1083 | return delegateLong(l) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1084 | } |
Sergey Mashkov | bab9f79 | 2017-11-07 19:42:15 +0300 | [diff] [blame] | 1085 | |
| 1086 | prepareBuffer(writeByteOrder, writePosition, c.availableForWrite) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1087 | if (!tryWriteLong(l, c)) { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1088 | return writeLongSuspend(l, c) |
Sergey Mashkov | d0cb0b1 | 2017-10-10 14:44:39 +0300 | [diff] [blame] | 1089 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | suspend override fun writeDouble(d: Double) { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1093 | return writeLong(java.lang.Double.doubleToRawLongBits(d)) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | suspend override fun writeFloat(f: Float) { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1097 | return writeInt(java.lang.Float.floatToRawIntBits(f)) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1098 | } |
| 1099 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 1100 | suspend override fun writeAvailable(src: ByteBuffer): Int { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1101 | joining?.let { resolveDelegation(this, it)?.let { return it.writeAvailable(src) } } |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1102 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1103 | val copied = writeAsMuchAsPossible(src) |
| 1104 | if (copied > 0) return copied |
| 1105 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1106 | joining?.let { resolveDelegation(this, it)?.let { return it.writeAvailableSuspend(src) } } |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1107 | return writeAvailableSuspend(src) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1108 | } |
| 1109 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1110 | suspend override fun writeAvailable(src: BufferView): Int { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1111 | joining?.let { resolveDelegation(this, it)?.let { return it.writeAvailable(src) } } |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1112 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1113 | val copied = writeAsMuchAsPossible(src) |
| 1114 | if (copied > 0) return copied |
| 1115 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1116 | joining?.let { resolveDelegation(this, it)?.let { return it.writeAvailableSuspend(src) } } |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1117 | return writeAvailableSuspend(src) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | private suspend fun writeAvailableSuspend(src: ByteBuffer): Int { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1121 | writeSuspend(1) // here we don't need to restoreStateAfterWrite as write copy loop doesn't hold state |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1122 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1123 | joining?.let { resolveDelegation(this, it)?.let { return it .writeAvailableSuspend(src) } } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1124 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1125 | return writeAvailable(src) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | private suspend fun writeAvailableSuspend(src: BufferView): Int { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1129 | writeSuspend(1) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1130 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1131 | joining?.let { resolveDelegation(this, it)?.let { return it.writeAvailableSuspend(src) } } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1132 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1133 | return writeAvailable(src) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | suspend override fun writeFully(src: ByteBuffer) { |
Sergey Mashkov | d42bdad | 2017-11-17 19:29:49 +0300 | [diff] [blame] | 1137 | joining?.let { resolveDelegation(this, it)?.let { return it.writeFully(src) } } |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1138 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1139 | writeAsMuchAsPossible(src) |
| 1140 | if (!src.hasRemaining()) return |
| 1141 | |
| 1142 | return writeFullySuspend(src) |
| 1143 | } |
| 1144 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1145 | suspend override fun writeFully(src: BufferView) { |
| 1146 | writeAsMuchAsPossible(src) |
| 1147 | if (!src.canRead()) return |
| 1148 | |
| 1149 | return writeFullySuspend(src) |
| 1150 | } |
| 1151 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1152 | private suspend fun writeFullySuspend(src: ByteBuffer) { |
| 1153 | while (src.hasRemaining()) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1154 | tryWriteSuspend(1) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1155 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1156 | joining?.let { resolveDelegation(this, it)?.let { return it.writeFully(src) } } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1157 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1158 | writeAsMuchAsPossible(src) |
| 1159 | } |
| 1160 | } |
| 1161 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1162 | private suspend fun writeFullySuspend(src: BufferView) { |
| 1163 | while (src.canRead()) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1164 | tryWriteSuspend(1) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1165 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1166 | joining?.let { resolveDelegation(this, it)?.let { return it.writeFully(src) } } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1167 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1168 | writeAsMuchAsPossible(src) |
| 1169 | } |
| 1170 | } |
| 1171 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1172 | private suspend fun awaitClose() { |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 1173 | if (closed != null) return |
Sergey Mashkov | 373d590 | 2017-11-10 12:22:23 +0300 | [diff] [blame] | 1174 | val joined = joining |
| 1175 | |
| 1176 | if (joined != null) { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1177 | return joined.awaitClose() |
Sergey Mashkov | 373d590 | 2017-11-10 12:22:23 +0300 | [diff] [blame] | 1178 | } else if (closed == null) { |
| 1179 | error("Only works for joined") |
| 1180 | } |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 1181 | } |
| 1182 | |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 1183 | internal suspend fun joinFrom(src: ByteBufferChannel, delegateClose: Boolean, delegateFlush: Boolean) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1184 | if (src.closed != null && src.state === ReadWriteBufferState.Terminated) { |
Sergey Mashkov | ca8762b | 2017-11-08 06:11:53 +0300 | [diff] [blame] | 1185 | if (delegateClose) close(src.closed!!.cause) |
| 1186 | return |
| 1187 | } |
Sergey Mashkov | 9e9929d | 2017-11-14 14:52:32 +0300 | [diff] [blame] | 1188 | closed?.let { closed -> |
| 1189 | if (src.closed == null) throw closed.sendException |
| 1190 | return |
| 1191 | } |
Sergey Mashkov | ca8762b | 2017-11-08 06:11:53 +0300 | [diff] [blame] | 1192 | |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 1193 | val joined = src.setupDelegateTo(this, delegateClose, delegateFlush) |
Sergey Mashkov | 9e9929d | 2017-11-14 14:52:32 +0300 | [diff] [blame] | 1194 | if (src.tryCompleteJoining(joined)) { |
| 1195 | return src.awaitClose() |
| 1196 | } |
| 1197 | |
| 1198 | return joinFromSuspend(src, delegateClose, joined) |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1199 | } |
| 1200 | |
Sergey Mashkov | 9e9929d | 2017-11-14 14:52:32 +0300 | [diff] [blame] | 1201 | private suspend fun joinFromSuspend(src: ByteBufferChannel, delegateClose: Boolean, joined: JoiningState) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1202 | copyDirect(src, Long.MAX_VALUE, joined) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1203 | |
| 1204 | if (delegateClose && src.isClosedForRead) { |
| 1205 | close() |
| 1206 | } else { |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 1207 | if (joined.delegateFlush) { |
| 1208 | flush() |
| 1209 | } |
Sergey Mashkov | d489d3f | 2017-11-08 07:15:01 +0300 | [diff] [blame] | 1210 | src.awaitClose() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1214 | internal suspend fun copyDirect(src: ByteBufferChannel, limit: Long, joined: JoiningState?): Long { |
| 1215 | if (limit == 0L) return 0L |
| 1216 | if (src.isClosedForRead) { |
| 1217 | if (joined != null) { |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1218 | check(src.tryCompleteJoining(joined)) |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1219 | } |
| 1220 | return 0L |
| 1221 | } |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1222 | if (joined != null && src.tryCompleteJoining(joined)) { |
| 1223 | return 0L |
| 1224 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1225 | |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1226 | val autoFlush = autoFlush |
| 1227 | val byteOrder = writeByteOrder |
| 1228 | |
| 1229 | try { |
| 1230 | var copied = 0L |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1231 | while (copied < limit) { |
| 1232 | writing { dstBuffer, state -> |
| 1233 | while (copied < limit) { |
| 1234 | var avWBefore = state.availableForWrite |
| 1235 | if (avWBefore == 0) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1236 | tryWriteSuspend(1) |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1237 | if (joining != null) break |
| 1238 | avWBefore = state.availableForWrite |
| 1239 | } |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1240 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1241 | dstBuffer.prepareBuffer(byteOrder, writePosition, avWBefore) |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1242 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1243 | var partSize = 0 |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1244 | |
Sergey Mashkov | ec2af2f | 2018-01-15 17:02:25 +0300 | [diff] [blame] | 1245 | src.reading { srcState -> |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1246 | val srcBuffer = this |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1247 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1248 | val rem = minOf(srcBuffer.remaining().toLong(), dstBuffer.remaining().toLong(), limit - copied).toInt() |
| 1249 | val n = state.tryWriteAtMost(rem) |
| 1250 | if (n > 0) { |
| 1251 | if (!srcState.tryReadExact(n)) throw AssertionError() |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1252 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1253 | srcBuffer.limit(srcBuffer.position() + n) |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1254 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1255 | dstBuffer.put(srcBuffer) |
| 1256 | partSize = n |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1257 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1258 | with(src) { |
| 1259 | srcBuffer.bytesRead(srcState, n) |
| 1260 | } |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1261 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1262 | |
| 1263 | true |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1264 | } |
| 1265 | |
Sergey Mashkov | ec2af2f | 2018-01-15 17:02:25 +0300 | [diff] [blame] | 1266 | if (partSize > 0) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1267 | dstBuffer.bytesWritten(state, partSize) |
| 1268 | copied += partSize |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1269 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1270 | if (avWBefore - partSize == 0 || autoFlush) { |
| 1271 | flush() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1272 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1273 | } else { |
| 1274 | break |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1275 | } |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1276 | } |
| 1277 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1278 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1279 | if (joined != null) { |
| 1280 | if (src.tryCompleteJoining(joined)) break |
| 1281 | else if (src.state.capacity.flush()) { // force flush src to read-up all the bytes |
| 1282 | src.resumeWriteOp() |
| 1283 | continue |
| 1284 | } |
| 1285 | } |
| 1286 | |
Sergey Mashkov | 373d590 | 2017-11-10 12:22:23 +0300 | [diff] [blame] | 1287 | if (copied >= limit) break |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1288 | |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1289 | flush() |
| 1290 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 1291 | if (src.availableForRead == 0) { |
| 1292 | if (src.readSuspendImpl(1)) { |
| 1293 | if (joined != null && src.tryCompleteJoining(joined)) break |
| 1294 | } else if (joined == null || src.tryCompleteJoining(joined)) break |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1295 | } |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1296 | |
| 1297 | if (joining != null) { |
Sergey Mashkov | ec2af2f | 2018-01-15 17:02:25 +0300 | [diff] [blame] | 1298 | tryWriteSuspend(1) |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1299 | } |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | if (autoFlush) { |
| 1303 | flush() |
| 1304 | } |
| 1305 | |
| 1306 | return copied |
| 1307 | } catch (t: Throwable) { |
| 1308 | close(t) |
| 1309 | throw t |
| 1310 | } |
| 1311 | } |
| 1312 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1313 | private fun ensureClosedJoined(joined: JoiningState) { |
| 1314 | val closed = closed ?: return |
| 1315 | this.joining = null |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1316 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1317 | if (joined.delegateClose) { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1318 | // writing state could be if we are inside of copyDirect loop |
| 1319 | // so in this case we shouldn't close channel |
| 1320 | // otherwise few bytes could be lost |
| 1321 | // it will be closed later in copyDirect's finalization |
| 1322 | // so we only do flush |
| 1323 | val writing = joined.delegatedTo.state.let { it is ReadWriteBufferState.Writing || it is ReadWriteBufferState.ReadingWriting } |
| 1324 | if (closed.cause != null || !writing) { |
| 1325 | joined.delegatedTo.close(closed.cause) |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 1326 | } else if (joined.delegateFlush) { |
Sergey Mashkov | 4d01f7e | 2018-01-12 18:46:22 +0300 | [diff] [blame] | 1327 | joined.delegatedTo.flush() |
| 1328 | } |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 1329 | } else if (joined.delegateFlush) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1330 | joined.delegatedTo.flush() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1331 | } |
| 1332 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 1333 | joined.complete() |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1334 | } |
| 1335 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1336 | private fun writeAsMuchAsPossible(src: ByteBuffer): Int { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1337 | writing { dst, state -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1338 | var written = 0 |
Sergey Mashkov | 0f7e922 | 2017-10-10 13:00:38 +0300 | [diff] [blame] | 1339 | val srcLimit = src.limit() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1340 | |
| 1341 | do { |
Sergey Mashkov | 0f7e922 | 2017-10-10 13:00:38 +0300 | [diff] [blame] | 1342 | val srcRemaining = srcLimit - src.position() |
| 1343 | if (srcRemaining == 0) break |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1344 | val possibleSize = state.tryWriteAtMost(minOf(srcRemaining, dst.remaining())) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1345 | if (possibleSize == 0) break |
| 1346 | require(possibleSize > 0) |
| 1347 | |
Sergey Mashkov | 0f7e922 | 2017-10-10 13:00:38 +0300 | [diff] [blame] | 1348 | src.limit(src.position() + possibleSize) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1349 | dst.put(src) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1350 | |
| 1351 | written += possibleSize |
| 1352 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1353 | dst.prepareBuffer(writeByteOrder, dst.carryIndex(writePosition + written), state.availableForWrite) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1354 | } while (true) |
| 1355 | |
Sergey Mashkov | 0f7e922 | 2017-10-10 13:00:38 +0300 | [diff] [blame] | 1356 | src.limit(srcLimit) |
| 1357 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1358 | dst.bytesWritten(state, written) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1359 | |
| 1360 | return written |
| 1361 | } |
| 1362 | |
| 1363 | return 0 |
| 1364 | } |
| 1365 | |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1366 | private fun writeAsMuchAsPossible(src: BufferView): Int { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1367 | writing { dst, state -> |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1368 | var written = 0 |
| 1369 | |
| 1370 | do { |
| 1371 | val srcSize = src.readRemaining |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1372 | val possibleSize = state.tryWriteAtMost(minOf(srcSize, dst.remaining())) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1373 | if (possibleSize == 0) break |
| 1374 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1375 | src.read(dst, possibleSize) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1376 | |
| 1377 | written += possibleSize |
| 1378 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1379 | dst.prepareBuffer(writeByteOrder, dst.carryIndex(writePosition + written), state.availableForWrite) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1380 | } while (true) |
| 1381 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1382 | dst.bytesWritten(state, written) |
Sergey Mashkov | c4694cc | 2017-10-09 12:51:34 +0300 | [diff] [blame] | 1383 | |
| 1384 | return written |
| 1385 | } |
| 1386 | |
| 1387 | return 0 |
| 1388 | } |
| 1389 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1390 | private fun writeAsMuchAsPossible(src: ByteArray, offset: Int, length: Int): Int { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1391 | writing { dst, state -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1392 | var written = 0 |
| 1393 | |
| 1394 | do { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1395 | val possibleSize = state.tryWriteAtMost(minOf(length - written, dst.remaining())) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1396 | if (possibleSize == 0) break |
| 1397 | require(possibleSize > 0) |
| 1398 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1399 | dst.put(src, offset + written, possibleSize) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1400 | written += possibleSize |
| 1401 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1402 | dst.prepareBuffer(writeByteOrder, dst.carryIndex(writePosition + written), state.availableForWrite) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1403 | } while (true) |
| 1404 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1405 | dst.bytesWritten(state, written) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1406 | |
| 1407 | return written |
| 1408 | } |
| 1409 | |
| 1410 | return 0 |
| 1411 | } |
| 1412 | |
| 1413 | suspend override fun writeFully(src: ByteArray, offset: Int, length: Int) { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1414 | joining?.let { resolveDelegation(this, it)?.let { return it .writeFully(src, offset, length) } } |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1415 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1416 | var rem = length |
| 1417 | var off = offset |
| 1418 | |
| 1419 | while (rem > 0) { |
| 1420 | val s = writeAsMuchAsPossible(src, off, rem) |
| 1421 | if (s == 0) break |
| 1422 | |
| 1423 | off += s |
| 1424 | rem -= s |
| 1425 | } |
| 1426 | |
| 1427 | if (rem == 0) return |
| 1428 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1429 | return writeFullySuspend(src, off, rem) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | private tailrec suspend fun writeFullySuspend(src: ByteArray, offset: Int, length: Int) { |
| 1433 | if (length == 0) return |
Sergey Mashkov | 7c99a99 | 2018-05-29 21:34:37 +0300 | [diff] [blame] | 1434 | writeSuspendUnit(src, offset, length) |
Sergey Mashkov | 9faa039 | 2018-06-13 19:49:45 +0300 | [diff] [blame] | 1435 | val copied = writeSuspendResult.takeIf { it != -1 } ?: |
| 1436 | joining?.let { resolveDelegation(this, it) }?.writeSuspendResult?.takeIf { it != -1 } ?: |
| 1437 | error("-1 should be only set in case of joining") |
| 1438 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1439 | return writeFullySuspend(src, offset + copied, length - copied) |
| 1440 | } |
| 1441 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 1442 | suspend override fun writeAvailable(src: ByteArray, offset: Int, length: Int): Int { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1443 | joining?.let { resolveDelegation(this, it)?.let { return it.writeAvailable(src, offset, length) } } |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1444 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1445 | val size = writeAsMuchAsPossible(src, offset, length) |
| 1446 | if (size > 0) return size |
| 1447 | return writeSuspend(src, offset, length) |
| 1448 | } |
| 1449 | |
Sergey Mashkov | 7c99a99 | 2018-05-29 21:34:37 +0300 | [diff] [blame] | 1450 | private var writeSuspendResult: Int = 0 |
| 1451 | |
| 1452 | private suspend fun writeSuspendUnit(src: ByteArray, offset: Int, length: Int) { |
| 1453 | while (true) { |
| 1454 | tryWriteSuspend(1) |
| 1455 | |
Sergey Mashkov | 9faa039 | 2018-06-13 19:49:45 +0300 | [diff] [blame] | 1456 | joining?.let { |
| 1457 | resolveDelegation(this, it)?.let { |
| 1458 | writeSuspendResult = -1; |
| 1459 | return it.writeSuspendUnit(src, offset, length) |
| 1460 | } |
| 1461 | } |
Sergey Mashkov | 7c99a99 | 2018-05-29 21:34:37 +0300 | [diff] [blame] | 1462 | |
| 1463 | val size = writeAsMuchAsPossible(src, offset, length) |
| 1464 | if (size > 0) { |
| 1465 | writeSuspendResult = size |
| 1466 | return |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1471 | private suspend fun writeSuspend(src: ByteArray, offset: Int, length: Int): Int { |
| 1472 | while (true) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1473 | tryWriteSuspend(1) |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1474 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1475 | joining?.let { resolveDelegation(this, it)?.let { return it.writeSuspend(src, offset, length) } } |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1476 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1477 | val size = writeAsMuchAsPossible(src, offset, length) |
| 1478 | if (size > 0) return size |
| 1479 | } |
| 1480 | } |
| 1481 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1482 | override suspend fun write(min: Int, block: (ByteBuffer) -> Unit) { |
| 1483 | require(min > 0) { "min should be positive"} |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1484 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1485 | var written = false |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1486 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1487 | writing { dst, state -> |
Sergey Mashkov | 7bf4fa9 | 2017-11-17 18:39:03 +0300 | [diff] [blame] | 1488 | val locked = state.tryWriteAtLeast(min) |
| 1489 | |
| 1490 | if (locked > 0) { |
| 1491 | // here we have locked all remaining for write bytes |
| 1492 | // however we don't know how many bytes will be actually written |
| 1493 | // so later we have to return (locked - actuallyWritten) bytes back |
| 1494 | |
| 1495 | // it is important to lock bytes to fail concurrent tryLockForRelease |
| 1496 | // once we have locked some bytes, tryLockForRelease will fail so it is safe to use buffer |
| 1497 | |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1498 | val position = dst.position() |
| 1499 | val l = dst.limit() |
| 1500 | block(dst) |
| 1501 | if (l != dst.limit()) throw IllegalStateException("buffer limit modified") |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1502 | |
Sergey Mashkov | 7bf4fa9 | 2017-11-17 18:39:03 +0300 | [diff] [blame] | 1503 | val actuallyWritten = dst.position() - position |
| 1504 | if (actuallyWritten < 0) throw IllegalStateException("position has been moved backward: pushback is not supported") |
| 1505 | |
| 1506 | dst.bytesWritten(state, actuallyWritten) |
| 1507 | |
| 1508 | if (actuallyWritten < locked) { |
| 1509 | state.completeRead(locked - actuallyWritten) // return back extra bytes (see note above) |
| 1510 | // we use completeRead in spite of that it is write block |
| 1511 | // we don't need to resume write as we are already in writing block |
| 1512 | } |
| 1513 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1514 | written = true |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1515 | } |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1516 | } |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1517 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1518 | if (!written) { |
| 1519 | return writeBlockSuspend(min, block) |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | private suspend fun writeBlockSuspend(min: Int, block: (ByteBuffer) -> Unit) { |
| 1524 | writeSuspend(min) |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1525 | joining?.let { resolveDelegation(this, it)?.let { return it.write(min, block) } } |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1526 | return write(min, block) |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1527 | } |
| 1528 | |
Sergey Mashkov | dfae5e2 | 2017-12-18 19:31:27 +0300 | [diff] [blame] | 1529 | override suspend fun writeWhile(block: (ByteBuffer) -> Boolean) { |
| 1530 | if (!writeWhileNoSuspend(block)) return |
| 1531 | closed?.let { throw it.sendException } |
| 1532 | return writeWhileSuspend(block) |
| 1533 | } |
| 1534 | |
| 1535 | private fun writeWhileNoSuspend(block: (ByteBuffer) -> Boolean): Boolean { |
| 1536 | var continueWriting = true |
| 1537 | |
| 1538 | writing { dst, capacity -> |
| 1539 | continueWriting = writeWhileLoop(dst, capacity, block) |
| 1540 | } |
| 1541 | |
| 1542 | return continueWriting |
| 1543 | } |
| 1544 | |
| 1545 | private suspend fun writeWhileSuspend(block: (ByteBuffer) -> Boolean) { |
| 1546 | var continueWriting = true |
| 1547 | |
| 1548 | writing { dst, capacity -> |
| 1549 | while (true) { |
| 1550 | writeSuspend(1) |
| 1551 | if (joining != null) break |
| 1552 | if (!writeWhileLoop(dst, capacity, block)) { |
| 1553 | continueWriting = false |
| 1554 | break |
| 1555 | } |
| 1556 | if (closed != null) break |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | if (!continueWriting) return |
| 1561 | closed?.let { throw it.sendException } |
| 1562 | joining?.let { return writeWhile(block) } |
| 1563 | } |
| 1564 | |
| 1565 | // it should be writing state set to use this function |
| 1566 | private fun writeWhileLoop(dst: ByteBuffer, capacity: RingBufferCapacity, block: (ByteBuffer) -> Boolean): Boolean { |
| 1567 | var continueWriting = true |
| 1568 | val bufferLimit = dst.capacity() - reservedSize |
| 1569 | |
| 1570 | while (continueWriting) { |
| 1571 | val locked = capacity.tryWriteAtLeast(1) // see comments in [write] |
| 1572 | if (locked == 0) break |
| 1573 | |
| 1574 | val position = writePosition |
| 1575 | val l = (position + locked).coerceAtMost(bufferLimit) |
| 1576 | dst.limit(l) |
| 1577 | dst.position(position) |
| 1578 | |
| 1579 | continueWriting = try { |
| 1580 | block(dst) |
| 1581 | } catch (t: Throwable) { |
| 1582 | capacity.completeRead(locked) |
| 1583 | throw t |
| 1584 | } |
| 1585 | |
| 1586 | if (dst.limit() != l) throw IllegalStateException("buffer limit modified") |
| 1587 | val actuallyWritten = dst.position() - position |
| 1588 | if (actuallyWritten < 0) throw IllegalStateException("position has been moved backward: pushback is not supported") |
| 1589 | |
| 1590 | dst.bytesWritten(capacity, actuallyWritten) |
| 1591 | if (actuallyWritten < locked) { |
| 1592 | capacity.completeRead(locked - actuallyWritten) // return back extra bytes |
| 1593 | // it is important to use completeRead in spite of that we are writing here |
| 1594 | // no need to resume read here |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | return continueWriting |
| 1599 | } |
| 1600 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1601 | override suspend fun read(min: Int, block: (ByteBuffer) -> Unit) { |
Sergey Mashkov | e86eb08 | 2017-12-04 18:45:05 +0300 | [diff] [blame] | 1602 | require(min >= 0) { "min should be positive or zero" } |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1603 | |
| 1604 | val read = reading { |
Sergey Mashkov | e86eb08 | 2017-12-04 18:45:05 +0300 | [diff] [blame] | 1605 | val av = it.availableForRead |
| 1606 | if (av > 0 && av >= min) { |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1607 | val position = this.position() |
| 1608 | val l = this.limit() |
| 1609 | block(this) |
| 1610 | if (l != this.limit()) throw IllegalStateException("buffer limit modified") |
| 1611 | val delta = position() - position |
| 1612 | if (delta < 0) throw IllegalStateException("position has been moved backward: pushback is not supported") |
| 1613 | |
| 1614 | if (!it.tryReadExact(delta)) throw IllegalStateException() |
| 1615 | bytesRead(it, delta) |
| 1616 | true |
| 1617 | } |
| 1618 | else false |
| 1619 | } |
| 1620 | |
| 1621 | if (!read) { |
Sergey Mashkov | 350d144 | 2017-12-05 15:25:05 +0300 | [diff] [blame] | 1622 | if (isClosedForRead) return |
Sergey Mashkov | c143ea6 | 2017-10-10 15:21:35 +0300 | [diff] [blame] | 1623 | return readBlockSuspend(min, block) |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | |
Sergey Mashkov | 4df720f | 2017-12-05 16:45:55 +0300 | [diff] [blame] | 1627 | override suspend fun discard(max: Long): Long { |
| 1628 | require(max >= 0) { "max shouldn't be negative: $max" } |
| 1629 | |
| 1630 | var discarded = 0L |
| 1631 | |
| 1632 | reading { |
| 1633 | val n = it.tryReadAtMost(minOf(Int.MAX_VALUE.toLong(), max).toInt()) |
| 1634 | bytesRead(it, n) |
| 1635 | discarded += n |
| 1636 | true |
| 1637 | } |
| 1638 | |
| 1639 | if (discarded == max || isClosedForRead) return discarded |
| 1640 | |
| 1641 | return discardSuspend(discarded, max) |
| 1642 | } |
| 1643 | |
| 1644 | private suspend fun discardSuspend(discarded0: Long, max: Long): Long { |
| 1645 | var discarded = discarded0 |
| 1646 | |
| 1647 | while (discarded < max) { |
| 1648 | val rc = reading { |
| 1649 | val n = it.tryReadAtMost(minOf(Int.MAX_VALUE.toLong(), max - discarded).toInt()) |
| 1650 | bytesRead(it, n) |
| 1651 | discarded += n |
| 1652 | |
| 1653 | true |
| 1654 | } |
| 1655 | |
| 1656 | if (!rc) { |
| 1657 | if (isClosedForRead || !readSuspend(1)) break |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | return discarded |
| 1662 | } |
| 1663 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1664 | private suspend fun readBlockSuspend(min: Int, block: (ByteBuffer) -> Unit) { |
Sergey Mashkov | e86eb08 | 2017-12-04 18:45:05 +0300 | [diff] [blame] | 1665 | if (!readSuspend(min.coerceAtLeast(1))) { |
| 1666 | if (min > 0) |
| 1667 | throw EOFException("Got EOF but at least $min bytes were expected") |
| 1668 | else return |
| 1669 | } |
| 1670 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1671 | read(min, block) |
| 1672 | } |
| 1673 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 1674 | override suspend fun writePacket(packet: ByteReadPacket) { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1675 | joining?.let { resolveDelegation(this, it)?.let { return it.writePacket(packet) } } |
Sergey Mashkov | 324611b | 2017-11-10 19:00:58 +0300 | [diff] [blame] | 1676 | |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1677 | try { |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1678 | while (!packet.isEmpty) { |
| 1679 | if (tryWritePacketPart(packet) == 0) break |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1680 | } |
| 1681 | } catch (t: Throwable) { |
Sergey Mashkov | 7510117 | 2017-09-25 19:41:08 +0300 | [diff] [blame] | 1682 | packet.release() |
| 1683 | throw t |
| 1684 | } |
| 1685 | |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1686 | if (packet.remaining > 0) { |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1687 | joining?.let { resolveDelegation(this, it)?.let { return it.writePacket(packet) } } |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1688 | return writePacketSuspend(packet) |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1689 | } |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1690 | } |
| 1691 | |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1692 | private suspend fun writePacketSuspend(packet: ByteReadPacket) { |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1693 | try { |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1694 | while (!packet.isEmpty) { |
| 1695 | writeSuspend(1) |
Sergey Mashkov | 4fe8eec | 2017-11-17 13:33:56 +0300 | [diff] [blame] | 1696 | |
Sergey Mashkov | 2874d16 | 2017-11-17 13:56:46 +0300 | [diff] [blame] | 1697 | joining?.let { resolveDelegation(this, it)?.let { return it.writePacket(packet) } } |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1698 | tryWritePacketPart(packet) |
| 1699 | } |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1700 | } finally { |
Sergey Mashkov | 5606133 | 2017-08-25 12:37:53 +0300 | [diff] [blame] | 1701 | packet.release() |
Sergey Mashkov | a10b692 | 2017-08-03 13:41:54 +0300 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1705 | private fun tryWritePacketPart(packet: ByteReadPacket): Int { |
| 1706 | var copied = 0 |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1707 | writing { dst, state -> |
| 1708 | val size = state.tryWriteAtMost(minOf(packet.remaining, dst.remaining())) |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1709 | if (size > 0) { |
Sergey Mashkov | c51ecd3 | 2017-10-27 22:42:39 +0300 | [diff] [blame] | 1710 | dst.limit(dst.position() + size) |
| 1711 | packet.readFully(dst) |
| 1712 | dst.bytesWritten(state, size) |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 1713 | } |
| 1714 | copied = size |
| 1715 | } |
| 1716 | |
| 1717 | return copied |
| 1718 | } |
| 1719 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1720 | /** |
| 1721 | * Invokes [visitor] for every available batch until all bytes processed or visitor if visitor returns false. |
| 1722 | * Never invokes [visitor] with empty buffer unless [last] = true. Invokes visitor with last = true at most once |
| 1723 | * even if there are remaining bytes and visitor returned true. |
| 1724 | */ |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1725 | final override suspend fun consumeEachBufferRange(visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean) { |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1726 | if (consumeEachBufferRangeFast(false, visitor)) return |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1727 | return consumeEachBufferRangeSuspend(visitor) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1728 | } |
| 1729 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1730 | override fun <R> lookAhead(visitor: LookAheadSession.() -> R): R { |
| 1731 | if (state === ReadWriteBufferState.Terminated) { |
| 1732 | return visitor(TerminatedLookAhead) |
| 1733 | } |
| 1734 | |
| 1735 | var result: R? = null |
| 1736 | val rc = reading { |
| 1737 | result = visitor(this@ByteBufferChannel) |
| 1738 | true |
| 1739 | } |
| 1740 | |
| 1741 | if (!rc) { |
| 1742 | return visitor(TerminatedLookAhead) |
| 1743 | } |
| 1744 | |
| 1745 | return result!! |
| 1746 | } |
| 1747 | |
| 1748 | suspend override fun <R> lookAheadSuspend(visitor: suspend LookAheadSuspendSession.() -> R): R { |
| 1749 | if (state === ReadWriteBufferState.Terminated) { |
| 1750 | return visitor(TerminatedLookAhead) |
| 1751 | } |
| 1752 | |
| 1753 | var result: R? = null |
| 1754 | val rc = reading { |
| 1755 | result = visitor(this@ByteBufferChannel) |
| 1756 | true |
| 1757 | } |
| 1758 | |
| 1759 | if (!rc) { |
| 1760 | if (closed != null || state === ReadWriteBufferState.Terminated) return visitor(TerminatedLookAhead) |
| 1761 | result = visitor(this) |
Sergey Mashkov | 8a30b45 | 2017-09-07 13:15:31 +0300 | [diff] [blame] | 1762 | if (!state.idle && state !== ReadWriteBufferState.Terminated) { |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1763 | restoreStateAfterRead() |
| 1764 | tryTerminate() |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | return result!! |
| 1769 | } |
| 1770 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1771 | override suspend fun writeSuspendSession(visitor: suspend WriterSuspendSession.() -> Unit) { |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1772 | var locked = 0 |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1773 | |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1774 | var current = joining?.let { resolveDelegation(this, it) } ?: this |
| 1775 | var byteBuffer = current.setupStateForWrite() ?: return writeSuspendSession(visitor) |
| 1776 | var ringBufferCapacity = current.state.capacity |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1777 | |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1778 | val session = object : WriterSuspendSession { |
| 1779 | override fun request(min: Int): ByteBuffer? { |
| 1780 | locked += ringBufferCapacity.tryWriteAtLeast(0) |
| 1781 | if (locked < min) return null |
| 1782 | byteBuffer.prepareBuffer(writeByteOrder, writePosition, locked) |
| 1783 | if (byteBuffer.remaining() < min) return null |
| 1784 | if (current.joining != null) return null |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1785 | |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1786 | return byteBuffer |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1787 | } |
| 1788 | |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1789 | override fun written(n: Int) { |
| 1790 | require(n >= 0) |
| 1791 | if (n > locked) throw IllegalStateException() |
| 1792 | locked -= n |
| 1793 | byteBuffer.bytesWritten(ringBufferCapacity, n) |
| 1794 | } |
| 1795 | |
| 1796 | override suspend fun tryAwait(n: Int) { |
| 1797 | val joining = current.joining |
| 1798 | if (joining != null) { |
| 1799 | return tryAwaitJoinSwitch(n, joining) |
| 1800 | } |
| 1801 | |
| 1802 | if (locked >= n) return |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1803 | if (locked > 0) { |
| 1804 | ringBufferCapacity.completeRead(locked) |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 1805 | locked = 0 |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1806 | } |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1807 | |
| 1808 | return tryWriteSuspend(n) |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1809 | } |
Sergey Mashkov | e15eae8 | 2018-06-01 17:23:47 +0300 | [diff] [blame] | 1810 | |
| 1811 | private suspend fun tryAwaitJoinSwitch(n: Int, joining: JoiningState) { |
| 1812 | if (locked > 0) { |
| 1813 | ringBufferCapacity.completeRead(locked) |
| 1814 | locked = 0 |
| 1815 | } |
| 1816 | flush() |
| 1817 | restoreStateAfterWrite() |
| 1818 | tryTerminate() |
| 1819 | |
| 1820 | do { |
| 1821 | current.tryWriteSuspend(n) |
| 1822 | current = resolveDelegation(current, joining) ?: continue |
| 1823 | byteBuffer = current.setupStateForWrite() ?: continue |
| 1824 | ringBufferCapacity = current.state.capacity |
| 1825 | } while (false) |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | try { |
| 1830 | visitor(session) |
| 1831 | } finally { |
| 1832 | if (locked > 0) { |
| 1833 | ringBufferCapacity.completeRead(locked) |
| 1834 | locked = 0 |
| 1835 | } |
| 1836 | |
| 1837 | current.restoreStateAfterWrite() |
| 1838 | current.tryTerminate() |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1842 | override fun consumed(n: Int) { |
| 1843 | require(n >= 0) |
| 1844 | |
| 1845 | state.let { s -> |
| 1846 | if (!s.capacity.tryReadExact(n)) throw IllegalStateException("Unable to consume $n bytes: not enough available bytes") |
| 1847 | s.readBuffer.bytesRead(s.capacity, n) |
| 1848 | } |
| 1849 | } |
| 1850 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1851 | final override suspend fun awaitAtLeast(n: Int): Boolean { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1852 | if (state.capacity.availableForRead >= n) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1853 | if (state.idle || state is ReadWriteBufferState.Writing) setupStateForRead() |
| 1854 | return true |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1855 | } |
| 1856 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1857 | if (state.idle || state is ReadWriteBufferState.Writing) return awaitAtLeastSuspend(n) |
| 1858 | else if (n == 1) return readSuspendImpl(1) |
| 1859 | else return readSuspend(n) |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1860 | } |
| 1861 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1862 | private suspend fun awaitAtLeastSuspend(n: Int): Boolean { |
| 1863 | val rc = readSuspend(n) |
| 1864 | if (rc && state.idle) { |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1865 | setupStateForRead() |
| 1866 | } |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1867 | return rc |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | override fun request(skip: Int, atLeast: Int): ByteBuffer? { |
| 1871 | return state.let { s -> |
| 1872 | val available = s.capacity.availableForRead |
| 1873 | val rp = readPosition |
| 1874 | |
| 1875 | if (available < atLeast + skip) return null |
| 1876 | if (s.idle || (s !is ReadWriteBufferState.Reading && s !is ReadWriteBufferState.ReadingWriting)) return null |
| 1877 | |
| 1878 | val buffer = s.readBuffer |
| 1879 | |
| 1880 | val position = buffer.carryIndex(rp + skip) |
| 1881 | buffer.prepareBuffer(readByteOrder, position, available - skip) |
| 1882 | |
| 1883 | if (buffer.remaining() >= atLeast) buffer else null |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | private inline fun consumeEachBufferRangeFast(last: Boolean, visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean): Boolean { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1888 | val rc = reading { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1889 | do { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1890 | if (hasRemaining() || last) { |
| 1891 | val rc = visitor(this, last) |
| 1892 | afterBufferVisited(this, it) |
| 1893 | if (!rc || (last && !hasRemaining())) return true |
| 1894 | } else break |
| 1895 | } while (true) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1896 | |
| 1897 | last |
| 1898 | } |
| 1899 | |
| 1900 | if (!rc && closed != null) { |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 1901 | visitor(EmptyByteBuffer, true) |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1902 | return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | return rc |
| 1906 | } |
| 1907 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 1908 | // private suspend fun consumeEachBufferRangeSuspendLoop(visitor: RendezvousChannel<ConsumeEachBufferVisitor>) { |
| 1909 | // var last = false |
| 1910 | // |
| 1911 | // do { |
| 1912 | // if (consumeEachBufferRangeFast(last, visitor)) return |
| 1913 | // if (last) return |
| 1914 | // if (!readSuspend(1)) { |
| 1915 | // last = true |
| 1916 | // } |
| 1917 | // } while (true) |
| 1918 | // } |
| 1919 | |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1920 | private suspend fun consumeEachBufferRangeSuspend(visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1921 | var last = false |
| 1922 | |
| 1923 | do { |
Sergey Mashkov | f75ec15 | 2017-11-14 14:11:46 +0300 | [diff] [blame] | 1924 | if (consumeEachBufferRangeFast(last, visitor)) return |
| 1925 | if (last) return |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1926 | if (!readSuspend(1)) { |
| 1927 | last = true |
| 1928 | } |
| 1929 | } while (true) |
| 1930 | } |
| 1931 | |
| 1932 | private fun afterBufferVisited(buffer: ByteBuffer, c: RingBufferCapacity): Int { |
| 1933 | val consumed = buffer.position() - readPosition |
| 1934 | if (consumed > 0) { |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 1935 | if (!c.tryReadExact(consumed)) throw IllegalStateException("Consumed more bytes than available") |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1936 | |
Sergey Mashkov | 2cb1d5b | 2017-07-25 11:26:14 +0300 | [diff] [blame] | 1937 | buffer.bytesRead(c, consumed) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 1938 | buffer.prepareBuffer(readByteOrder, readPosition, c.availableForRead) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | return consumed |
| 1942 | } |
| 1943 | |
| 1944 | private suspend fun readUTF8LineToAscii(out: Appendable, limit: Int): Boolean { |
| 1945 | if (state === ReadWriteBufferState.Terminated) return false |
| 1946 | |
| 1947 | var cr = false |
| 1948 | var consumed = 0 |
| 1949 | var unicodeStarted = false |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 1950 | var eol = false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1951 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 1952 | consumeEachBufferRangeFast(false) { buffer, last -> |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 1953 | var forceConsume = false |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1954 | |
| 1955 | val rejected = !buffer.decodeASCII { ch -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1956 | when { |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1957 | ch == '\r' -> { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1958 | cr = true |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1959 | true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1960 | } |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1961 | ch == '\n' -> { |
| 1962 | eol = true |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 1963 | forceConsume = true |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1964 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1965 | } |
| 1966 | cr -> { |
| 1967 | cr = false |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1968 | eol = true |
| 1969 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1970 | } |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1971 | else -> { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1972 | if (consumed == limit) throw BufferOverflowException() |
| 1973 | consumed++ |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1974 | out.append(ch) |
| 1975 | true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 1980 | if (cr && last) { |
| 1981 | eol = true |
| 1982 | } |
| 1983 | |
| 1984 | if (eol && forceConsume) { |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 1985 | buffer.position(buffer.position() + 1) |
| 1986 | } |
| 1987 | |
| 1988 | if (rejected && buffer.hasRemaining() && !eol) { |
| 1989 | unicodeStarted = true |
| 1990 | false |
| 1991 | } else |
| 1992 | !eol && !last |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1993 | } |
| 1994 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 1995 | if (eol && !unicodeStarted) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1996 | return readUTF8LineToUtf8(out, limit - consumed, cr, consumed) |
| 1997 | } |
| 1998 | |
| 1999 | private suspend fun readUTF8LineToUtf8(out: Appendable, limit: Int, cr0: Boolean, consumed0: Int): Boolean { |
| 2000 | var cr1 = cr0 |
| 2001 | var consumed1 = 0 |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2002 | var eol = false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2003 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 2004 | consumeEachBufferRangeFast(false) { buffer, last -> |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2005 | var forceConsume = false |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2006 | |
| 2007 | val rc = buffer.decodeUTF8 { ch -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2008 | when { |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2009 | ch == '\r' -> { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2010 | cr1 = true |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2011 | true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2012 | } |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2013 | ch == '\n' -> { |
| 2014 | eol = true |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2015 | forceConsume = true |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2016 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2017 | } |
| 2018 | cr1 -> { |
| 2019 | cr1 = false |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2020 | eol = true |
| 2021 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2022 | } |
| 2023 | else -> { |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2024 | if (consumed1 == limit) throw BufferOverflowException() |
| 2025 | consumed1++ |
| 2026 | out.append(ch) |
| 2027 | true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2032 | if (cr1 && last) { |
| 2033 | eol = true |
| 2034 | } |
| 2035 | |
| 2036 | if (eol && forceConsume) { |
Sergey Mashkov | 0b12ffc | 2017-07-26 12:59:16 +0300 | [diff] [blame] | 2037 | buffer.position(buffer.position() + 1) |
| 2038 | } |
| 2039 | |
| 2040 | rc != 0 && !eol && !last |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2041 | } |
| 2042 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2043 | if (eol) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2044 | |
| 2045 | return readUTF8LineToUtf8Suspend(out, limit, cr1, consumed1 + consumed0) |
| 2046 | } |
| 2047 | |
| 2048 | private suspend fun readUTF8LineToUtf8Suspend(out: Appendable, limit: Int, cr0: Boolean, consumed0: Int): Boolean { |
| 2049 | var cr1 = cr0 |
| 2050 | var consumed1 = 0 |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2051 | var eol = false |
| 2052 | var wrap = 0 |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2053 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 2054 | consumeEachBufferRangeSuspend { buffer, last -> |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2055 | var forceConsume = false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2056 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2057 | val rc = buffer.decodeUTF8 { ch -> |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2058 | when { |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2059 | ch == '\r' -> { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2060 | cr1 = true |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2061 | true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2062 | } |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2063 | ch == '\n' -> { |
| 2064 | eol = true |
| 2065 | forceConsume = true |
| 2066 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2067 | } |
| 2068 | cr1 -> { |
| 2069 | cr1 = false |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2070 | eol = true |
| 2071 | false |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2072 | } |
| 2073 | else -> { |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2074 | if (consumed1 == limit) throw BufferOverflowException() |
| 2075 | consumed1++ |
| 2076 | out.append(ch) |
| 2077 | true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | } |
| 2081 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2082 | if (cr1 && last) { |
| 2083 | eol = true |
| 2084 | } |
| 2085 | |
| 2086 | if (eol && forceConsume) { |
| 2087 | buffer.position(buffer.position() + 1) |
| 2088 | } |
| 2089 | |
| 2090 | wrap = maxOf(0, rc) |
| 2091 | |
| 2092 | wrap == 0 && !eol && !last |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2093 | } |
| 2094 | |
Sergey Mashkov | a9a936d | 2017-08-23 14:39:41 +0300 | [diff] [blame] | 2095 | if (wrap != 0) { |
| 2096 | if (!readSuspend(wrap)) { |
| 2097 | |
| 2098 | } |
| 2099 | |
| 2100 | return readUTF8LineToUtf8Suspend(out, limit, cr1, consumed1) |
| 2101 | } |
| 2102 | |
| 2103 | return (consumed1 > 0 || consumed0 > 0 || eol) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2104 | } |
| 2105 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 2106 | suspend override fun <A : Appendable> readUTF8LineTo(out: A, limit: Int) = readUTF8LineToAscii(out, limit) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2107 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2108 | private fun resumeReadOp() { |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 2109 | ReadOp.getAndSet(this, null)?.apply { |
| 2110 | val closedCause = closed?.cause |
| 2111 | when { |
| 2112 | closedCause != null -> resumeWithException(closedCause) |
| 2113 | else -> resume(true) |
| 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
Sergey Mashkov | 060f36a | 2017-11-23 12:39:27 +0300 | [diff] [blame] | 2118 | private inline fun resumeReadOp(exception: () -> Throwable) { |
| 2119 | ReadOp.getAndSet(this, null)?.resumeWithException(exception()) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2120 | } |
| 2121 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2122 | private fun resumeWriteOp() { |
Sergey Mashkov | ec2af2f | 2018-01-15 17:02:25 +0300 | [diff] [blame] | 2123 | while (true) { |
| 2124 | val writeOp = writeOp ?: return |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2125 | val closed = closed |
Sergey Mashkov | 40f3fe5 | 2018-01-15 23:10:29 +0300 | [diff] [blame] | 2126 | if (closed == null && joining != null) { |
| 2127 | val state = state |
| 2128 | if (state is ReadWriteBufferState.Writing || state is ReadWriteBufferState.ReadingWriting) { |
| 2129 | } else if (state !== ReadWriteBufferState.Terminated) return |
| 2130 | } |
Sergey Mashkov | ec2af2f | 2018-01-15 17:02:25 +0300 | [diff] [blame] | 2131 | if (WriteOp.compareAndSet(this, writeOp, null)) { |
| 2132 | if (closed == null) writeOp.resume(Unit) else writeOp.resumeWithException(closed.sendException) |
| 2133 | return |
| 2134 | } |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2135 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2136 | } |
| 2137 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 2138 | private fun resumeClosed(cause: Throwable?) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2139 | ReadOp.getAndSet(this, null)?.let { c -> |
| 2140 | if (cause != null) |
| 2141 | c.resumeWithException(cause) |
| 2142 | else |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2143 | c.resume(state.capacity.availableForRead > 0) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2144 | } |
| 2145 | |
Sergey Mashkov | 9ef4e0c | 2017-12-06 17:09:01 +0300 | [diff] [blame] | 2146 | WriteOp.getAndSet(this, null)?.resumeWithException(cause ?: |
Roman Elizarov | 339ccf3 | 2017-09-27 17:44:00 +0300 | [diff] [blame] | 2147 | ClosedWriteChannelException(DEFAULT_CLOSE_MESSAGE)) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2148 | } |
| 2149 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2150 | private suspend fun readSuspend(size: Int): Boolean { |
| 2151 | val capacity = state.capacity |
| 2152 | if (capacity.availableForRead >= size) return true |
| 2153 | |
| 2154 | closed?.let { c -> |
| 2155 | if (c.cause != null) throw c.cause |
| 2156 | val afterCapacity = state.capacity |
| 2157 | val result = afterCapacity.flush() && afterCapacity.availableForRead >= size |
| 2158 | if (readOp != null) throw IllegalStateException("Read operation is already in progress") |
| 2159 | return result |
| 2160 | } |
| 2161 | |
| 2162 | if (size == 1) return readSuspendImpl(1) |
| 2163 | return readSuspendLoop(size) |
| 2164 | } |
| 2165 | |
| 2166 | private tailrec suspend fun readSuspendLoop(size: Int): Boolean { |
Sergey Mashkov | e4d3ce7 | 2017-09-11 10:31:08 +0300 | [diff] [blame] | 2167 | val capacity = state.capacity |
| 2168 | if (capacity.availableForRead >= size) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2169 | |
| 2170 | closed?.let { c -> |
Sergey Mashkov | e4d3ce7 | 2017-09-11 10:31:08 +0300 | [diff] [blame] | 2171 | if (c.cause != null) throw c.cause |
Sergey Mashkov | 32c90b1 | 2017-11-07 19:18:16 +0300 | [diff] [blame] | 2172 | val afterCapacity = state.capacity |
Sergey Mashkov | 62deb43 | 2017-11-09 19:50:26 +0300 | [diff] [blame] | 2173 | val result = afterCapacity.flush() && afterCapacity.availableForRead >= size |
| 2174 | if (readOp != null) throw IllegalStateException("Read operation is already in progress") |
| 2175 | return result |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | if (!readSuspendImpl(size)) return false |
| 2179 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2180 | return readSuspendLoop(size) |
| 2181 | } |
| 2182 | |
| 2183 | private val readSuspendContinuationCache = MutableDelegateContinuation<Boolean>() |
| 2184 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2185 | @Suppress("NOTHING_TO_INLINE") |
| 2186 | private inline fun readSuspendPredicate(size: Int): Boolean { |
| 2187 | val state = state |
| 2188 | |
| 2189 | if (state.capacity.availableForRead >= size) return false |
| 2190 | if (joining != null && writeOp != null && |
| 2191 | (state === ReadWriteBufferState.IdleEmpty || state is ReadWriteBufferState.IdleNonEmpty)) return false |
| 2192 | |
| 2193 | return true |
| 2194 | } |
| 2195 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2196 | private fun suspensionForSize(size: Int, c: Continuation<Boolean>): Any { |
| 2197 | do { |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2198 | if (!readSuspendPredicate(size)) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2199 | c.resume(true) |
| 2200 | break |
| 2201 | } |
| 2202 | |
| 2203 | closed?.let { |
| 2204 | if (it.cause != null) { |
| 2205 | c.resumeWithException(it.cause) |
| 2206 | } else { |
| 2207 | c.resume(state.capacity.flush() && state.capacity.availableForRead >= size) |
| 2208 | } |
| 2209 | return COROUTINE_SUSPENDED |
| 2210 | } |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2211 | } while (!setContinuation({ readOp }, ReadOp, c, { closed == null && readSuspendPredicate(size) })) |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2212 | |
| 2213 | return COROUTINE_SUSPENDED |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | private suspend fun readSuspendImpl(size: Int): Boolean { |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2217 | if (!readSuspendPredicate(size)) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2218 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2219 | return suspendCoroutineOrReturn { raw -> |
| 2220 | val c = readSuspendContinuationCache |
| 2221 | suspensionForSize(size, c) |
| 2222 | c.swap(raw) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2226 | private fun shouldResumeReadOp() = joining != null && |
| 2227 | (state === ReadWriteBufferState.IdleEmpty || state is ReadWriteBufferState.IdleNonEmpty) |
| 2228 | |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2229 | private fun writeSuspendPredicate(size: Int): Boolean { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2230 | val joined = joining |
Sergey Mashkov | 33d5d13 | 2017-11-10 10:28:57 +0300 | [diff] [blame] | 2231 | val state = state |
| 2232 | val closed = closed |
| 2233 | |
Sergey Mashkov | 39a8854 | 2017-12-19 18:41:34 +0300 | [diff] [blame] | 2234 | return when { |
| 2235 | closed != null -> false |
| 2236 | joined == null -> state.capacity.availableForWrite < size && state !== ReadWriteBufferState.IdleEmpty |
Sergey Mashkov | 40f3fe5 | 2018-01-15 23:10:29 +0300 | [diff] [blame] | 2237 | else -> state !== ReadWriteBufferState.Terminated && state !is ReadWriteBufferState.Writing && state !is ReadWriteBufferState.ReadingWriting |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2241 | private val writeSuspendContinuationCache = MutableDelegateContinuation<Unit>() |
| 2242 | @Volatile |
| 2243 | private var writeSuspensionSize: Int = 0 |
| 2244 | private val writeSuspension = { c: Continuation<Unit> -> |
| 2245 | val size = writeSuspensionSize |
| 2246 | |
| 2247 | do { |
| 2248 | closed?.sendException?.let { throw it } |
| 2249 | if (!writeSuspendPredicate(size)) { |
| 2250 | c.resume(Unit) |
| 2251 | break |
| 2252 | } |
| 2253 | } while (!setContinuation({ writeOp }, WriteOp, c, { writeSuspendPredicate(size) })) |
| 2254 | |
| 2255 | flushImpl(1, minWriteSize = size) |
| 2256 | |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2257 | if (shouldResumeReadOp()) { |
| 2258 | resumeReadOp() |
| 2259 | } |
| 2260 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2261 | COROUTINE_SUSPENDED |
| 2262 | } |
| 2263 | |
| 2264 | private suspend fun tryWriteSuspend(size: Int) { |
| 2265 | if (!writeSuspendPredicate(size)) { |
| 2266 | closed?.sendException?.let { throw it } |
| 2267 | return |
| 2268 | } |
| 2269 | |
| 2270 | writeSuspensionSize = size |
| 2271 | if (attachedJob != null) { |
| 2272 | return suspendCoroutineOrReturn(writeSuspension) |
| 2273 | } |
| 2274 | |
| 2275 | return suspendCoroutineOrReturn { raw -> |
| 2276 | val c = writeSuspendContinuationCache |
| 2277 | writeSuspension(c) |
| 2278 | c.swap(raw) |
| 2279 | } |
| 2280 | } |
| 2281 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 2282 | private suspend fun writeSuspend(size: Int) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2283 | while (writeSuspendPredicate(size)) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2284 | suspendCancellableCoroutine<Unit>(holdCancellability = true) { c -> |
| 2285 | do { |
| 2286 | closed?.sendException?.let { throw it } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2287 | if (!writeSuspendPredicate(size)) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2288 | c.resume(Unit) |
| 2289 | break |
| 2290 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2291 | } while (!setContinuation({ writeOp }, WriteOp, c, { writeSuspendPredicate(size) })) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2292 | |
Sergey Mashkov | e676408 | 2017-12-05 12:27:09 +0300 | [diff] [blame] | 2293 | flushImpl(1, minWriteSize = size) |
Sergey Mashkov | d1842c6 | 2018-01-23 23:16:54 +0300 | [diff] [blame] | 2294 | |
| 2295 | if (shouldResumeReadOp()) { |
| 2296 | resumeReadOp() |
| 2297 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | closed?.sendException?.let { throw it } |
| 2302 | } |
| 2303 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2304 | private inline fun <T, C : Continuation<T>> setContinuation(getter: () -> C?, updater: AtomicReferenceFieldUpdater<ByteBufferChannel, C?>, continuation: C, predicate: () -> Boolean): Boolean { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2305 | while (true) { |
| 2306 | val current = getter() |
| 2307 | if (current != null) throw IllegalStateException("Operation is already in progress") |
| 2308 | |
| 2309 | if (!predicate()) { |
| 2310 | return false |
| 2311 | } |
| 2312 | |
| 2313 | if (updater.compareAndSet(this, null, continuation)) { |
| 2314 | if (predicate() || !updater.compareAndSet(this, continuation, null)) { |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2315 | if (attachedJob == null && continuation is CancellableContinuation<*>) { |
| 2316 | continuation.initCancellability() |
| 2317 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2318 | return true |
| 2319 | } |
| 2320 | |
| 2321 | return false |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | private fun newBuffer(): ReadWriteBufferState.Initial { |
| 2327 | val result = pool.borrow() |
| 2328 | |
Sergey Mashkov | f774f37 | 2017-10-09 12:09:16 +0300 | [diff] [blame] | 2329 | result.readBuffer.order(readByteOrder.nioOrder) |
| 2330 | result.writeBuffer.order(writeByteOrder.nioOrder) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2331 | result.capacity.resetForWrite() |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2332 | |
| 2333 | return result |
| 2334 | } |
| 2335 | |
| 2336 | private fun releaseBuffer(buffer: ReadWriteBufferState.Initial) { |
| 2337 | pool.recycle(buffer) |
| 2338 | } |
| 2339 | |
Sergey Mashkov | 39a8854 | 2017-12-19 18:41:34 +0300 | [diff] [blame] | 2340 | private inline fun updateStateAndGet(block: (ReadWriteBufferState) -> ReadWriteBufferState?): ReadWriteBufferState { |
| 2341 | val updater = State |
| 2342 | while (true) { |
| 2343 | val old = state |
| 2344 | val newState = block(old) ?: continue |
| 2345 | if (old === newState || updater.compareAndSet(this, old, newState)) return newState |
| 2346 | } |
| 2347 | } |
| 2348 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2349 | companion object { |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 2350 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2351 | private const val ReservedLongIndex = -8 |
| 2352 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2353 | // todo: replace with atomicfu, remove companion object |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2354 | private val State = updater(ByteBufferChannel::state) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2355 | private val WriteOp = updater(ByteBufferChannel::writeOp) |
| 2356 | private val ReadOp = updater(ByteBufferChannel::readOp) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2357 | private val Closed = updater(ByteBufferChannel::closed) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2358 | } |
| 2359 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 2360 | private object TerminatedLookAhead : LookAheadSuspendSession { |
| 2361 | override fun consumed(n: Int) { |
| 2362 | if (n > 0) throw IllegalStateException("Unable to mark $n bytes consumed for already terminated channel") |
| 2363 | } |
| 2364 | |
| 2365 | override fun request(skip: Int, atLeast: Int) = null |
| 2366 | |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 2367 | suspend override fun awaitAtLeast(n: Int): Boolean { |
| 2368 | return false |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
Sergey Mashkov | 86fe591 | 2017-07-25 11:20:01 +0300 | [diff] [blame] | 2372 | private class ClosedElement(val cause: Throwable?) { |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2373 | val sendException: Throwable |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2374 | get() = cause ?: ClosedWriteChannelException("The channel was closed") |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2375 | |
| 2376 | companion object { |
| 2377 | val EmptyCause = ClosedElement(null) |
| 2378 | } |
| 2379 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2380 | |
Sergey Mashkov | 7831716 | 2018-03-21 10:12:40 +0300 | [diff] [blame] | 2381 | internal class JoiningState(val delegatedTo: ByteBufferChannel, val delegateClose: Boolean, val delegateFlush: Boolean) { |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2382 | private val _closeWaitJob = atomic<Job?>(null) |
| 2383 | private val closed = atomic(0) |
| 2384 | |
| 2385 | private val closeWaitJob: Job |
| 2386 | get() { |
| 2387 | while (true) { |
| 2388 | val current = _closeWaitJob.value |
| 2389 | if (current != null) return current |
| 2390 | val newJob = Job() |
Sergey Mashkov | 47d92ba | 2017-11-17 19:30:22 +0300 | [diff] [blame] | 2391 | if (_closeWaitJob.compareAndSet(null, newJob)) { |
| 2392 | if (closed.value == 1) newJob.cancel() |
| 2393 | return newJob |
| 2394 | } |
Sergey Mashkov | fd85fb9 | 2017-11-08 22:27:18 +0300 | [diff] [blame] | 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | fun complete() { |
| 2399 | closed.value = 1 |
| 2400 | _closeWaitJob.getAndSet(null)?.cancel() |
| 2401 | } |
| 2402 | |
| 2403 | suspend fun awaitClose() { |
| 2404 | if (closed.value == 1) return |
| 2405 | return closeWaitJob.join() |
| 2406 | } |
| 2407 | } |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 2408 | } |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 2409 | |