-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaddin2.rs
More file actions
173 lines (155 loc) · 4.81 KB
/
addin2.rs
File metadata and controls
173 lines (155 loc) · 4.81 KB
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
use std::{
error::Error,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::{self, JoinHandle},
time::Duration,
};
use addin1c::{
cstr1c, name, AddinResult, CStr1C, CString1C, Connection, MethodInfo, Methods, PropInfo,
SimpleAddin, Variant,
};
pub struct Addin2 {
last_error: Option<Box<dyn Error>>,
prop1: i32,
connection: Option<&'static Connection>,
timer_enabled: Arc<AtomicBool>,
thread_handle: Option<JoinHandle<()>>,
}
impl Addin2 {
pub fn new() -> Addin2 {
Addin2 {
last_error: None,
prop1: 0,
connection: None,
timer_enabled: Arc::new(AtomicBool::new(false)),
thread_handle: None,
}
}
fn last_error(&mut self, value: &mut Variant) -> AddinResult {
match &self.last_error {
Some(err) => value.set_str1c(err.to_string()).map_err(|e| e.into()),
None => value.set_str1c("").map_err(|e| e.into()),
}
}
fn method1(&mut self, param: &mut Variant, ret_value: &mut Variant) -> AddinResult {
let value = param.get_i32()?;
self.prop1 = value;
ret_value.set_i32(value * 2);
Ok(())
}
fn method2(
&mut self,
param1: &mut Variant,
param2: &mut Variant,
ret_value: &mut Variant,
) -> AddinResult {
let value1 = param1.get_i32()?;
let value2 = param2.get_i32()?;
self.prop1 = value1 + value2;
ret_value.set_i32(self.prop1);
Ok(())
}
fn start_timer(&mut self, duration: &mut Variant, _ret_value: &mut Variant) -> AddinResult {
let Some(connection) = self.connection else {
return Err("Нет интерфейса".into());
};
let duration = duration.get_i32()?;
let duration = duration.try_into()?;
if let Some(handle) = self.thread_handle.as_ref() {
if !handle.is_finished() {
return Err("Timer is started".into());
}
}
self.timer_enabled.store(true, Ordering::Relaxed);
let enabled = self.timer_enabled.clone();
connection.set_event_buffer_depth(100);
let handle = thread::spawn(move || {
let mut counter = 0;
loop {
thread::sleep(Duration::from_millis(duration));
if enabled.load(Ordering::Relaxed) {
counter += 1;
connection.external_event(
cstr1c!("Class2"),
cstr1c!("Timer"),
CString1C::new(&format!("{counter}")),
);
} else {
break;
}
}
connection.external_event(cstr1c!("Class2"), cstr1c!("TimerShutdown"), cstr1c!(""));
});
self.thread_handle = Some(handle);
Ok(())
}
fn stop_timer(&mut self, _ret_value: &mut Variant) -> AddinResult {
self.timer_enabled.store(false, Ordering::Relaxed);
Ok(())
}
fn set_prop1(&mut self, value: &Variant) -> AddinResult {
self.prop1 = value.get_i32()?;
Ok(())
}
fn get_prop1(&mut self, value: &mut Variant) -> AddinResult {
value.set_i32(self.prop1);
Ok(())
}
}
impl Drop for Addin2 {
fn drop(&mut self) {
self.timer_enabled.store(false, Ordering::Relaxed);
if let Some(handle) = self.thread_handle.take() {
let _ = handle.join();
}
}
}
impl SimpleAddin for Addin2 {
fn name() -> &'static CStr1C {
name!("Class2")
}
fn init(&mut self, interface: &'static Connection) -> bool {
self.connection = Some(interface);
true
}
fn save_error(&mut self, err: Option<Box<dyn Error>>) {
self.last_error = err;
}
fn methods() -> &'static [MethodInfo<Self>] {
&[
MethodInfo {
name: name!("Method1"),
method: Methods::Method1(Self::method1),
},
MethodInfo {
name: name!("Method2"),
method: Methods::Method2(Self::method2),
},
MethodInfo {
name: name!("StartTimer"),
method: Methods::Method1(Self::start_timer),
},
MethodInfo {
name: name!("StopTimer"),
method: Methods::Method0(Self::stop_timer),
},
]
}
fn properties() -> &'static [PropInfo<Self>] {
&[
PropInfo {
name: name!("Prop1"),
getter: Some(Self::get_prop1),
setter: Some(Self::set_prop1),
},
PropInfo {
name: name!("LastError"),
getter: Some(Self::last_error),
setter: None,
},
]
}
}