Write a network backend¶
tdslite doesn't open network connections itself. It talks through a backend, and you can plug in your own when none of the built-in ones fits your network stack.
There are two ways, depending on what your stack looks like.
Your client looks like Arduino's EthernetClient¶
Many Arduino networking libraries copy the EthernetClient interface. If yours
has these members, use it with tdsl::arduino_driver as it is:
struct my_client {
int connect(const char * host, unsigned short port);
bool connected();
int available();
void flush();
void stop();
tdsl::size_t write(const unsigned char * buf, tdsl::size_t len);
int read(unsigned char * buf, unsigned long amount);
// Only needed with debug printing on (TDSL_DEBUG_PRINT_ENABLED):
tdsl::uint16_t remotePort();
IPAddress remoteIP();
};
tdsl::uint8_t net_buf [768] = {};
tdsl::arduino_driver<my_client> driver{net_buf};
If your client has most of these under other names, write a small wrapper class with these members that calls into it.
Anything else: implement the backend¶
A backend is a class that derives from tdsl::net::network_io_base and has
five functions: do_connect, do_disconnect, do_send and two do_recv.
The library checks at compile time that they exist with the right signatures.
This guide builds a backend on top of four functions of an imaginary network stack:
// Your network stack. Each returns a negative value on error.
int my_transport_open(const char * host, unsigned short port);
int my_transport_write(const unsigned char * data, unsigned long len);
int my_transport_read(unsigned char * data, unsigned long len); // blocks until len bytes arrive
void my_transport_close();
1. Write the class¶
#include <tdslite/tdslite.hpp>
#include <tdslite-net/base/network_io_base.hpp>
namespace my {
struct netimpl : public tdsl::net::network_io_base<netimpl> {
using network_io_result = typename network_io_base<netimpl>::network_io_result;
// The caller supplies the network buffer
template <tdsl::uint32_t N>
explicit netimpl(tdsl::uint8_t (&buf) [N]) noexcept {
this->network_buffer = tdsl::tdsl_buffer_object{buf};
}
// Connect to target:port. Called by driver.connect().
template <typename T>
tdsl::expected<tdsl::traits::true_type, int> do_connect(T target,
tdsl::uint16_t port) noexcept {
do_disconnect();
this->network_buffer.get_writer()->reset();
// target can be a flash, wide or plain string: copy it out
char host [256] = {};
tdsl::size_t n = 0;
for (auto c : target) {
if (c == 0 || n == sizeof(host) - 1) {
break;
}
host [n++] = static_cast<char>(c);
}
if (my_transport_open(host, port) < 0) {
return tdsl::unexpected(-1);
}
connected = true;
return tdsl::traits::true_type{};
}
tdsl::int32_t do_disconnect() noexcept {
if (connected) {
my_transport_close();
connected = false;
}
return 0;
}
// Send one TDS packet: its 8-byte header, then its data. The driver
// puts the header directly in front of the data, so as_packet()
// joins them into one block for a single write.
tdsl::int32_t do_send(tdsl::byte_view header, tdsl::byte_view message) noexcept {
if (not connected) {
return -1;
}
const auto packet = this->as_packet(header, message);
const bool ok =
packet.size_bytes()
? my_transport_write(packet.data(), packet.size_bytes()) >= 0
: (my_transport_write(header.data(), header.size_bytes()) >= 0 &&
my_transport_write(message.data(), message.size_bytes()) >= 0);
if (not ok) {
do_disconnect();
return -1;
}
return 0;
}
// Receive exactly `amount` bytes into dst
network_io_result do_recv(tdsl::uint32_t amount, tdsl::byte_span dst) noexcept {
if (amount > dst.size_bytes() || not connected) {
return tdsl::unexpected(-1);
}
if (my_transport_read(dst.data(), amount) < 0) {
do_disconnect();
return tdsl::unexpected(-2);
}
return amount;
}
// Receive exactly `amount` bytes into the free space of the network buffer
network_io_result do_recv(tdsl::uint32_t amount) noexcept {
auto writer = this->network_buffer.get_writer();
if (amount > writer->remaining_bytes()) {
return tdsl::unexpected(-3);
}
auto result = do_recv(amount, writer->free_span());
if (result) {
writer->advance(static_cast<tdsl::int32_t>(amount));
}
return result;
}
private:
bool connected = {false};
};
using driver = tdsl::driver<netimpl>;
} // namespace my
Things that matter:
- The network buffer. Set
this->network_bufferin the constructor. The library reads all its data from it. do_recvreads exactly. Both overloads return only after exactlyamountbytes have arrived, or with an error. The one-argument overload appends to the network buffer and advances its writer.- One write per packet. Send each packet with a single write where you can. On TLS connections this is required: SQL Server drops a strict connection when a packet arrives split over several TLS records.
- Close on error. After a failed send or receive, close the connection.
The driver reports
network_error, and the application reconnects.
2. Use it¶
static tdsl::uint8_t net_buf [2048] = {};
static my::driver driver{net_buf};
int run() {
my::driver::connection_parameters params;
params.server_name = "192.168.1.10";
params.user_name = "sa";
params.password = "<password>";
if (not(driver.connect(params) == my::driver::e_driver_error_code::success)) {
return 1;
}
return driver.execute_query("SELECT 1") ? 0 : 1;
}
driver.network() returns your backend object, if you need to configure it
before connecting.
3. Learn from the built-in backends¶
The BSD sockets backend is the closest to this skeleton, with timeouts and retries added. The ESP-IDF TLS backend shows how a backend handles TLS.
Related¶
- The network abstraction: why tdslite is split this way.
- Network backends