11# IExchangeConnector
22
3- ` IExchangeConnector ` is the abstract interface for real-time market data adapters. It provides lifecycle control and typed callback delivery for ` BookUpdateEvent ` and ` TradeEvent ` .
3+ ` IExchangeConnector ` is the abstract interface for real-time market data adapters. It provides lifecycle control, typed callback delivery for market data events, and error notifications .
44
55``` cpp
6- class IExchangeConnector : public ISubsystem
6+ class IExchangeConnector : public ISubsystem , public IDrainable
77{
88public:
9- using BookUpdateCallback = std::move_only_function<void(const BookUpdateEvent&)>;
10- using TradeCallback = std::move_only_function<void(const TradeEvent&)>;
9+ using BookUpdateCallback = MoveOnlyFunction<void(const BookUpdateEvent&)>;
10+ using TradeCallback = MoveOnlyFunction<void(const TradeEvent&)>;
11+ using DisconnectCallback = MoveOnlyFunction<void(std::string_view reason)>;
12+ using SequenceGapCallback = MoveOnlyFunction<void(uint64_t expected, uint64_t received)>;
13+ using StaleDataCallback = MoveOnlyFunction<void(SymbolId symbol, uint64_t lastUpdateMs)>;
1114
1215 virtual ~ IExchangeConnector() = default;
1316
17+ bool drain(uint32_t timeoutMs) override { return true; }
18+
1419 virtual std::string exchangeId() const = 0;
1520
1621 virtual void setCallbacks(BookUpdateCallback onBookUpdate, TradeCallback onTrade);
22+ virtual void setErrorCallbacks(DisconnectCallback onDisconnect,
23+ SequenceGapCallback onSequenceGap,
24+ StaleDataCallback onStaleData);
1725
1826protected:
1927 void emitBookUpdate(const BookUpdateEvent& bu);
2028 void emitTrade(const TradeEvent& t);
29+ void emitDisconnect(std::string_view reason);
30+ void emitSequenceGap(uint64_t expected, uint64_t received);
31+ void emitStaleData(SymbolId symbol, uint64_t lastUpdateMs);
2132};
2233```
2334
@@ -27,16 +38,26 @@ protected:
2738
2839## Responsibilities
2940
30- | Aspect | Details |
31- | ------------- | ----------------------------------------------------------------------- |
32- | Lifecycle | Inherits ` start() ` and ` stop() ` from ` ISubsystem ` . |
33- | Identity | ` exchangeId() ` provides a stable identifier for the connector instance. |
34- | Callbacks | ` setCallbacks() ` binds downstream handlers for book and trade events. |
35- | Event Routing | ` emitBookUpdate() ` and ` emitTrade() ` dispatch data to subscribers. |
41+ | Aspect | Details |
42+ | --------------- | ----------------------------------------------------------------------- |
43+ | Lifecycle | Inherits ` start() ` and ` stop() ` from ` ISubsystem ` . |
44+ | Draining | Implements ` IDrainable ` for graceful shutdown with pending operations. |
45+ | Identity | ` exchangeId() ` provides a stable identifier for the connector instance. |
46+ | Data Callbacks | ` setCallbacks() ` binds handlers for book and trade events. |
47+ | Error Callbacks | ` setErrorCallbacks() ` binds handlers for connection/data errors. |
48+ | Event Routing | ` emit*() ` methods dispatch data and errors to subscribers. |
49+
50+ ## Error Callbacks
51+
52+ | Callback | When Called |
53+ | -------------- | -------------------------------------------------------------- |
54+ | onDisconnect | Connection lost or closed unexpectedly. |
55+ | onSequenceGap | Sequence number gap detected (expected vs received). |
56+ | onStaleData | No updates received for a symbol beyond threshold. |
3657
3758## Notes
3859
39- * Inherits from ` ISubsystem ` , enabling unified lifecycle management via ` start() ` and ` stop() ` .
40- * Callbacks use ` std::move_only_function ` to avoid ` std::function ` overhead and enable capturing closures with ownership.
60+ * Inherits from ` ISubsystem ` and ` IDrainable ` , enabling unified lifecycle and graceful shutdown .
61+ * Callbacks use ` MoveOnlyFunction ` to avoid ` std::function ` overhead and enable capturing closures with ownership.
4162* Implementations must call ` emit*() ` manually from internal processing (e.g. websocket handler).
4263* The class is intentionally non-copyable and non-thread-safe — connectors are expected to run in isolated threads.
0 commit comments