Skip to content
Navigation Menu
{{ message }}
forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteClient.h
More file actions
275 lines (224 loc) · 9.24 KB
/
Copy pathRemoteClient.h
File metadata and controls
275 lines (224 loc) · 9.24 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once
#include "Export.h"
#include "ColorText.h"
#include "CoreDefs.h"
class CPassiveSocket;
class CActiveSocket;
class CSimpleSocket;
#include "CoreProtocol.pb.h"
namespace DFHack
{
using dfproto::EmptyMessage;
using dfproto::IntMessage;
using dfproto::StringMessage;
enum DFHackReplyCode : int16_t {
RPC_REPLY_RESULT = -1,
RPC_REPLY_FAIL = -2,
RPC_REPLY_TEXT = -3,
RPC_REQUEST_QUIT = -4
};
struct RPCHandshakeHeader {
char magic[8];
int version;
static const char REQUEST_MAGIC[9];
static const char RESPONSE_MAGIC[9];
};
struct RPCMessageHeader {
static const int MAX_MESSAGE_SIZE = 64*1048576;
int16_t id;
int32_t size;
};
/* Protocol description:
*
* 1. Handshake
*
* Client initiates connection by sending the handshake
* request header. The server responds with the response
* magic. Currently both versions must be 1.
*
* 2. Interaction
*
* Requests are done by exchanging messages between the
* client and the server. Messages consist of a serialized
* protobuf message preceeded by RPCMessageHeader. The size
* field specifies the length of the protobuf part.
*
* NOTE: As a special exception, RPC_REPLY_FAIL uses the size
* field to hold the error code directly.
*
* Every callable function is assigned a non-negative id by
* the server. Id 0 is reserved for BindMethod, which can be
* used to request any other id by function name. Id 1 is
* RunCommand, used to call console commands remotely.
*
* The client initiates every call by sending a message with
* appropriate function id and input arguments. The server
* responds with zero or more RPC_REPLY_TEXT:CoreTextNotification
* messages, followed by RPC_REPLY_RESULT containing the output
* of the function if it succeeded, or RPC_REPLY_FAIL with the
* error code if it did not.
*
* 3. Disconnect
*
* The client terminates the connection by sending an
* RPC_REQUEST_QUIT header with zero size and immediately
* closing the socket.
*/
class DFHACK_EXPORT RemoteClient;
class DFHACK_EXPORT RPCFunctionBase {
public:
typedef ::google::protobuf::MessageLite message_type;
const message_type *const p_in_template;
const message_type *const p_out_template;
message_type *make_in() const {
return p_in_template->New();
}
message_type *in() {
if (!p_in) p_in = make_in();
return p_in;
}
message_type *make_out() const {
return p_out_template->New();
}
message_type *out() {
if (!p_out) p_out = make_out();
return p_out;
}
void reset(bool free = false);
protected:
RPCFunctionBase(const message_type *in, const message_type *out)
: p_in_template(in), p_out_template(out), p_in(NULL), p_out(NULL)
{}
~RPCFunctionBase() { delete p_in; delete p_out; }
message_type *p_in, *p_out;
};
class DFHACK_EXPORT RemoteFunctionBase : public RPCFunctionBase {
public:
bool bind(RemoteClient *client, const std::string &name,
const std::string &plugin = std::string());
bool bind(color_ostream &out,
RemoteClient *client, const std::string &name,
const std::string &plugin = std::string());
bool isValid() { return (id >= 0); }
protected:
friend class RemoteClient;
RemoteFunctionBase(const message_type *in, const message_type *out)
: RPCFunctionBase(in, out), p_client(NULL), id(-1)
{}
inline color_ostream &default_ostream();
command_result execute(color_ostream &out, const message_type *input, message_type *output);
std::string name, plugin;
RemoteClient *p_client;
int16_t id;
};
template<typename In, typename Out = EmptyMessage>
class RemoteFunction : public RemoteFunctionBase {
public:
In *make_in() const { return static_cast<In*>(RemoteFunctionBase::make_in()); }
In *in() { return static_cast<In*>(RemoteFunctionBase::in()); }
Out *make_out() const { return static_cast<Out*>(RemoteFunctionBase::make_out()); }
Out *out() { return static_cast<Out*>(RemoteFunctionBase::out()); }
RemoteFunction() : RemoteFunctionBase(&In::default_instance(), &Out::default_instance()) {}
command_result operator() () {
return p_client ? RemoteFunctionBase::execute(default_ostream(), in(), out())
: CR_NOT_IMPLEMENTED;
}
command_result operator() (color_ostream &stream) {
return RemoteFunctionBase::execute(stream, in(), out());
}
command_result operator() (const In *input, Out *output) {
return p_client ? RemoteFunctionBase::execute(default_ostream(), input, output)
: CR_NOT_IMPLEMENTED;
}
command_result operator() (color_ostream &stream, const In *input, Out *output) {
return RemoteFunctionBase::execute(stream, input, output);
}
};
template<typename In>
class RemoteFunction<In,EmptyMessage> : public RemoteFunctionBase {
public:
In *make_in() const { return static_cast<In*>(RemoteFunctionBase::make_in()); }
In *in() { return static_cast<In*>(RemoteFunctionBase::in()); }
RemoteFunction() : RemoteFunctionBase(&In::default_instance(), &EmptyMessage::default_instance()) {}
command_result operator() () {
return p_client ? RemoteFunctionBase::execute(default_ostream(), in(), out())
: CR_NOT_IMPLEMENTED;
}
command_result operator() (color_ostream &stream) {
return RemoteFunctionBase::execute(stream, in(), out());
}
command_result operator() (const In *input) {
return p_client ? RemoteFunctionBase::execute(default_ostream(), input, out())
: CR_NOT_IMPLEMENTED;
}
command_result operator() (color_ostream &stream, const In *input) {
return RemoteFunctionBase::execute(stream, input, out());
}
};
class DFHACK_EXPORT RemoteClient
{
friend class RemoteFunctionBase;
bool bind(color_ostream &out, RemoteFunctionBase *function,
const std::string &name, const std::string &plugin);
public:
RemoteClient(color_ostream *default_output = NULL);
~RemoteClient();
static constexpr int DEFAULT_PORT = 5000;
static int GetDefaultPort();
color_ostream &default_output() { return *p_default_output; };
bool connect(int port = -1);
void disconnect();
command_result run_command(const std::string &cmd, const std::vector<std::string> &args) {
return run_command(default_output(), cmd, args);
}
command_result run_command(color_ostream &out, const std::string &cmd,
const std::vector<std::string> &args);
// For executing multiple calls in rapid succession.
// Best used via RemoteSuspender.
int suspend_game();
int resume_game();
private:
bool active, delete_output;
CActiveSocket *socket;
color_ostream *p_default_output;
RemoteFunction<dfproto::CoreBindRequest,dfproto::CoreBindReply> bind_call;
RemoteFunction<dfproto::CoreRunCommandRequest> runcmd_call;
bool suspend_ready;
RemoteFunction<EmptyMessage, IntMessage> suspend_call, resume_call;
};
inline color_ostream &RemoteFunctionBase::default_ostream() {
return p_client->default_output();
}
inline bool RemoteFunctionBase::bind(RemoteClient *client, const std::string &name,
const std::string &plugin) {
return bind(client->default_output(), client, name, plugin);
}
class RemoteSuspender {
RemoteClient *client;
public:
RemoteSuspender(RemoteClient *client) : client(client) {
if (!client || client->suspend_game() <= 0) client = NULL;
}
~RemoteSuspender() {
if (client) client->resume_game();
}
};
}
You can’t perform that action at this time.
