sqlx_core/mssql/protocol/
header.rs

1use crate::io::Encode;
2
3pub(crate) struct AllHeaders<'a>(pub(crate) &'a [Header]);
4
5impl Encode<'_> for AllHeaders<'_> {
6    fn encode_with(&self, buf: &mut Vec<u8>, _: ()) {
7        let offset = buf.len();
8        buf.resize(buf.len() + 4, 0);
9
10        for header in self.0 {
11            header.encode_with(buf, ());
12        }
13
14        let len = buf.len() - offset;
15        buf[offset..(offset + 4)].copy_from_slice(&(len as u32).to_le_bytes());
16    }
17}
18
19pub(crate) enum Header {
20    TransactionDescriptor {
21        // number of requests currently active on the connection
22        outstanding_request_count: u32,
23
24        // for each connection, a number that uniquely identifies the transaction with which the
25        // request is associated; initially generated by the server when a new transaction is
26        // created and returned to the client as part of the ENVCHANGE token stream
27        transaction_descriptor: u64,
28    },
29}
30
31impl Encode<'_> for Header {
32    fn encode_with(&self, buf: &mut Vec<u8>, _: ()) {
33        match self {
34            Header::TransactionDescriptor {
35                outstanding_request_count,
36                transaction_descriptor,
37            } => {
38                buf.extend(&18_u32.to_le_bytes()); // [HeaderLength] 4 + 2 + 8 + 4
39                buf.extend(&2_u16.to_le_bytes()); // [HeaderType]
40                buf.extend(&transaction_descriptor.to_le_bytes());
41                buf.extend(&outstanding_request_count.to_le_bytes());
42            }
43        }
44    }
45}