Package anbxj

Class Channel_Protobuf


public class Channel_Protobuf extends Channel_Abstraction
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).

  • Constructor Details

    • Channel_Protobuf

      public Channel_Protobuf(Socket socket, Channel_Properties cp, Channel_MessageSerializer serializer)
      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 logging
      serializer - the message serializer that will handle conversion of protocol objects to and from byte arrays
  • Method Details

    • getSerializer

      public Channel_MessageSerializer 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:
      setStep in class Channel_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:
      Open in class Channel_Abstraction
    • Send

      public void Send(Object obj)
      Serializes the given object using the current step number, prefixed the resulting bytes with a varint length, and sends them over the socket.
      Overrides:
      Send in class Channel_Abstraction
      Parameters:
      obj - the protocol object to send
      Throws:
      RuntimeException - if an I/O error occurs during sending
    • Receive

      public Object Receive()
      Reads a varint-prefixed message from the socket, deserializes it using the current step number, and returns the reconstructed protocol object.
      Overrides:
      Receive in class Channel_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:
      Close in class Channel_Abstraction