Lime3DS/src/core/rpc/server.cpp
Weiyi Wang 870579d38f rpc: send ending packet in Server instead of UDPServer
udp_server might not be created due to error (occupied port etc.), in which case its destructor and thread-ending call chain will not be excuted in Server::Stop. However, the ending packet still need to be send no matter udp is on or not, so move it to Server::Stop
2019-03-04 11:05:07 -05:00

43 lines
1.2 KiB
C++

#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<Packet> new_request) {
NewRequestCallback(std::move(new_request));
};
try {
udp_server = std::make_unique<UDPServer>(callback);
} catch (...) {
LOG_ERROR(RPC_Server, "Error starting UDP server");
}
}
void Server::Stop() {
udp_server.reset();
NewRequestCallback(nullptr); // Notify the RPC server to end
}
void Server::NewRequestCallback(std::unique_ptr<RPC::Packet> new_request) {
if (new_request) {
LOG_INFO(RPC_Server, "Received request version={} id={} type={} size={}",
new_request->GetVersion(), new_request->GetId(),
static_cast<u32>(new_request->GetPacketType()), new_request->GetPacketDataSize());
} else {
LOG_INFO(RPC_Server, "Received end packet");
}
rpc_server.QueueRequest(std::move(new_request));
}
}; // namespace RPC