Connection

Struct Connection 

Source
pub struct Connection<T>
where T: AsyncRead + AsyncWrite,
{ /* private fields */ }
Expand description

A TDS connection with split I/O for cancellation safety.

This struct splits the underlying transport into read and write halves, allowing Attention packets to be sent even while blocked reading results.

§Cancellation

SQL Server uses out-of-band “Attention” packets to cancel running queries. Without split I/O, the driver would be unable to send cancellation while blocked awaiting a read (e.g., processing a large result set).

§Example

use mssql_codec::Connection;
use tokio::net::TcpStream;

let stream = TcpStream::connect("localhost:1433").await?;
let conn = Connection::new(stream);

// Can cancel from another task while reading
let cancel_handle = conn.cancel_handle();
tokio::spawn(async move {
    tokio::time::sleep(Duration::from_secs(5)).await;
    cancel_handle.cancel().await?;
});

Implementations§

Source§

impl<T> Connection<T>
where T: AsyncRead + AsyncWrite,

Source

pub fn new(transport: T) -> Self

Create a new connection from a transport.

The transport is immediately split into read and write halves.

Source

pub fn with_codecs( transport: T, read_codec: TdsCodec, write_codec: TdsCodec, ) -> Self

Create a new connection with custom codecs.

Source

pub fn cancel_handle(&self) -> CancelHandle<T>

Get a handle for cancelling queries on this connection.

The handle can be cloned and sent to other tasks.

Source

pub fn is_cancelling(&self) -> bool

Check if a cancellation is currently in progress.

Source

pub async fn read_message(&mut self) -> Result<Option<Message>, CodecError>

Read the next complete message from the connection.

This handles multi-packet message reassembly automatically.

Source

pub async fn read_packet(&mut self) -> Result<Option<Packet>, CodecError>

Read a single packet from the connection.

This is lower-level than read_message and doesn’t perform reassembly.

Source

pub async fn send_packet(&mut self, packet: Packet) -> Result<(), CodecError>

Send a packet on the connection.

Source

pub async fn send_message( &mut self, packet_type: PacketType, payload: Bytes, max_packet_size: usize, ) -> Result<(), CodecError>

Send a complete message, splitting into multiple packets if needed.

Source

pub async fn flush(&mut self) -> Result<(), CodecError>

Flush the write buffer.

Source

pub fn read_codec(&self) -> &TdsCodec

Get a reference to the read codec.

Source

pub fn read_codec_mut(&mut self) -> &mut TdsCodec

Get a mutable reference to the read codec.

Trait Implementations§

Source§

impl<T> Debug for Connection<T>
where T: AsyncRead + AsyncWrite + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Connection<T>

§

impl<T> !RefUnwindSafe for Connection<T>

§

impl<T> Send for Connection<T>
where T: Send,

§

impl<T> Sync for Connection<T>
where T: Sync + Send,

§

impl<T> Unpin for Connection<T>

§

impl<T> !UnwindSafe for Connection<T>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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