CCommonSubService:程式內部真正的高層訂閱 APICCommonSubService。它已經幫我們處理好「訂閱 → 分發 → 回呼 → 緩存重放」全部流程。
CCommonSubService::Singleton() ← 全域單例
CCommonSubService::Init() / UnInit()
CCommonSubService::Subscribe(unsigned long securityId,
std::string const& 通道名,
std::unordered_set<std::string> const& 要訂的欄位集)
CCommonSubService::Unsubscribe(unsigned long securityId, std::string const&)
CCommonSubService::UnsubscribeAll()
CCommonSubService::SendAllSubscribeReq() / SendUnsubscribeAllReq()
CCommonSubService::FlushPendingSubscribes() / TriggerFlush()
CCommonSubService::OnSubscribeResp(bool, int) / OnHeartbeat()
CCommonSubService::ReplayCachedData(quote::common_sub::CommonSubKey const&) ← ★ 重放緩存
CCommonSubService::GenerateHandle() / SetHandleName(unsigned long, std::string const&)
Subscribe 的簽名清晰可讀。FlushPendingSubscribes 表示有批次合併,
OnHeartbeat 表示有連線保活,ReplayCachedData 表示有本地緩存且可重放。Subscribe 真的在做訂閱我把 libQotBase.so 的程式碼段抄回本機(x86-64 objdump)反匯編
CCommonSubService::Subscribe @ 0x2f6650:
2f6650: push %r15 2f6652: lea 0x38ccd3(%rip),%r9 # 0x68332c ← 源碼檔名字串 2f6659: mov $0x5d,%r8d ← 行號 93 ... 2f666e: mov %rdi,0x10(%rsp) ← this 2f6673: mov %rsi,0x38(%rsp) ← securityId 2f667d: mov %rcx,0x8(%rsp) ← 欄位集 2f6687: test %rsi,%rsi 2f668a: je 0x2f6ead ← securityId==0 就跳走 2f6690: cmpq $0x0,0x8(%rdx) 2f6698: lea 0x38c9ee(%rip),%r9 # 0x68308d ← 第二個檔名字串 2f669f: mov $0x5e,%r8d ← 行號 94 2f66a5: je 0x2f6ead ← 通道名為空就跳走 2f66b0: cmpq $0x0,0x18(%rax) 2f66b5: je 0x2f6ea0 ← 欄位集為空就跳走 2f66c0: lea 0x48(%rax),%rdi ← this+0x48(內部 map) 2f66d6: call 0x22af40 ← 建立訂閱鍵 2f6725: movl $0x3f800000,0xe0(%rsp) ← 1.0f(優先級/權重)
this+0x48 是內部容器,call 0x22af40 建立鍵 → 訂閱以 map 索引管理。1.0f 寫入堆疊結構 → 每個訂閱帶優先級/權重。0x68332c/0x68308d,行號 93 / 94——即源碼裡兩個相鄰的斷言。
.rodata 裡。
讀出來就能知道它檢查了甚麼、以及檔案叫甚麼名字。下一步做這個(只讀)。
quote::common_sub與 CCommonSubService 配套的是一整套觀察者模式:
| 符號 | 作用 |
|---|---|
quote::common_sub::ICommonSubObserver | 觀察者介面——實現它就能收推送 |
quote::common_sub::AddObserver / DelObserver | 註冊/移除觀察者 |
quote::common_sub::CCommonSubDispatcher | 分發器(事件廣播) |
quote::common_sub::CommonSubEvent | 推送事件物件 |
quote::common_sub::CommonSubKey | 訂閱鍵(securityId + 通道) |
quote::common_sub::TryGetParsedData | 取已解析的數據 |
quote::common_sub::CCommonSubDataMgr | 數據管理(含緩存) |
quote::common_sub::CCommonSubHelper | 便捷封裝 |
quote::common_sub::Sub / UnSub | 訂/退訂 |
TryGetParsedData 是最乾淨的目標。
名字已經說明一切:拿「已解析」的數據——不需要我們自己解 protobuf。
如果能在不新增訂閱的情況下取得現有訂閱的已解析數據,那就是完美的取數路徑。
從 .rodata 抽到與訂閱配置相關的字串:
quote_sub_conf ← 訂閱配置 sub_security_type ← 證券類型(通道分類) sub_instrument_type ← 工具類型 quote_type / quote_types ← 報價類型 quote_quality_mopage_type ← 行情質量/權限檔位 quote_display_type quote_config_svr ← 訂閱配置服務 quote_currency
Subscribe() 第二個參數的實際值。
那些實際值可能是短字串(如 "ticker"、"rt"),藏在更隱蔽的位置。
要確認,需要在執行期把 map 的鍵印出來——那屬 live 操作,需批准。
| # | 位置 | 侵入度 | 評價 |
|---|---|---|---|
| 1 | common_sub::TryGetParsedData | 最低 | 取已解析數據,不解 protobuf。理想。但要先有已存在的訂閱(面板開著就有)。 |
| 2 | CCommonSubService::ReplayCachedData | 最低 | 重放本地緩存,零新增訂閱。適合補歷史。 |
| 3 | ICommonSubObserver 觀察者 | 低 | 註冊自己收推送。乾淨但要 preload。 |
| 4 | PullTicks::OnReply_PullTickerList(buf,len) | 中 | 原始 protobuf buffer。需自行解析,但數據最全。 |
| 5 | __SendCommand<Req,Resp> | 最高 | 模板、ABI 隨型別變、A99 crash 根源。不建議。 |
PullTicks::OnReply 排第一、把 sentinel 排在
SendCommand 之後。現在看,前三個才是更好的入口——因為它們都是穩定的非模板接口,
而且第 1、2 個不增加任何伺服器負載。PullTicks 應該退到第 4。
CCommonSubService 21 個方法簽名、Subscribe 的三個前置檢查與內部 map 操作
(本機 objdump 反匯編,位址 0x2f6650)、quote::common_sub 全部型別名。TryGetParsedData 的簽名與資料型別;
③ 是否可以在不 preload 的情況下呼叫(目前答案應該是不行,因為它是程式內部的 C++ 物件);
④ 訂閱上限;⑤ 緩存有多大/多久。