Conversation
this module will be used as source of truth, it will allso alow us to quickly search to the code by searching using symbol usage
… feature extensions New tokens added (not in the original implementation): - `offset` (0x78), `altmetadata` (0x88), `dataclassification` (0xA3), `tabname` (0xA4), `colinfo` (0xA5), `featureextack` (0xAE), `altrow` (0xD3), `sessionstate` (0xE4), `sspi` (0xED), `fedauthinfo` (0xEE)
Consolidates little-endian macros from `Tds.BinaryUtils` and big-endian and parameterized macros from `Tds.Protocol.Grammar` into a single module. Establishes clear conventions for byte order, with little-endian as the default and `_be` suffixes for big-endian variants. This centralizes common binary encoding and decoding utilities, improving consistency and maintainability across the TDS protocol implementation.
Centralizes all TDS protocol constants (packet types, token types, data types, etc.) into `Tds.Protocol.Constants` and binary utility functions into `Tds.Protocol.Binary`. This refactor replaces hardcoded hexadecimal values with compile-time macros, enhancing maintainability and consistency across the codebase. Corrects a bug in `Tds.Protocol.Prelogin` where the `:instopt` prelogin token was incorrectly decoded as an `:encryption` token.
Introduces `Tds.Protocol.Packet` for TDS packet framing. Provides `encode/2` to split payloads into 4096-byte packets, each with an 8-byte header and up to 4088 bytes of data. Packet IDs start at 1 and wrap at 256. Adds `decode_header/1` to parse 8-byte packet headers from incoming binaries.
Introduces `Packet.reassemble/2` to reconstruct complete TDS messages from a stream of packets. This function correctly handles message fragmentation by: * Concatenating data from multiple packets. * Validating sequential packet IDs to ensure correct ordering, including wrap-around behavior. * Enforcing a configurable maximum payload size to mitigate denial-of-service risks. * Gracefully managing partial `recv` operations and multiple packets within a single read from the socket.
Improves code quality and readability within the packet reassembly module. - Relocates type and constant definitions to the module top for better organization. - Removes an unused parameter from the `extract_and_continue` function. - Enhances documentation for the `:max_payload_size` option to show a clear default value. - Replaces a nested conditional with pattern matching in `finish_or_continue` for cleaner, more idiomatic code.
Migrates all calls to `encode_packets` and `encode_header` in `Messages`, `Prelogin`, and `Login7` to use the unified `Tds.Protocol.Packet.encode/2` function. Removes the now-unused `encode_header/4` and `encode_packets/3` from `Messages`, centralizing packet serialization logic and reducing duplication.
Adjusts `login7_test.exs` to correctly handle `iodata` returned by `Login7.encode`, converting it to a binary for assertion. Removes `messages_test.exs` as its functionality is now covered and superseded by `PacketTest`.
Replaces the internal `msg_recv` and `next_tds_pkg` functions with `Packet.reassemble/1`. This centralizes the logic for receiving and reassembling TDS packets into a dedicated module. It simplifies the `Tds.Protocol` module and improves error handling by leveraging the new `Packet` module's more robust error reporting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TDS v3 Internals Redesign
Goal: Incrementally rewrite the TDS library internals to deliver a well-layered, well-tested codebase with
user-extensible types, clean protocol layers, correct TLS, and proper error handling. Without breaking the
public API.
Motivation
The current TDS library (v2.x) works but has fundamental architectural problems:
types.exis 1800+ lines; adding one type requires 9-11 edits; no user extensibilitywithout forking
Tds.TlsGenServer has race conditions, unbounded buffers, and leaked processesProcess.putfor hidden state, unguarded transitions, silent transaction corruptionmacros in two modules with different endianness semantics
Phase 1: Constants & Binary Macros - Single Source of Truth
Tds.Protocol.Constants(compile-time macros for all TDS protocol constants)Tds.Protocol.Binary(unified binary macros with explicit endianness):instoptdecode bugBinaryUtilsandGrammarmodulesPhase 2: Packet Framing Layer
Tds.Protocol.Packet(encode/decode TDS packet frames with bounded reassembly)MessagesandProtocolto usePacketPhase 3: Extensible Type System
Tds.Typebehaviour (type_codes,decode_metadata,decode,encode,param_descriptor,infer)Tds.Type.Registry(TDS code <-> handler module mapping, user types checked first)uuid, money, xml)
Type.Registryinto Protocol, replacing the monolithictypes.exPhase 4: Transport Abstraction
Tds.Transportbehaviour andTransport.TcpimplementationTransport.Tls+TlsHandshakecb_info module (eliminates GenServer, fixes raceconditions)
Protocolto useTransportPhase 5: State Machine Cleanup
Protocol.Statewith explicit phase transitions (eliminatesProcess.put, guardsinvalid transitions)
Protocol.ConnectionandProtocol.ExecutionmodulesPhase 6: Error Handling
Tds.Errorwith:reason,:errors,:context,:original)Phase 7: Login/Prelogin & Connection Init Cleanup
inspect(self())thread ID)Bonus
mix tds.export_errorstask for refreshing SQL Server error codes CSVQuality Gates
mix compile --warnings-as-errorsmix format --check-formattedgrep -r "BinaryUtils\|Protocol.Grammar" lib/returns emptylib/tds.exfunction signatures match v2.x