Skip to main content

futu_backend/conn/
protocol_identity.rs

1use futu_core::error::{FutuError, Result};
2
3/// Immutable protocol identity copied into every FTLogin/NN frame.
4///
5/// C++ initializes these values from `AppConfig` before any Platform, Broker,
6/// or WebTCP connection is created and each connection generation retains the
7/// same pair for its lifetime.
8/// Ref:
9/// - `FutuOpenD/Src/FTGateway/FTGTW_Inner_API.cpp:490-494`
10/// - `f3c/FTNet/Src/ftnet/channel/impl/protocol_header.cpp:25-32`
11///
12/// The numeric values are protocol enum/config output, not server-discovered
13/// transport data. They become invalid only if the upstream client-type or
14/// language enum width exceeds the current one-byte wire fields.
15#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16pub struct BackendProtocolIdentity {
17    client_type: u8,
18    lang_id: u8,
19}
20
21impl BackendProtocolIdentity {
22    pub const fn new(client_type: u8, lang_id: u8) -> Self {
23        Self {
24            client_type,
25            lang_id,
26        }
27    }
28
29    pub fn try_from_app_lang(client_type: u8, app_lang: i32) -> Result<Self> {
30        let lang_id = u8::try_from(app_lang)
31            .map_err(|_| FutuError::Codec("backend protocol language is out of range".into()))?;
32        Ok(Self::new(client_type, lang_id))
33    }
34
35    pub const fn client_type(self) -> u8 {
36        self.client_type
37    }
38
39    pub const fn lang_id(self) -> u8 {
40        self.lang_id
41    }
42}