Package express.mvp.myra.transport.framing
package express.mvp.myra.transport.framing
Message framing utilities for network protocols.
This package provides abstractions and implementations for message framing, which is the process of delimiting messages in a byte stream. Framing is essential for reliable communication over stream-based protocols like TCP, which provide a continuous byte stream without inherent message boundaries.
Key Components
FramingHandler- Strategy interface for framing/deframingLengthPrefixedFramingHandler- 4-byte big-endian length prefix implementationFramingException- Exception for framing errors
Framing Strategies
Common framing strategies include:
- Length-prefixed: Each message is preceded by its length (implemented here)
- Delimiter-based: Messages are separated by special byte sequences
- Fixed-length: All messages have the same size
This package currently provides length-prefixed framing, which is widely used in RPC protocols and binary formats due to its efficiency and simplicity.
Usage Example
import express.mvp.myra.transport.framing.*;
import java.lang.foreign.*;
// Create a framing handler
FramingHandler framing = new LengthPrefixedFramingHandler();
// Frame a message for sending
try (Arena arena = Arena.ofConfined()) {
byte[] payload = "Hello, World!".getBytes(StandardCharsets.UTF_8);
MemorySegment source = MemorySegment.ofArray(payload);
MemorySegment frame = arena.allocate(payload.length + framing.getHeaderSize());
int frameLength = framing.frameMessage(source, payload.length, frame);
sendToNetwork(frame.asSlice(0, frameLength));
}
// Deframe a received message
try (Arena arena = Arena.ofConfined()) {
MemorySegment received = receiveFromNetwork();
MemorySegment output = arena.allocate(framing.getMaxPayloadSize());
int payloadLength = framing.deframeMessage(received, (int) received.byteSize(), output);
if (payloadLength >= 0) {
processPayload(output.asSlice(0, payloadLength));
} else {
// Need more data for complete frame
bufferPartialData(received);
}
}
- See Also:
-
ClassDescriptionException thrown when message framing or deframing fails.Strategy interface for message framing and deframing in network protocols.A framing handler that uses a 4-byte big-endian length prefix.