-
Notifications
You must be signed in to change notification settings - Fork 18
/
tdapi.rs
194 lines (161 loc) · 5.46 KB
/
tdapi.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use ctp_rs::*;
use std::os::raw::*;
use std::ffi::{CStr, CString};
use std::sync::mpsc::{self, Sender, SyncSender, Receiver};
use log::*;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Resume {
Restart = THOST_TE_RESUME_TYPE_THOST_TERT_RESTART as _,
Resume = THOST_TE_RESUME_TYPE_THOST_TERT_RESUME as _,
Quick = THOST_TE_RESUME_TYPE_THOST_TERT_QUICK as _,
}
#[derive(Debug, Clone, Default)]
pub struct Config {
flowpath: String,
front_addr: String,
nm_addr: String,
user_info: String,
product_info: String,
auth_code: String,
app_id: String,
public_resume: Resume,
private_resume: Resume,
broker_id: String,
user_id: String,
password: String,
qry_freq: i32,
}
pub struct TDApi {
api: Rust_CThostFtdcTraderApi,
spi: Option<*mut Rust_CThostFtdcTraderSpi>,
rx: Option<Receiver<String>>,
pub(crate) config: Config,
}
struct Spi {
tx: SyncSender<String>
}
impl Rust_CThostFtdcTraderSpi_Trait for Spi {
fn on_front_connected(&mut self) {
debug!("connected.");
self.tx.send("connected".into()).unwrap();
}
}
impl TDApi {
pub fn get_version() -> String {
let cs = unsafe { CStr::from_ptr(CThostFtdcTraderApi::GetApiVersion()) };
cs.to_string_lossy().into()
}
pub fn new(config: &Config) -> Self {
let cs = std::ffi::CString::new(config.flowpath.as_bytes()).unwrap();
let api = unsafe {
Rust_CThostFtdcTraderApi::new(CThostFtdcTraderApi_CreateFtdcTraderApi(cs.as_ptr()))
};
Self { api, spi: None, config: config.clone(), rx: None }
}
/// destory `self.spi`, which created by `TDApi`
fn drop_spi(spi: *mut Rust_CThostFtdcTraderSpi) {
let mut spi = unsafe { Box::from_raw(spi) };
unsafe { spi.destruct(); }
}
fn register<S: Rust_CThostFtdcTraderSpi_Trait>(&mut self, spi: S) {
if let Some(spi) = self.spi.take() {
debug!("des old registered spi");
Self::drop_spi(spi);
}
let spi: Box<Box<dyn Rust_CThostFtdcTraderSpi_Trait>> = Box::new(Box::new(spi));
let ptr = Box::into_raw(spi) as *mut _ as *mut c_void;
// Here is the trick part:
//
// 1. `RegisterSpi` recv a `CThostFtdcTraderSpi*`, which can be cast from
// Rust_CThostFtdcTraderSpi, because `Rust_CThostFtdcTraderSpi` inherit from `CThostFtdcTraderSpi`
// in wrapper.hpp.
//
// 2. What `RegisterSpi` recved must be valid afterwards. We can use pin or boxed pointer to achieve
// that. What we can't do is code below, which use `Rust_CThostFtdcTraderSpi` as spi instead of a
// pointer `*mut Rust_CThostFtdcTraderSpi`:
//
// ```rust
// let mut spi_stub = unsafe { Rust_CThostFtdcTraderSpi::new(ptr) } ;
// let spi: *mut Rust_CThostFtdcTraderSpi = &mut spi_stub;
//
// unsafe { self.api.RegisterSpi(spi as _); }
//
// self.spi = Some(spi_stub);
// ```
//
// but we can do this:
//
// ```rust
// let mut spi_stub = unsafe { Rust_CThostFtdcTraderSpi::new(ptr) } ;
// self.spi = Some(spi_stub);
// let spi: *mut Rust_CThostFtdcTraderSpi = self.spi.as_mut().unwrap() as *mut _;
//
// unsafe { self.api.RegisterSpi(spi as _); }
// ```
//
// Because original code move spi_stub into self.spi after RegisterSpi, which cause original
// address invalid.
let spi_stub = unsafe { Rust_CThostFtdcTraderSpi::new(ptr) } ;
let spi: *mut Rust_CThostFtdcTraderSpi = Box::into_raw(Box::new(spi_stub));
unsafe { self.api.RegisterSpi(spi as _); }
self.spi = Some(spi);
}
pub fn req_init(&mut self) -> Result<(), String> {
let (tx, rx) = mpsc::sync_channel(1024);
self.register(Spi { tx });
self.rx = Some(rx);
debug!("start api...");
if self.config.front_addr.len() > 0 {
debug!("cs is: {}", self.config.front_addr);
let cs = CString::new(self.config.front_addr.as_bytes()).unwrap();
unsafe { self.api.RegisterFront(cs.as_ptr() as *mut _); }
}
unsafe {
self.api.SubscribePrivateTopic(self.config.private_resume as _);
self.api.SubscribePublicTopic(self.config.public_resume as _);
}
unsafe { self.api.Init(); }
Ok(())
}
}
impl Default for Resume {
fn default() -> Self {
Self::Quick
}
}
impl Drop for TDApi {
fn drop(&mut self) {
debug!("drop api");
unsafe { self.api.destruct(); }
if let Some(spi) = self.spi {
debug!("drop spi");
Self::drop_spi(spi);
}
}
}
pub fn main() {
dotenv::dotenv().ok();
env_logger::init();
eprintln!("api version: {}", TDApi::get_version());
let mut tdapi = TDApi::new(&Config {
flowpath: "".into(),
nm_addr: "".into(),
user_info: "".into(),
product_info: "".into(),
public_resume: Resume::Quick,
private_resume: Resume::Quick,
// simnow - full
front_addr: "tcp://180.168.146.187:10101".into(),
broker_id: "9999".into(),
auth_code: "".into(),
app_id: "".into(),
..Default::default()
});
tdapi.req_init().unwrap();
eprintln!("mk api success");
if let Some(ref mut rx) = tdapi.rx {
while let Ok(event) = rx.recv() {
debug!("Got event: {}", event);
}
}
}