org.reaktivity.nukleus.http2.internal.HttpWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nukleus-http2 Show documentation
Show all versions of nukleus-http2 Show documentation
HTTP/2 Nukleus Implementation
/**
* Copyright 2016-2017 The Reaktivity Project
*
* The Reaktivity Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.reaktivity.nukleus.http2.internal;
import org.agrona.DirectBuffer;
import org.agrona.MutableDirectBuffer;
import org.agrona.concurrent.UnsafeBuffer;
import org.reaktivity.nukleus.function.MessageConsumer;
import org.reaktivity.nukleus.http2.internal.types.Flyweight;
import org.reaktivity.nukleus.http2.internal.types.HttpHeaderFW;
import org.reaktivity.nukleus.http2.internal.types.ListFW;
import org.reaktivity.nukleus.http2.internal.types.stream.AbortFW;
import org.reaktivity.nukleus.http2.internal.types.stream.BeginFW;
import org.reaktivity.nukleus.http2.internal.types.stream.DataFW;
import org.reaktivity.nukleus.http2.internal.types.stream.EndFW;
import org.reaktivity.nukleus.http2.internal.types.stream.HttpBeginExFW;
import java.util.function.Consumer;
import static java.nio.charset.StandardCharsets.UTF_8;
class HttpWriter
{
private static final DirectBuffer SOURCE_NAME_BUFFER = new UnsafeBuffer("http2".getBytes(UTF_8));
private final BeginFW.Builder beginRW = new BeginFW.Builder();
private final DataFW.Builder dataRW = new DataFW.Builder();
private final EndFW.Builder endRW = new EndFW.Builder();
private final AbortFW.Builder abortRW = new AbortFW.Builder();
private final HttpBeginExFW.Builder httpBeginExRW = new HttpBeginExFW.Builder();
private final MutableDirectBuffer writeBuffer;
HttpWriter(MutableDirectBuffer writeBuffer)
{
this.writeBuffer = writeBuffer;
}
// HTTP begin frame's extension data is written using the given buffer
void doHttpBegin(
MessageConsumer target,
long targetId,
long targetRef,
long correlationId,
DirectBuffer extBuffer,
int extOffset,
int extLength)
{
BeginFW begin = beginRW.wrap(writeBuffer, 0, writeBuffer.capacity())
.streamId(targetId)
.source(SOURCE_NAME_BUFFER, 0, SOURCE_NAME_BUFFER.capacity())
.sourceRef(targetRef)
.correlationId(correlationId)
.extension(extBuffer, extOffset, extLength)
.build();
target.accept(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof());
}
void doHttpBegin(
MessageConsumer target,
long targetId,
long targetRef,
long correlationId,
Consumer> mutator)
{
BeginFW begin = beginRW.wrap(writeBuffer, 0, writeBuffer.capacity())
.streamId(targetId)
.source(SOURCE_NAME_BUFFER, 0, SOURCE_NAME_BUFFER.capacity())
.sourceRef(targetRef)
.correlationId(correlationId)
.extension(e -> e.set(visitHttpBeginEx(mutator)))
.build();
target.accept(begin.typeId(), begin.buffer(), begin.offset(), begin.sizeof());
}
void doHttpData(
MessageConsumer target,
long targetId,
DirectBuffer payload,
int offset,
int length)
{
assert length < 65536; // DATA frame length is 2 bytes
DataFW data = dataRW.wrap(writeBuffer, 0, writeBuffer.capacity())
.streamId(targetId)
.payload(p -> p.set(payload, offset, length))
.build();
target.accept(data.typeId(), data.buffer(), data.offset(), data.sizeof());
}
void doHttpEnd(
MessageConsumer target,
long targetId)
{
EndFW end = endRW.wrap(writeBuffer, 0, writeBuffer.capacity())
.streamId(targetId)
.extension(e -> e.reset())
.build();
target.accept(end.typeId(), end.buffer(), end.offset(), end.sizeof());
}
void doHttpAbort(
final MessageConsumer target,
final long targetId)
{
final AbortFW abort = abortRW.wrap(writeBuffer, 0, writeBuffer.capacity())
.streamId(targetId)
.extension(e -> e.reset())
.build();
target.accept(abort.typeId(), abort.buffer(), abort.offset(), abort.sizeof());
}
private Flyweight.Builder.Visitor visitHttpBeginEx(
Consumer> headers)
{
return (buffer, offset, limit) ->
httpBeginExRW.wrap(buffer, offset, limit)
.headers(headers)
.build()
.sizeof();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy