Skip to main content

OvpnCodec

Struct OvpnCodec 

Source
pub struct OvpnCodec { /* private fields */ }
Expand description

Tokio codec for the OpenVPN management interface.

The encoder serializes typed OvpnCommand values into correct wire-format bytes, including proper escaping and multi-line block framing.

The decoder performs the opposite operation. It uses command-tracking state to correctly distinguish single-line from multi-line responses, and accumulates multi-line >CLIENT: notifications into a single OvpnMessage before emitting them.

§Sequential usage and pipelining

The OpenVPN management protocol is strictly sequential: the server processes one command at a time and sends its response before reading the next command. The codec maintains a queue of expected response kinds — one per encoded command. This allows callers to pipeline multiple commands (encode A, then B, then C) without waiting for each response, as long as responses arrive in the same order. Outgoing bytes are not buffered by the codec itself — the Encoder implementation writes into the BytesMut that tokio_util::codec::Framed owns, and Framed flushes them to the socket.

Encoding while a multi-line response or >CLIENT: notification is being accumulated is still discouraged (and logged as a warning), because it means the caller is not draining the stream (emptying the read half of the codec).

§Notification interleaving

Real-time notifications (>STATE:, >LOG:, >BYTECOUNT:, etc.) can arrive at any time, including in the middle of a multi-line command response. The decoder emits these immediately as OvpnMessage::Notification without disrupting the ongoing accumulation. The completed multi-line response is emitted afterward with the interleaved notification lines excluded.

Consumers should always be prepared to handle Notification variants between sending a command and receiving its response.

Implementations§

Source§

impl OvpnCodec

Source

pub fn new() -> Self

Create a new codec with default state, ready to encode commands and decode responses.

Source

pub fn with_max_multi_line_lines(self, limit: AccumulationLimit) -> Self

Set the maximum number of lines accumulated in a multi-line response before the decoder returns an error.

Source

pub fn with_max_client_env_entries(self, limit: AccumulationLimit) -> Self

Set the maximum number of ENV entries accumulated for >CLIENT: notifications before the decoder returns an error.

Source

pub fn with_encoder_mode(self, mode: EncoderMode) -> Self

Set the encoder mode for handling unsafe characters in user-supplied strings.

The default is EncoderMode::Sanitize, which silently strips \n, \r, and \0. Use EncoderMode::Strict to reject inputs containing those characters with an error instead.

Trait Implementations§

Source§

impl Decoder for OvpnCodec

Source§

type Item = OvpnMessage

The type of decoded frames.
Source§

type Error = Error

The type of unrecoverable frame decoding errors. Read more
Source§

fn decode( &mut self, src: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>

Attempts to decode a frame from the provided buffer of bytes. Read more
Source§

fn decode_eof( &mut self, buf: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
Source§

fn framed<T>(self, io: T) -> Framed<T, Self>
where T: AsyncRead + AsyncWrite, Self: Sized,

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more
Source§

impl Default for OvpnCodec

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Encoder<OvpnCommand> for OvpnCodec

Source§

type Error = Error

The type of encoding errors. Read more
Source§

fn encode( &mut self, item: OvpnCommand, dst: &mut BytesMut, ) -> Result<(), Self::Error>

Encodes a frame into the buffer provided. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more