Merge pull request #4609 from wwylele/nuke-zmq

Reimplement scripting over plain UDP
This commit is contained in:
Weiyi Wang 2019-02-08 18:38:21 -05:00 committed by GitHub
commit 687fa0134d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 162 additions and 271 deletions

6
.gitmodules vendored
View file

@ -37,12 +37,6 @@
[submodule "discord-rpc"]
path = externals/discord-rpc
url = https://github.com/discordapp/discord-rpc.git
[submodule "externals/libzmq"]
path = externals/libzmq
url = https://github.com/zeromq/libzmq
[submodule "externals/cppzmq"]
path = externals/cppzmq
url = https://github.com/zeromq/cppzmq
[submodule "cpp-jwt"]
path = externals/cpp-jwt
url = https://github.com/arun11299/cpp-jwt.git

View file

@ -5,7 +5,7 @@ cd /citra
echo 'max_size = 3.0G' > "$HOME/.ccache/ccache.conf"
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE="$(pwd)/../CMakeModules/MinGWCross.cmake" -DUSE_CCACHE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"} -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DUSE_DISCORD_PRESENCE=ON -DENABLE_SCRIPTING=ON
cmake .. -DCMAKE_TOOLCHAIN_FILE="$(pwd)/../CMakeModules/MinGWCross.cmake" -DUSE_CCACHE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"} -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DUSE_DISCORD_PRESENCE=ON
make -j4
echo "Tests skipped"

View file

@ -3,7 +3,7 @@
cd /citra
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/usr/lib/ccache/gcc -DCMAKE_CXX_COMPILER=/usr/lib/ccache/g++ -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"} -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DUSE_DISCORD_PRESENCE=ON -DENABLE_SCRIPTING=ON
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/usr/lib/ccache/gcc -DCMAKE_CXX_COMPILER=/usr/lib/ccache/g++ -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"} -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DUSE_DISCORD_PRESENCE=ON
make -j4
ctest -VV -C Release

View file

@ -7,7 +7,7 @@ export Qt5_DIR=$(brew --prefix)/opt/qt5
export PATH="/usr/local/opt/ccache/libexec:$PATH"
mkdir build && cd build
cmake .. -DCMAKE_OSX_ARCHITECTURES="x86_64;x86_64h" -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"} -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DUSE_DISCORD_PRESENCE=ON -DENABLE_SCRIPTING=ON
cmake .. -DCMAKE_OSX_ARCHITECTURES="x86_64;x86_64h" -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"} -DENABLE_COMPATIBILITY_LIST_DOWNLOAD=ON -DUSE_DISCORD_PRESENCE=ON
make -j4
ctest -VV -C Release

View file

@ -26,7 +26,7 @@ tx --version
cd /citra
mkdir build && cd build
cmake .. -DENABLE_QT_TRANSLATION=ON -DGENERATE_QT_TRANSLATION=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_SDL2=OFF -DENABLE_SCRIPTING=OFF
cmake .. -DENABLE_QT_TRANSLATION=ON -DGENERATE_QT_TRANSLATION=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_SDL2=OFF
make translation
cd ..

View file

@ -22,8 +22,6 @@ option(ENABLE_CUBEB "Enables the cubeb audio backend" ON)
option(USE_DISCORD_PRESENCE "Enables Discord Rich Presence" OFF)
option(ENABLE_SCRIPTING "Enables scripting support" OFF)
if(NOT EXISTS ${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit)
message(STATUS "Copying pre-commit hook")
file(COPY hooks/pre-commit
@ -253,10 +251,6 @@ if (ENABLE_QT)
endif()
endif()
if (ENABLE_SCRIPTING)
add_definitions(-DENABLE_SCRIPTING)
endif()
# Platform-specific library requirements
# ======================================

View file

@ -1,22 +1,22 @@
import zmq
import struct
import random
import enum
import socket
CURRENT_REQUEST_VERSION = 1
MAX_REQUEST_DATA_SIZE = 32
MAX_PACKET_SIZE = 48
class RequestType(enum.IntEnum):
ReadMemory = 1,
WriteMemory = 2
CITRA_PORT = "45987"
CITRA_PORT = 45987
class Citra:
def __init__(self, address="127.0.0.1", port=CITRA_PORT):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
self.socket.connect("tcp://" + address + ":" + port)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.address = address
def is_connected(self):
return self.socket is not None
@ -45,9 +45,9 @@ class Citra:
request_data = struct.pack("II", read_address, temp_read_size)
request, request_id = self._generate_header(RequestType.ReadMemory, len(request_data))
request += request_data
self.socket.send(request)
self.socket.sendto(request, (self.address, CITRA_PORT))
raw_reply = self.socket.recv()
raw_reply = self.socket.recv(MAX_PACKET_SIZE)
reply_data = self._read_and_validate_header(raw_reply, request_id, RequestType.ReadMemory)
if reply_data:
@ -77,9 +77,9 @@ class Citra:
request_data += write_contents[:temp_write_size]
request, request_id = self._generate_header(RequestType.WriteMemory, len(request_data))
request += request_data
self.socket.send(request)
self.socket.sendto(request, (self.address, CITRA_PORT))
raw_reply = self.socket.recv()
raw_reply = self.socket.recv(MAX_PACKET_SIZE)
reply_data = self._read_and_validate_header(raw_reply, request_id, RequestType.WriteMemory)
if None != reply_data:

View file

@ -100,98 +100,3 @@ if (ENABLE_WEB_SERVICE)
add_library(cpp-jwt INTERFACE)
target_include_directories(cpp-jwt INTERFACE ./cpp-jwt/include)
endif()
if (ENABLE_SCRIPTING)
# ZeroMQ
# libzmq includes its own clang-format target, which conflicts with the
# clang-format in Citra if libzmq is added as a subdirectory. An external
# project gets around this issue. Unfortunately, a lot of different
# configuration options are required for each different platform. An
# attempt was made to use CMake variables where possible, but some
# information necessarily had to be repeated. Hopefully there is not
# often a need to change anything.
if (MINGW)
if (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
set(LIBZMQ_MAKE mingw32-make)
set(LIBZMQ_COMPILER "")
set(LIBZMQ_TOOLCHAIN_FILE "")
else()
set(LIBZMQ_MAKE make)
set(LIBZMQ_COMPILER -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER};-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
set(LIBZMQ_TOOLCHAIN_FILE -DCMAKE_TOOLCHAIN_FILE=${PROJECT_SOURCE_DIR}/CMakeModules/MinGWCross.cmake)
endif()
ExternalProject_Add(libzmq-external
SOURCE_DIR ./libzmq
CMAKE_ARGS -DWITH_PERF_TOOL=OFF;-DZMQ_BUILD_TESTS=OFF;-DENABLE_CPACK=OFF;-DCMAKE_MAKE_PROGRAM=${LIBZMQ_MAKE};-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE};${LIBZMQ_TOOLCHAIN_FILE};${LIBZMQ_COMPILER}
BUILD_COMMAND cmake --build ${CMAKE_CURRENT_BINARY_DIR}/libzmq-external-prefix/src/libzmq-external-build --target libzmq-static --config ${CMAKE_BUILD_TYPE}
GIT_REPOSITORY https://github.com/zeromq/libzmq
GIT_TAG v4.2.5
INSTALL_COMMAND "")
else()
if (MSVC)
set(LIBZMQ_COMPILER_FLAGS -DCMAKE_C_FLAGS=/GL-;-DCMAKE_CXX_FLAGS=/GL-)
else()
set(LIBZMQ_COMPILER_FLAGS "")
endif()
ExternalProject_Add(libzmq-external
SOURCE_DIR ./libzmq
CMAKE_ARGS -DCMAKE_MACOSX_RPATH=1;-DCMAKE_OSX_ARCHITECTURES=x86_64;-DWITH_PERF_TOOL=OFF;-DZMQ_BUILD_TESTS=OFF;-DENABLE_CPACK=OFF;-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE};${LIBZMQ_COMPILER_FLAGS}
BUILD_COMMAND cmake --build ${CMAKE_CURRENT_BINARY_DIR}/libzmq-external-prefix/src/libzmq-external-build --target libzmq-static --config ${CMAKE_BUILD_TYPE}
GIT_REPOSITORY https://github.com/zeromq/libzmq
GIT_TAG v4.2.5
INSTALL_COMMAND "")
endif()
set(LIBZMQ_DIR ${CMAKE_CURRENT_BINARY_DIR}/libzmq-external-prefix/src/libzmq-external-build/lib)
# On macOS, we need to build a fat static library containing both x86_64 and x86_64h, since macOS
# targets specify two architectures in their link command line ("-arch x86_64 -arch x86_64h").
if (APPLE)
ExternalProject_Add(libzmq-external-h
SOURCE_DIR ./libzmq-h
CMAKE_ARGS -DCMAKE_MACOSX_RPATH=1;-DCMAKE_OSX_ARCHITECTURES=x86_64h;-DWITH_PERF_TOOL=OFF;-DZMQ_BUILD_TESTS=OFF;-DENABLE_CPACK=OFF;-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
BUILD_COMMAND cmake --build ${CMAKE_CURRENT_BINARY_DIR}/libzmq-external-h-prefix/src/libzmq-external-h-build --target libzmq-static --config ${CMAKE_BUILD_TYPE}
GIT_REPOSITORY https://github.com/zeromq/libzmq
GIT_TAG v4.2.5
INSTALL_COMMAND "")
set(LIBZMQ_H_DIR ${CMAKE_CURRENT_BINARY_DIR}/libzmq-external-h-prefix/src/libzmq-external-h-build/lib)
add_library(libzmq-external-imported STATIC IMPORTED GLOBAL)
add_library(libzmq-external-imported-h STATIC IMPORTED GLOBAL)
add_dependencies(libzmq-external-imported libzmq-external)
add_dependencies(libzmq-external-imported-h libzmq-external-h)
else()
add_library(libzmq STATIC IMPORTED GLOBAL)
add_dependencies(libzmq libzmq-external)
endif()
# Set up the imported target properties
if (MSVC)
set_target_properties(libzmq PROPERTIES IMPORTED_LOCATION ${LIBZMQ_DIR}/${CMAKE_BUILD_TYPE}/libzmq-v141-mt-s-4_2_5${CMAKE_STATIC_LIBRARY_SUFFIX})
set_target_properties(libzmq PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES iphlpapi${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
if (APPLE)
set_target_properties(libzmq-external-imported PROPERTIES IMPORTED_LOCATION ${LIBZMQ_DIR}/libzmq${CMAKE_STATIC_LIBRARY_SUFFIX})
set_target_properties(libzmq-external-imported-h PROPERTIES IMPORTED_LOCATION ${LIBZMQ_H_DIR}/libzmq${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
set_target_properties(libzmq PROPERTIES IMPORTED_LOCATION ${LIBZMQ_DIR}/libzmq${CMAKE_STATIC_LIBRARY_SUFFIX})
if(MINGW)
set_target_properties(libzmq PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES "ws2_32${CMAKE_STATIC_LIBRARY_SUFFIX};iphlpapi${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()
endif()
endif()
# On macOS, create the combined target
if (APPLE)
set(LIBZMQ_COMBINED_OUTPUT ${LIBZMQ_DIR}/libzmq_combined${CMAKE_STATIC_LIBRARY_SUFFIX})
add_custom_target(libzmq-combined COMMAND lipo -create ${LIBZMQ_DIR}/libzmq${CMAKE_STATIC_LIBRARY_SUFFIX} ${LIBZMQ_H_DIR}/libzmq${CMAKE_STATIC_LIBRARY_SUFFIX} -o ${LIBZMQ_COMBINED_OUTPUT}
BYPRODUCTS ${LIBZMQ_COMBINED_OUTPUT})
add_dependencies(libzmq-combined libzmq-external-imported libzmq-external-imported-h)
add_library(libzmq STATIC IMPORTED GLOBAL)
set_target_properties(libzmq PROPERTIES IMPORTED_LOCATION ${LIBZMQ_COMBINED_OUTPUT})
add_dependencies(libzmq libzmq-combined)
endif()
# C interface to ZeroMQ
add_library(libzmq-headers INTERFACE)
target_include_directories(libzmq-headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/libzmq/include)
# C++ interface to ZeroMQ
add_library(cppzmq-headers INTERFACE)
target_include_directories(cppzmq-headers INTERFACE ./cppzmq)
add_dependencies(cppzmq-headers libzmq)
endif()

1
externals/cppzmq vendored

@ -1 +0,0 @@
Subproject commit 6aa3ab686e916cb0e62df7fa7d12e0b13ae9fae6

1
externals/libzmq vendored

@ -1 +0,0 @@
Subproject commit d062edd8c142384792955796329baf1e5a3377cd

View file

@ -423,6 +423,14 @@ add_library(core STATIC
movie.h
perf_stats.cpp
perf_stats.h
rpc/packet.cpp
rpc/packet.h
rpc/rpc_server.cpp
rpc/rpc_server.h
rpc/server.cpp
rpc/server.h
rpc/udp_server.cpp
rpc/udp_server.h
settings.cpp
settings.h
telemetry_session.cpp
@ -431,18 +439,6 @@ add_library(core STATIC
tracer/recorder.cpp
tracer/recorder.h
)
if (ENABLE_SCRIPTING)
target_sources(core PRIVATE
rpc/packet.cpp
rpc/packet.h
rpc/rpc_server.cpp
rpc/rpc_server.h
rpc/server.cpp
rpc/server.h
rpc/zmq_server.cpp
rpc/zmq_server.h
)
endif()
create_target_directory_groups(core)
@ -462,7 +458,3 @@ if (ARCHITECTURE_x86_64)
)
target_link_libraries(core PRIVATE dynarmic)
endif()
if (ENABLE_SCRIPTING)
target_link_libraries(core PUBLIC libzmq-headers cppzmq-headers libzmq)
endif()

View file

@ -27,9 +27,7 @@
#include "core/hw/hw.h"
#include "core/loader/loader.h"
#include "core/movie.h"
#ifdef ENABLE_SCRIPTING
#include "core/rpc/rpc_server.h"
#endif
#include "core/settings.h"
#include "network/network.h"
#include "video_core/video_core.h"
@ -201,9 +199,7 @@ System::ResultStatus System::Init(EmuWindow& emu_window, u32 system_mode) {
telemetry_session = std::make_unique<Core::TelemetrySession>();
#ifdef ENABLE_SCRIPTING
rpc_server = std::make_unique<RPC::RPCServer>();
#endif
service_manager = std::make_shared<Service::SM::ServiceManager>(*this);
archive_manager = std::make_unique<Service::FS::ArchiveManager>(*this);
@ -294,9 +290,7 @@ void System::Shutdown() {
kernel.reset();
HW::Shutdown();
telemetry_session.reset();
#ifdef ENABLE_SCRIPTING
rpc_server.reset();
#endif
cheat_engine.reset();
service_manager.reset();
dsp_core.reset();

View file

@ -24,11 +24,9 @@ namespace AudioCore {
class DspInterface;
}
#ifdef ENABLE_SCRIPTING
namespace RPC {
class RPCServer;
}
#endif
namespace Service {
namespace SM {
@ -267,10 +265,8 @@ private:
/// Cheats manager
std::unique_ptr<Cheats::CheatEngine> cheat_engine;
#ifdef ENABLE_SCRIPTING
/// RPC Server for scripting support
std::unique_ptr<RPC::RPCServer> rpc_server;
#endif
std::unique_ptr<Service::FS::ArchiveManager> archive_manager;

View file

@ -13,6 +13,9 @@
namespace RPC {
class Packet;
struct PacketHeader;
class RPCServer {
public:
RPCServer();

View file

@ -1,27 +1,30 @@
#include <functional>
#include "core/core.h"
#include "core/rpc/packet.h"
#include "core/rpc/rpc_server.h"
#include "core/rpc/server.h"
#include "core/rpc/udp_server.h"
namespace RPC {
Server::Server(RPCServer& rpc_server) : rpc_server(rpc_server) {}
Server::~Server() = default;
void Server::Start() {
const auto callback = [this](std::unique_ptr<RPC::Packet> new_request) {
const auto callback = [this](std::unique_ptr<Packet> new_request) {
NewRequestCallback(std::move(new_request));
};
try {
zmq_server = std::make_unique<ZMQServer>(callback);
udp_server = std::make_unique<UDPServer>(callback);
} catch (...) {
LOG_ERROR(RPC_Server, "Error starting ZeroMQ server");
LOG_ERROR(RPC_Server, "Error starting UDP server");
}
}
void Server::Stop() {
zmq_server.reset();
udp_server.reset();
}
void Server::NewRequestCallback(std::unique_ptr<RPC::Packet> new_request) {

View file

@ -4,24 +4,25 @@
#pragma once
#include "core/rpc/packet.h"
#include "core/rpc/zmq_server.h"
#include <memory>
namespace RPC {
class RPCServer;
class ZMQServer;
class UDPServer;
class Packet;
class Server {
public:
Server(RPCServer& rpc_server);
~Server();
void Start();
void Stop();
void NewRequestCallback(std::unique_ptr<RPC::Packet> new_request);
void NewRequestCallback(std::unique_ptr<Packet> new_request);
private:
RPCServer& rpc_server;
std::unique_ptr<ZMQServer> zmq_server;
std::unique_ptr<UDPServer> udp_server;
};
} // namespace RPC

100
src/core/rpc/udp_server.cpp Normal file
View file

@ -0,0 +1,100 @@
// Copyright 2019 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <thread>
#include <boost/asio.hpp>
#include "common/common_types.h"
#include "common/logging/log.h"
#include "core/rpc/packet.h"
#include "core/rpc/udp_server.h"
namespace RPC {
class UDPServer::Impl {
public:
explicit Impl(std::function<void(std::unique_ptr<Packet>)> new_request_callback)
// Use a random high port
// TODO: Make configurable or increment port number on failure
: socket(io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 45987)),
new_request_callback(std::move(new_request_callback)) {
StartReceive();
worker_thread = std::thread([this] {
io_context.run();
this->new_request_callback(nullptr);
});
}
~Impl() {
io_context.stop();
worker_thread.join();
}
private:
void StartReceive() {
socket.async_receive_from(boost::asio::buffer(request_buffer), remote_endpoint,
[this](const boost::system::error_code& error, std::size_t size) {
HandleReceive(error, size);
});
}
void HandleReceive(const boost::system::error_code& error, std::size_t size) {
if (error) {
LOG_WARNING(RPC_Server, "Failed to receive data on UDP socket: {}", error.message());
} else if (size >= MIN_PACKET_SIZE && size <= MAX_PACKET_SIZE) {
PacketHeader header;
std::memcpy(&header, request_buffer.data(), sizeof(header));
if ((size - MIN_PACKET_SIZE) == header.packet_size) {
u8* data = request_buffer.data() + MIN_PACKET_SIZE;
std::function<void(Packet&)> send_reply_callback =
std::bind(&Impl::SendReply, this, remote_endpoint, 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));
}
} else {
LOG_WARNING(RPC_Server, "Received message with wrong size: {}", size);
}
StartReceive();
}
void SendReply(boost::asio::ip::udp::endpoint endpoint, Packet& reply_packet) {
std::vector<u8> reply_buffer(MIN_PACKET_SIZE + reply_packet.GetPacketDataSize());
auto reply_header = reply_packet.GetHeader();
std::memcpy(reply_buffer.data(), &reply_header, sizeof(reply_header));
std::memcpy(reply_buffer.data() + (4 * sizeof(u32)), reply_packet.GetPacketData().data(),
reply_packet.GetPacketDataSize());
boost::system::error_code error;
socket.send_to(boost::asio::buffer(reply_buffer), endpoint, 0, error);
if (error) {
LOG_WARNING(RPC_Server, "Failed to send reply: {}", error.message());
} else {
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());
}
}
std::thread worker_thread;
boost::asio::io_context io_context;
boost::asio::ip::udp::socket socket;
std::array<u8, MAX_PACKET_SIZE> request_buffer;
boost::asio::ip::udp::endpoint remote_endpoint;
std::function<void(std::unique_ptr<Packet>)> new_request_callback;
};
UDPServer::UDPServer(std::function<void(std::unique_ptr<Packet>)> new_request_callback)
: impl(std::make_unique<Impl>(new_request_callback)) {}
UDPServer::~UDPServer() = default;
} // namespace RPC

24
src/core/rpc/udp_server.h Normal file
View file

@ -0,0 +1,24 @@
// Copyright 2019 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <functional>
#include <memory>
namespace RPC {
class Packet;
class UDPServer {
public:
explicit UDPServer(std::function<void(std::unique_ptr<Packet>)> new_request_callback);
~UDPServer();
private:
class Impl;
std::unique_ptr<Impl> impl;
};
} // namespace RPC

View file

@ -1,79 +0,0 @@
#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");
}
}
std::unique_ptr<Packet> end_packet = nullptr;
new_request_callback(std::move(end_packet));
// 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

View file

@ -1,34 +0,0 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <functional>
#include <thread>
#define ZMQ_STATIC
#include <zmq.hpp>
namespace RPC {
class Packet;
class ZMQServer {
public:
explicit ZMQServer(std::function<void(std::unique_ptr<Packet>)> new_request_callback);
~ZMQServer();
private:
void WorkerLoop();
void SendReply(Packet& request);
std::thread worker_thread;
std::atomic_bool running = true;
std::unique_ptr<zmq::context_t> zmq_context;
std::unique_ptr<zmq::socket_t> zmq_socket;
std::function<void(std::unique_ptr<Packet>)> new_request_callback;
};
} // namespace RPC