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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::collections::HashMap;
use crate::{
message::{self, Receive, Send},
Error,
};
#[derive(PartialEq, Eq, Hash, Clone)]
struct Method {
pub plugin: String,
pub name: String,
}
impl Method {
fn new(plugin: String, name: String) -> Self {
Method { plugin, name }
}
}
pub struct Channel {
stream: std::net::TcpStream,
bindings: HashMap<Method, i16>,
}
const MAGIC_QUERY: &str = "DFHack?\n";
const MAGIC_REPLY: &str = "DFHack!\n";
const VERSION: i32 = 1;
const BIND_METHOD_ID: i16 = 0;
const RUN_COMMAND_ID: i16 = 1;
impl dfhack_proto::Channel for Channel {
type TError = crate::Error;
fn request<TRequest, TReply>(
&mut self,
plugin: std::string::String,
name: std::string::String,
request: TRequest,
) -> crate::Result<TReply>
where
TRequest: protobuf::Message,
TReply: protobuf::Message,
{
let method = Method::new(plugin, name);
let maybe_id = self.bindings.get(&method);
let id = match maybe_id {
Some(id) => *id,
None => {
let id = self.bind_method::<TRequest, TReply>(&method)?;
self.bindings.insert(method, id);
id
}
};
self.request_raw(id, request)
}
}
impl Channel {
pub(crate) fn connect() -> crate::Result<Self> {
let port = match std::env::var("DFHACK_PORT") {
Ok(p) => p,
Err(_) => "5000".to_string(),
};
Self::connect_to(&format!("127.0.0.1:{}", port))
}
pub(crate) fn connect_to(address: &str) -> crate::Result<Channel> {
log::info!("Connecting to {}", address);
let mut client = Channel {
stream: std::net::TcpStream::connect(address)?,
bindings: HashMap::new(),
};
client.bindings.insert(
Method::new("".to_string(), "BindMethod".to_string()),
BIND_METHOD_ID,
);
client.bindings.insert(
Method::new("".to_string(), "RunCommand".to_string()),
RUN_COMMAND_ID,
);
let handshake_request = message::Handshake::new(MAGIC_QUERY.to_string(), VERSION);
handshake_request.send(&mut client.stream)?;
let handshake_reply = message::Handshake::receive(&mut client.stream)?;
if handshake_reply.magic != MAGIC_REPLY {
return Err(Error::ProtocolError(format!(
"Unexpected magic {}",
handshake_reply.magic
)));
}
if handshake_reply.version != VERSION {
return Err(Error::ProtocolError(format!(
"Unexpected magic version {}",
handshake_reply.version
)));
}
Ok(client)
}
fn request_raw<TIN: protobuf::Message, TOUT: protobuf::Message>(
&mut self,
id: i16,
message: TIN,
) -> crate::Result<TOUT> {
let request = message::Request::new(id, message);
request.send(&mut self.stream)?;
loop {
let reply: message::Reply<TOUT> = message::Reply::receive(&mut self.stream)?;
match reply {
message::Reply::Text(text) => {
for fragment in text.get_fragments() {
println!("{}", fragment.get_text());
}
}
message::Reply::Result(result) => return Ok(result),
message::Reply::Fail(command_result) => {
return Err(Error::RpcError(command_result))
}
}
}
}
fn bind_method<TIN: protobuf::Message, TOUT: protobuf::Message>(
&mut self,
method: &Method,
) -> crate::Result<i16> {
let input_msg = TIN::descriptor_static().full_name();
let output_msg = TOUT::descriptor_static().full_name();
self.bind_method_by_name(&method.plugin, &method.name, input_msg, output_msg)
}
fn bind_method_by_name(
&mut self,
plugin: &str,
method: &str,
input_msg: &str,
output_msg: &str,
) -> crate::Result<i16> {
log::debug!("Binding the method {}:{}", plugin, method);
let mut request = crate::CoreBindRequest::new();
request.set_method(method.to_owned());
request.set_input_msg(input_msg.to_string());
request.set_output_msg(output_msg.to_string());
request.set_plugin(plugin.to_owned());
let reply: crate::CoreBindReply = match self.request_raw(BIND_METHOD_ID, request) {
Ok(reply) => reply,
Err(_) => {
log::error!("Error attempting to bind {}", method);
return Err(Error::FailedToBind(format!(
"{}::{} ({}->{})",
plugin, method, input_msg, output_msg,
)));
}
};
let id = reply.get_assigned_id() as i16;
log::debug!("{}:{} bound to {}", plugin, method, id);
Ok(id)
}
}
impl Drop for Channel {
fn drop(&mut self) {
let quit = message::Quit::new();
let res = quit.send(&mut self.stream);
if let Err(failure) = res {
println!(
"Warning: failed to close the connection to dfhack-remote: {}",
failure
);
}
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "test-with-df")]
mod withdf {
use crate::Error;
#[test]
fn bind() {
use crate::channel::Channel;
let mut channel = Channel::connect().unwrap();
channel
.bind_method_by_name(
"",
"GetVersion",
"dfproto.EmptyMessage",
"dfproto.StringMessage",
)
.unwrap();
}
#[test]
fn bad_bind() {
use crate::channel::Channel;
let mut channel = Channel::connect().unwrap();
let err = channel
.bind_method_by_name(
"",
"GetVersion",
"dfproto.EmptyMessage",
"dfproto.EmptyMessage",
)
.unwrap_err();
assert!(std::matches!(err, Error::FailedToBind(_)));
let err = channel
.bind_method_by_name(
"dorf",
"GetVersion",
"dfproto.StringMessage",
"dfproto.EmptyMessage",
)
.unwrap_err();
assert!(std::matches!(err, Error::FailedToBind(_)));
}
#[test]
#[cfg(feature = "reflection")]
fn bind_all() {
use dfhack_proto::{reflection::StubReflection, stubs::Stubs};
use crate::channel::Channel;
let mut channel = Channel::connect().unwrap();
let methods = Stubs::<Channel>::list_methods();
for method in &methods {
channel
.bind_method_by_name(
&method.plugin_name,
&method.name,
&method.input_type,
&method.output_type,
)
.unwrap();
}
}
}
}