Skip to content

Latest commit

 

History

History
44 lines (38 loc) · 2.84 KB

coding-rules.md

File metadata and controls

44 lines (38 loc) · 2.84 KB

Project coding rules

This page describes how the library is structured and where the hot path may work differently from the rest of the code. The rules apply to all code in this repository; CONTRIBUTING.md describes the general style.

Conventions

  • One type with behaviour per unit. Records that belong to a single class (options, slots, results) go into <Unit>.Types, for example WebSocket4D.Tls.Types.
  • Folders are layers. Source/Core uses only the RTL; Source/Tls uses Core; backends in Source/Tls/<Backend> use Core and Tls; Source/Client may use all of them. Never add a uses that points the other way.
  • Exception classes are grouped in WebSocket4D.Exceptions. They are a small hierarchy with no behaviour beyond building their message, and callers catch them together.
  • Test units live in Tests/<Folder>, mirroring Source/<Folder>. Consumers add the Source folders to their search path, and test units must not end up there.

Allowed only where stated

The exceptions below apply only where stated. Everywhere else (handshake, URL parsing, options, events, samples, tests), the general style applies.

  • Pointers and pointer arithmetic on the hot path: frame parsing, masking, UTF-8 validation, inflate/deflate (zlib and zstd), SPSC queue and socket I/O. Reason: zero-copy parsing and word-at-a-time loops.
  • var parameters for growable buffers passed into hot-path helpers. Reason: SetLength may move the array and the caller must see the new reference. A var parameter is passed by address, so it copies nothing.
  • Comments only where they document how a protocol is implemented (RFC 6455, RFC 7692, the zlib-stream and zstd-stream formats) or something important that code cannot express: cache-line layout, memory ordering, thread ownership, buffer lifetime. API descriptions that restate the signature are not allowed.
  • No framework dependencies; TArray<T>/SetLength for collections. Reason: the hot path needs flat arrays, and a transport library should not force a framework on consumers.
  • on E: Exception without re-raise at thread entry points and around user callbacks. Reason: an exception escaping a worker thread or a user handler must not kill the connection silently; it is reported through OnError or turned into a close code.
  • Cracker classes for TLS handles: TTaurusTLSSocketAccess (WebSocket4D.Tls.TaurusTLS.Access), and TIdSSLSocketAccess with TIdSSLContextAccess (WebSocket4D.Tls.Indy.Access; two crackers in one unit, as they open up one library). Reason: TaurusTLS and Indy keep the PSSL and SSL_CTX handles protected. The TLS sessions need PSSL for direct non-blocking SSL_read/SSL_write, and the Indy backend needs SSL_CTX to load the Windows root store. Re-check on every TaurusTLS or Indy upgrade.