scoop:scoop.http.asyncio:0.1.0
src/http2_stream.scoop
Selected production source · src/http2_stream.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.memory.NativeByteBuffer6
import scoop.time.TimeMark8
/**9
* One multiplexed HTTP/2 exchange. Serialize operations within each direction; different streams10
* can progress independently. Reading returns flow-control credit as data is consumed.11
*/12
public class AsyncHttp2Stream internal constructor(internal val state: H2State, internal val stream: H2Stream)13
: AsyncByteStream, AsyncCloseable {14
/**15
* The protocol stream identifier.16
*/17
public val id: Int get() = stream.id18
/**19
* The first response-byte time when timing collection is enabled.20
*/21
public val firstResponseByteAt: TimeMark? get() = state.lock.withLock { stream.firstResponseByteAt }22
/**23
* The retained stream failure, if one occurred.24
*/25
public val failure: Throwable? get() = state.lock.withLock { stream.failure }26
/**27
* Whether the input ended and all queued body bytes were consumed.28
*/29
public val isReadComplete: Boolean get() = state.lock.withLock { stream.inputEnded && stream.data.size == 0L }30
/**31
* Whether output END_STREAM was submitted.32
*/33
public val isWriteComplete: Boolean get() = state.lock.withLock { stream.sendEnded }34
/**35
* Complete input trailers after the input ended and all queued body bytes were consumed.36
*/37
public val trailers: Array<HttpField>? get() = state.lock.withLock {38
if (stream.inputEnded && stream.data.size == 0L) stream.trailers else None39
}41
/**42
* Receives the next response head for this client stream.43
*/44
public suspend fun receiveResponse(): HttpResponseHead = receiveH2Response(state, stream)46
/**47
* Sends a server response head, optionally closing the output direction immediately.48
*/49
public suspend fun sendResponse(head: HttpResponseHead, endStream: Boolean = true) {50
h2BodyRequest(state, stream, H2BodyKind.Flush, None, 0L) {51
state.session.submitResponse(stream.id, head, endStream)52
if (head.status >= 200U) { stream.sendStarted = true; stream.sendEnded = endStream }53
}54
}56
/**57
* Reads this stream's body and returns flow-control credit for bytes actually consumed.58
*/59
public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long =60
h2BodyRequest(state, stream, H2BodyKind.Read, Some(buffer), count, { })62
/**63
* Writes body bytes subject to peer flow-control windows and bounded pending output.64
*/65
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =66
h2BodyRequest(state, stream, H2BodyKind.Write, Some(buffer), count, { })68
/**69
* Waits for accepted stream output to be written, including window availability, and completes70
* the underlying transport flush.71
*/72
public override suspend fun flush() { h2BodyRequest(state, stream, H2BodyKind.Flush, None, 0L, { }) }74
/**75
* Submits optional trailers and ends this stream's output direction.76
*/77
public suspend fun finish(trailers: List<HttpField> = ArrayList<HttpField>()) {78
h2BodyRequest(state, stream, H2BodyKind.Flush, None, 0L) {79
state.session.finish(stream.id, trailers)80
stream.sendEnded = true81
}82
}84
/**85
* Resets or cleans up this stream without cancelling transport I/O shared by other streams.86
*/87
public override suspend fun close() { closeH2Stream(state, stream) }88
}