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,
impl<T> Connection<T>where
T: AsyncRead + AsyncWrite,
Sourcepub fn new(transport: T) -> Self
pub fn new(transport: T) -> Self
Create a new connection from a transport.
The transport is immediately split into read and write halves.
Sourcepub fn with_codecs(
transport: T,
read_codec: TdsCodec,
write_codec: TdsCodec,
) -> Self
pub fn with_codecs( transport: T, read_codec: TdsCodec, write_codec: TdsCodec, ) -> Self
Create a new connection with custom codecs.
Sourcepub fn cancel_handle(&self) -> CancelHandle<T>
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.
Sourcepub fn is_cancelling(&self) -> bool
pub fn is_cancelling(&self) -> bool
Check if a cancellation is currently in progress.
Sourcepub async fn read_message(&mut self) -> Result<Option<Message>, CodecError>
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.
Sourcepub async fn read_packet(&mut self) -> Result<Option<Packet>, CodecError>
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.
Sourcepub async fn send_packet(&mut self, packet: Packet) -> Result<(), CodecError>
pub async fn send_packet(&mut self, packet: Packet) -> Result<(), CodecError>
Send a packet on the connection.
Sourcepub async fn send_message(
&mut self,
packet_type: PacketType,
payload: Bytes,
max_packet_size: usize,
) -> Result<(), CodecError>
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.
If reset_connection is true, the RESETCONNECTION flag is set on the
first packet. This causes SQL Server to reset connection state (temp
tables, SET options, isolation level, etc.) before executing the command.
Per TDS spec, this flag MUST only be set on the first packet of a message.
Sourcepub async fn send_message_with_reset(
&mut self,
packet_type: PacketType,
payload: Bytes,
max_packet_size: usize,
reset_connection: bool,
) -> Result<(), CodecError>
pub async fn send_message_with_reset( &mut self, packet_type: PacketType, payload: Bytes, max_packet_size: usize, reset_connection: bool, ) -> Result<(), CodecError>
Send a complete message with optional connection reset.
If reset_connection is true, the RESETCONNECTION flag is set on the
first packet. This causes SQL Server to reset connection state (temp
tables, SET options, isolation level, etc.) before executing the command.
Per TDS spec, this flag MUST only be set on the first packet of a message.
Sourcepub async fn flush(&mut self) -> Result<(), CodecError>
pub async fn flush(&mut self) -> Result<(), CodecError>
Flush the write buffer.
Sourcepub fn read_codec(&self) -> &TdsCodec
pub fn read_codec(&self) -> &TdsCodec
Get a reference to the read codec.
Sourcepub fn read_codec_mut(&mut self) -> &mut TdsCodec
pub fn read_codec_mut(&mut self) -> &mut TdsCodec
Get a mutable reference to the read codec.