ClientConn

Struct ClientConn 

Source
pub struct ClientConn {
    pub conn_id: u64,
    pub state: ConnState,
    pub aes_key: [u8; 16],
    pub aes_encrypt_enabled: bool,
    pub proto_fmt_type: ProtoFmtType,
    pub last_keepalive: Instant,
    pub keepalive_count: AtomicU32,
    pub tx: Sender<FutuFrame>,
    pub key_id: Option<String>,
    pub scopes: HashSet<Scope>,
}
Expand description

单个客户端连接

Fields§

§conn_id: u64§state: ConnState§aes_key: [u8; 16]§aes_encrypt_enabled: bool

AES 加解密已启用(InitConnect 完成且配置了 RSA 时为 true)

§proto_fmt_type: ProtoFmtType§last_keepalive: Instant§keepalive_count: AtomicU32§tx: Sender<FutuFrame>

发送帧到此连接

§key_id: Option<String>

该连接绑定的 API key id;WS 握手时填,未配 keys.json 时为 None

§scopes: HashSet<Scope>

该连接持有的 scope 集合;空集 = legacy 模式 / TCP 直连,scope 检查放行

Implementations§

Source§

impl ClientConn

Source

pub fn generate_conn_id() -> u64

生成随机连接 ID(与 C++ 的 GetRand_MilliTimeAndU22 对应)

Source

pub fn generate_aes_key() -> [u8; 16]

生成随机 AES key(16 字节 hex 字符串的 ASCII 字节)

Source

pub fn make_frame( &self, proto_id: u32, serial_no: u32, body: Bytes, ) -> FutuFrame

创建发送帧,自动处理 AES 加密

当 aes_encrypt_enabled 为 true 时:

  • SHA1 基于明文计算(FutuFrame::new 自动处理)
  • body 使用 AES-128 ECB 加密
  • header.body_len 更新为密文长度

对应 C++ APIServerCS_Conn::OnSendPacketData 的加密逻辑

Source

pub fn decrypt_body(&self, body: &[u8]) -> Result<Vec<u8>, FutuError>

解密请求 body(如果启用了 AES 加密)

对应 C++ APIServerCS_Conn::OnRecvPacket 的解密逻辑

Source

pub fn handle_init_connect( &mut self, body: &[u8], server_ver: i32, login_user_id: u64, keepalive_interval: i32, rsa_private_key: Option<&str>, ) -> Result<Vec<u8>, FutuError>

处理 InitConnect 请求,返回 InitConnect 响应 body

当配置了 RSA 私钥时:

  • C2S 请求 body 使用 RSA 公钥加密(需要用私钥解密)
  • S2C 响应 body 使用 RSA 公钥加密(客户端用私钥解密)

对应 C++ APIServer::OnRecvInitConnect

Source

pub fn handle_keepalive(&self, body: &[u8]) -> Result<Vec<u8>, FutuError>

处理 KeepAlive 请求

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more