Package anbxj
Class Channel_Protobuf
java.lang.Object
anbxj.Channel_Abstraction
anbxj.Channel_Protobuf
A length-delimited communication channel that uses a
Channel_MessageSerializer to translate between protocol-level
objects and raw bytes, and Google's Protobuf
CodedOutputStream/CodedInputStream for compact
varint-prefixed framing.
This channel expects an already connected Socket (which may
be plain TCP or an SSLSocket - the framing is
identical). After construction, it immediately begins reading/writing
varint-delimited messages. The serialiser's per-step protocol
awareness is driven by setStep(int).
-
Field Summary
Fields inherited from class anbxj.Channel_Abstraction
cp -
Constructor Summary
ConstructorsConstructorDescriptionChannel_Protobuf(Socket socket, Channel_Properties cp, Channel_MessageSerializer serializer) Constructs a new protobuf-framed channel for an already connected socket. -
Method Summary
Modifier and TypeMethodDescriptionvoidClose()Closes the underlying socket.voidOpen()Does nothing - the socket is expected to be already connected when the channel is created.Receive()Reads a varint-prefixed message from the socket, deserializes it using the current step number, and returns the reconstructed protocol object.voidSerializes the given object using the current step number, prefixed the resulting bytes with a varint length, and sends them over the socket.voidsetStep(int step) Informs the channel about the current protocol step.Methods inherited from class anbxj.Channel_Abstraction
checkPort, getChannelRole, getChannelSettings, OpenStreams, Receive, safeReadObject
-
Constructor Details
-
Channel_Protobuf
Constructs a new protobuf-framed channel for an already connected socket.- Parameters:
socket- the connected socket (plain TCP or SSL)cp- the channel properties (host, port, security type, etc.) - mainly used for debugging and loggingserializer- the message serializer that will handle conversion of protocol objects to and from byte arrays
-
-
Method Details
-
getSerializer
-
setStep
public void setStep(int step) Informs the channel about the current protocol step. This value is passed to the serializer every time an object is sent or received, enabling per-step message schemas.- Overrides:
setStepin classChannel_Abstraction- Parameters:
step- the protocol step number (1-based)
-
Open
public void Open()Does nothing - the socket is expected to be already connected when the channel is created.- Overrides:
Openin classChannel_Abstraction
-
Send
Serializes the given object using the current step number, prefixed the resulting bytes with a varint length, and sends them over the socket.- Overrides:
Sendin classChannel_Abstraction- Parameters:
obj- the protocol object to send- Throws:
RuntimeException- if an I/O error occurs during sending
-
Receive
Reads a varint-prefixed message from the socket, deserializes it using the current step number, and returns the reconstructed protocol object.- Overrides:
Receivein classChannel_Abstraction- Returns:
- the deserialized protocol object
- Throws:
RuntimeException- if an I/O error occurs or the deserialization fails
-
Close
public void Close()Closes the underlying socket. Any I/O error during closing is logged but suppressed.- Overrides:
Closein classChannel_Abstraction
-