2012-03-14 09:57:29 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 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 "Pragma.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "ColorText.h"
|
|
|
|
|
2012-03-15 15:05:33 -06:00
|
|
|
class CPassiveSocket;
|
|
|
|
class CActiveSocket;
|
|
|
|
class CSimpleSocket;
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
#include "CoreProtocol.pb.h"
|
|
|
|
|
|
|
|
namespace DFHack
|
|
|
|
{
|
2012-03-15 01:07:43 -06:00
|
|
|
using dfproto::EmptyMessage;
|
2012-03-15 05:33:19 -06:00
|
|
|
using dfproto::IntMessage;
|
|
|
|
using dfproto::StringMessage;
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
enum command_result
|
|
|
|
{
|
2012-03-16 04:11:46 -06:00
|
|
|
CR_LINK_FAILURE = -3, // RPC call failed due to I/O or protocol error
|
|
|
|
CR_NEEDS_CONSOLE = -2, // Attempt to call interactive command without console
|
2012-03-15 03:01:23 -06:00
|
|
|
CR_NOT_IMPLEMENTED = -1, // Command not implemented, or plugin not loaded
|
|
|
|
CR_OK = 0, // Success
|
|
|
|
CR_FAILURE = 1, // Failure
|
|
|
|
CR_WRONG_USAGE = 2, // Wrong arguments or ui state
|
|
|
|
CR_NOT_FOUND = 3 // Target object not found (for RPC mainly)
|
2012-03-14 09:57:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
int16_t id;
|
|
|
|
int32_t size;
|
|
|
|
};
|
|
|
|
|
2012-03-15 01:07:43 -06:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
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:
|
2012-03-16 04:11:46 -06:00
|
|
|
bool bind(RemoteClient *client, const std::string &name,
|
|
|
|
const std::string &proto = std::string());
|
2012-03-14 09:57:29 -06:00
|
|
|
bool bind(color_ostream &out,
|
|
|
|
RemoteClient *client, const std::string &name,
|
|
|
|
const std::string &proto = std::string());
|
2012-03-16 04:11:46 -06:00
|
|
|
|
|
|
|
bool isValid() { return (id >= 0); }
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class RemoteClient;
|
|
|
|
|
|
|
|
RemoteFunctionBase(const message_type *in, const message_type *out)
|
|
|
|
: RPCFunctionBase(in, out), p_client(NULL), id(-1)
|
|
|
|
{}
|
|
|
|
|
2012-03-16 04:11:46 -06:00
|
|
|
inline color_ostream &default_ostream();
|
2012-03-14 09:57:29 -06:00
|
|
|
command_result execute(color_ostream &out, const message_type *input, message_type *output);
|
|
|
|
|
|
|
|
std::string name, proto;
|
|
|
|
RemoteClient *p_client;
|
|
|
|
int16_t id;
|
|
|
|
};
|
|
|
|
|
2012-03-15 01:07:43 -06:00
|
|
|
template<typename In, typename Out = EmptyMessage>
|
2012-03-14 09:57:29 -06:00
|
|
|
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()) {}
|
|
|
|
|
2012-03-16 04:11:46 -06:00
|
|
|
command_result operator() () {
|
|
|
|
return p_client ? RemoteFunctionBase::execute(default_ostream(), in(), out())
|
|
|
|
: CR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-03-15 04:06:50 -06:00
|
|
|
command_result operator() (color_ostream &stream) {
|
2012-03-15 01:07:43 -06:00
|
|
|
return RemoteFunctionBase::execute(stream, in(), out());
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
2012-03-16 04:11:46 -06:00
|
|
|
command_result operator() (const In *input, Out *output) {
|
|
|
|
return p_client ? RemoteFunctionBase::execute(default_ostream(), input, output)
|
|
|
|
: CR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-03-15 01:07:43 -06:00
|
|
|
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()) {}
|
|
|
|
|
2012-03-16 04:11:46 -06:00
|
|
|
command_result operator() () {
|
|
|
|
return p_client ? RemoteFunctionBase::execute(default_ostream(), in(), out())
|
|
|
|
: CR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-03-15 04:06:50 -06:00
|
|
|
command_result operator() (color_ostream &stream) {
|
2012-03-15 01:07:43 -06:00
|
|
|
return RemoteFunctionBase::execute(stream, in(), out());
|
|
|
|
}
|
2012-03-16 04:11:46 -06:00
|
|
|
command_result operator() (const In *input) {
|
|
|
|
return p_client ? RemoteFunctionBase::execute(default_ostream(), input, out())
|
|
|
|
: CR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-03-15 01:07:43 -06:00
|
|
|
command_result operator() (color_ostream &stream, const In *input) {
|
|
|
|
return RemoteFunctionBase::execute(stream, input, out());
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-15 15:05:33 -06:00
|
|
|
bool readFullBuffer(CSimpleSocket *socket, void *buf, int size);
|
|
|
|
bool sendRemoteMessage(CSimpleSocket *socket, int16_t id,
|
2012-03-15 01:07:43 -06:00
|
|
|
const ::google::protobuf::MessageLite *msg, int *psz = NULL);
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
class DFHACK_EXPORT RemoteClient
|
|
|
|
{
|
|
|
|
friend class RemoteFunctionBase;
|
|
|
|
|
|
|
|
bool bind(color_ostream &out, RemoteFunctionBase *function,
|
|
|
|
const std::string &name, const std::string &proto);
|
|
|
|
|
|
|
|
public:
|
2012-03-16 04:11:46 -06:00
|
|
|
RemoteClient(color_ostream *default_output = NULL);
|
2012-03-14 09:57:29 -06:00
|
|
|
~RemoteClient();
|
|
|
|
|
|
|
|
static int GetDefaultPort();
|
|
|
|
|
2012-03-16 04:11:46 -06:00
|
|
|
color_ostream &default_output() { return *p_default_output; };
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
bool connect(int port = -1);
|
|
|
|
void disconnect();
|
|
|
|
|
2012-03-16 04:11:46 -06:00
|
|
|
command_result run_command(const std::string &cmd, const std::vector<std::string> &args) {
|
|
|
|
return run_command(default_output(), cmd, args);
|
|
|
|
}
|
2012-03-15 01:07:43 -06:00
|
|
|
command_result run_command(color_ostream &out, const std::string &cmd,
|
|
|
|
const std::vector<std::string> &args);
|
|
|
|
|
2012-03-16 04:11:46 -06:00
|
|
|
// For executing multiple calls in rapid succession.
|
|
|
|
// Best used via RemoteSuspender.
|
|
|
|
int suspend_game();
|
|
|
|
int resume_game();
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
private:
|
2012-03-16 04:11:46 -06:00
|
|
|
bool active, delete_output;
|
|
|
|
CActiveSocket *socket;
|
|
|
|
color_ostream *p_default_output;
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
RemoteFunction<dfproto::CoreBindRequest,dfproto::CoreBindReply> bind_call;
|
2012-03-15 01:07:43 -06:00
|
|
|
RemoteFunction<dfproto::CoreRunCommandRequest> runcmd_call;
|
2012-03-16 04:11:46 -06:00
|
|
|
|
|
|
|
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 &proto) {
|
|
|
|
return bind(client->default_output(), client, name, proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemoteSuspender {
|
|
|
|
RemoteClient *client;
|
|
|
|
public:
|
|
|
|
RemoteSuspender(RemoteClient *client) : client(client) {
|
|
|
|
if (!client || client->suspend_game() <= 0) client = NULL;
|
|
|
|
}
|
|
|
|
~RemoteSuspender() {
|
|
|
|
if (client) client->resume_game();
|
|
|
|
}
|
2012-03-14 09:57:29 -06:00
|
|
|
};
|
|
|
|
}
|