04dd91be82
* Add ZeroMQ external submodule * ZeroMQ libzmq building on macOS * Added RPC namespace, settings and logging * Added request queue handling and new classes * Add C++ interface to ZeroMQ * Added start of ZeroMQ RPC Server implementation. * Request construction and callback request handling * Read and write memory implementation * Add ID to request format and send reply * Add RPC setting to macOS UI * Fixed initialization order bug and added exception handling * Working read-write through Python * Update CMakeLists for libzmq to resolve target name conflict on Windows * Platform-specific CMake definitions for Windows/non-Windows * Add comments * Revert "Add RPC setting to macOS UI" * Always run RPC server instead of configurable * Add Python scripting example. Updated .gitignore * Rename member variables to remove trailing underscore * Finally got libzmq external project building on macOS * Add missing dependency during libzmq build * Adding more missing dependencies [skip ci] * Only build what is required from libzmq * Extra length checks on client input * Call InvalidateCacheRange after memory write * Revert MinGW change. Fix clang-format. Improve error handling in request/reply. Allow any length of data read/write in Python. * Re-organized RPC static global state into a proper class. [skip ci] * Make sure libzmq always builds in Release mode * Renamed Request to Packet since Request and Reply are the same thing * Moved request fulfillment out of Packet and into RPCServer * Change request thread from sleep to condition variable * Remove non-blocking polling from ZMQ server code. Receive now blocks and terminates properly without sleeping. This change significantly improves script speed. * Move scripting files to dist/ instead of src/ * C++ code review changes for jroweboy [skip ci] * Python code review changes for jroweboy [skip ci] * Add docstrings and tests to citra.py [skip ci] * Add host OS check for libzmq build * Revert "Add host OS check for libzmq build" * Fixed a hang when emulation is stopped and restarted due to improper destruction order of ZMQ objects [skip ci] * Add scripting directory to archive packaging [skip ci] * Specify C/CXX compiler variables on MinGW build * Only specify compiler on Linux mingw * Use gcc and g++ on Windows mingw * Specify generator for mingw * Don't specify toolchain on windows mingw * Changed citra.py to support Python 3 instead of Python 2 * Fix bug where RPC wouldn't restart after Stop/Start emulation * Added copyright to headers and reorganized includes and forward declarations
78 lines
3 KiB
C++
78 lines
3 KiB
C++
#include "common/common_types.h"
|
|
#include "core/core.h"
|
|
#include "core/rpc/packet.h"
|
|
#include "core/rpc/zmq_server.h"
|
|
|
|
namespace RPC {
|
|
|
|
ZMQServer::ZMQServer(std::function<void(std::unique_ptr<Packet>)> new_request_callback)
|
|
: zmq_context(std::move(std::make_unique<zmq::context_t>(1))),
|
|
zmq_socket(std::move(std::make_unique<zmq::socket_t>(*zmq_context, ZMQ_REP))),
|
|
new_request_callback(std::move(new_request_callback)) {
|
|
// Use a random high port
|
|
// TODO: Make configurable or increment port number on failure
|
|
zmq_socket->bind("tcp://127.0.0.1:45987");
|
|
LOG_INFO(RPC_Server, "ZeroMQ listening on port 45987");
|
|
|
|
worker_thread = std::thread(&ZMQServer::WorkerLoop, this);
|
|
}
|
|
|
|
ZMQServer::~ZMQServer() {
|
|
// Triggering the zmq_context destructor will cancel
|
|
// any blocking calls to zmq_socket->recv()
|
|
running = false;
|
|
zmq_context.reset();
|
|
worker_thread.join();
|
|
|
|
LOG_INFO(RPC_Server, "ZeroMQ stopped");
|
|
}
|
|
|
|
void ZMQServer::WorkerLoop() {
|
|
zmq::message_t request;
|
|
while (running) {
|
|
try {
|
|
if (zmq_socket->recv(&request, 0)) {
|
|
if (request.size() >= MIN_PACKET_SIZE && request.size() <= MAX_PACKET_SIZE) {
|
|
u8* request_buffer = static_cast<u8*>(request.data());
|
|
PacketHeader header;
|
|
std::memcpy(&header, request_buffer, sizeof(header));
|
|
if ((request.size() - MIN_PACKET_SIZE) == header.packet_size) {
|
|
u8* data = request_buffer + MIN_PACKET_SIZE;
|
|
std::function<void(Packet&)> send_reply_callback =
|
|
std::bind(&ZMQServer::SendReply, this, std::placeholders::_1);
|
|
std::unique_ptr<Packet> new_packet =
|
|
std::make_unique<Packet>(header, data, send_reply_callback);
|
|
|
|
// Send the request to the upper layer for handling
|
|
new_request_callback(std::move(new_packet));
|
|
}
|
|
}
|
|
}
|
|
} catch (...) {
|
|
LOG_WARNING(RPC_Server, "Failed to receive data on ZeroMQ socket");
|
|
}
|
|
}
|
|
|
|
// Destroying the socket must be done by this thread.
|
|
zmq_socket.reset();
|
|
}
|
|
|
|
void ZMQServer::SendReply(Packet& reply_packet) {
|
|
if (running) {
|
|
auto reply_buffer =
|
|
std::make_unique<u8[]>(MIN_PACKET_SIZE + reply_packet.GetPacketDataSize());
|
|
auto reply_header = reply_packet.GetHeader();
|
|
|
|
std::memcpy(reply_buffer.get(), &reply_header, sizeof(reply_header));
|
|
std::memcpy(reply_buffer.get() + (4 * sizeof(u32)), reply_packet.GetPacketData().data(),
|
|
reply_packet.GetPacketDataSize());
|
|
|
|
zmq_socket->send(reply_buffer.get(), MIN_PACKET_SIZE + reply_packet.GetPacketDataSize());
|
|
|
|
LOG_INFO(RPC_Server, "Sent reply version({}) id=({}) type=({}) size=({})",
|
|
reply_packet.GetVersion(), reply_packet.GetId(),
|
|
static_cast<u32>(reply_packet.GetPacketType()), reply_packet.GetPacketDataSize());
|
|
}
|
|
}
|
|
|
|
}; // namespace RPC
|