|
| 1 | + |
| 2 | +#include "ClientManager.hpp" |
| 3 | + |
| 4 | +#include <cstdio> |
| 5 | +#include <filesystem> |
| 6 | +#include <string_view> |
| 7 | +#include <system_error> |
| 8 | + |
| 9 | +#include "openvic-simulation/GameManager.hpp" |
| 10 | +#include "openvic-simulation/multiplayer/PacketType.hpp" |
| 11 | +#include "openvic-simulation/multiplayer/lowlevel/HostnameAddress.hpp" |
| 12 | +#include "openvic-simulation/multiplayer/lowlevel/NetworkError.hpp" |
| 13 | +#include "openvic-simulation/multiplayer/lowlevel/NetworkSocket.hpp" |
| 14 | +#include "openvic-simulation/multiplayer/lowlevel/PacketBuilder.hpp" |
| 15 | +#include "openvic-simulation/multiplayer/lowlevel/ReliableUdpClient.hpp" |
| 16 | +#include "openvic-simulation/utility/Containers.hpp" |
| 17 | +#include "openvic-simulation/utility/ErrorMacros.hpp" |
| 18 | + |
| 19 | +using namespace OpenVic; |
| 20 | + |
| 21 | +bool ClientManager::connect_to(HostnameAddress const& address, NetworkSocket::port_type port) { |
| 22 | + NetworkError err = client.connect_to(address.resolved_address(), port); |
| 23 | + OV_ERR_FAIL_COND_V(err != NetworkError::OK, false); |
| 24 | + PacketBuilder builder; |
| 25 | + builder.put_back<uint8_t>(1); |
| 26 | + return client.set_packet(builder) == NetworkError::OK; |
| 27 | +} |
| 28 | + |
| 29 | +template<PacketType const& type> |
| 30 | +bool ClientManager::_send_packet_to_host(auto const& argument) { |
| 31 | + PacketBuilder builder; |
| 32 | + builder.put_back(type.packet_id); |
| 33 | + if (!type.create_packet(this, argument, builder)) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + if (!add_packet_to_cache(builder)) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + client.set_packet(builder); |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +bool ClientManager::broadcast_packet(PacketType const& type, std::any const& argument) { |
| 46 | + if (!BaseMultiplayerManager::broadcast_packet(type, argument)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + OV_ERR_FAIL_COND_V(!type.can_client_send, false); |
| 51 | + return _send_packet_to_host<PacketTypes::broadcast_packet>(BroadcastData { type.packet_id, argument }); |
| 52 | +} |
| 53 | + |
| 54 | +bool ClientManager::send_packet(client_id_type client_id, PacketType const& type, std::any const& argument) { |
| 55 | + if (!BaseMultiplayerManager::send_packet(client_id, type, argument)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + OV_ERR_FAIL_COND_V(!type.can_client_send, false); |
| 60 | + return _send_packet_to_host<PacketTypes::retransmit_packet>(RetransmitData { client_id, type.packet_id, argument }); |
| 61 | +} |
| 62 | + |
| 63 | +int64_t ClientManager::poll() { |
| 64 | + int64_t result = client.available_packets(); |
| 65 | + if (result < 1) { |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + PacketSpan span = client.packet_span(); |
| 70 | + if (client_id == INVALID_CLIENT_ID) { |
| 71 | + OV_ERR_FAIL_COND_V(client.get_current_sequence_value() != 0, -1); |
| 72 | + client_id = span.read<decltype(client_id)>(); |
| 73 | + OV_ERR_FAIL_COND_V(client_id == INVALID_CLIENT_ID, -1); |
| 74 | + return poll(); |
| 75 | + } |
| 76 | + |
| 77 | + decltype(PacketType::packet_id) packet_id = span.read<decltype(packet_id)>(); |
| 78 | + OV_ERR_FAIL_COND_V(!PacketType::is_valid_type(packet_id), -1); |
| 79 | + |
| 80 | + PacketType const& packet_type = PacketType::get_type_by_id(packet_id); |
| 81 | + if (!packet_type.process_packet(this, span)) { |
| 82 | + // TODO: packet processing failed |
| 83 | + return -1; |
| 84 | + } |
| 85 | + // TODO: packet was processed |
| 86 | + |
| 87 | + return result; |
| 88 | +} |
| 89 | + |
| 90 | +bool ClientManager::connect_to_resource_server(std::optional<NetworkSocket::port_type> port) { |
| 91 | + if (is_running_as_host()) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + NetworkError err = resource_client.connect_to(client.get_peer_ip(), port.value_or(client.get_peer_port() + 1)); |
| 96 | + OV_ERR_FAIL_COND_V(err != NetworkError::OK, false); |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +void ClientManager::poll_resource_download() { |
| 101 | + if (is_running_as_host()) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (resource_client.poll() != NetworkError::OK || resource_client.get_status() != TcpPacketStream::Status::CONNECTED) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + PacketBuffer buffer = resource_client.packet_buffer(sizeof(size_t)); |
| 110 | + size_t buffer_size = buffer.read<size_t>(); |
| 111 | + buffer.clear(); |
| 112 | + buffer.resize(buffer_size); |
| 113 | + resource_client.get_data(buffer); |
| 114 | + std::string_view str = buffer.read<std::string_view>(); |
| 115 | + std::filesystem::path path = str; |
| 116 | + size_t index = buffer.index(); |
| 117 | + std::span<const uint8_t> data { &buffer.data()[index], buffer.size() - index }; |
| 118 | + |
| 119 | + // TODO: separate file writing into a separate thread? |
| 120 | + std::error_code ec; |
| 121 | + |
| 122 | + OV_ERR_FAIL_COND(!std::filesystem::create_directories(path.parent_path(), ec) || !ec); |
| 123 | + |
| 124 | + { |
| 125 | + std::FILE* file = std::fopen(path.c_str(), "w"); |
| 126 | + OV_ERR_FAIL_COND(file == nullptr); |
| 127 | + |
| 128 | + size_t write_count = std::fwrite(data.data(), sizeof(uint8_t), data.size(), file); |
| 129 | + std::fclose(file); |
| 130 | + OV_ERR_FAIL_COND(write_count != buffer_size); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +bool ClientManager::is_running_as_host() const { |
| 135 | + return game_manager->get_host_manager() != nullptr; |
| 136 | +} |
| 137 | + |
| 138 | +bool ClientManager::add_packet_to_cache(std::span<uint8_t> bytes) { |
| 139 | + OV_ERR_FAIL_COND_V(bytes.size_bytes() > ReliableUdpClient::MAX_PACKET_SIZE, false); |
| 140 | + decltype(packet_cache)::iterator begin = packet_cache.append(bytes.data(), bytes.size_bytes()); |
| 141 | + decltype(packet_cache)::iterator end = packet_cache.end(); |
| 142 | + OV_ERR_FAIL_COND_V(begin == end, false); |
| 143 | + return sequence_to_index.try_emplace(client.get_next_sequence_value(), PacketCacheIndex { begin, end }).second; |
| 144 | +} |
| 145 | + |
| 146 | +memory::vector<uint8_t> ClientManager::get_packet_cache(sequence_type sequence_value) { |
| 147 | + decltype(sequence_to_index)::iterator it = sequence_to_index.find(sequence_value); |
| 148 | + OV_ERR_FAIL_COND_V(it == sequence_to_index.end(), {}); |
| 149 | + return { it.value().begin, it.value().end }; |
| 150 | +} |
| 151 | + |
| 152 | +void ClientManager::remove_from_cache(sequence_type sequence_value) { |
| 153 | + decltype(sequence_to_index)::iterator it = sequence_to_index.find(sequence_value); |
| 154 | + OV_ERR_FAIL_COND(it == sequence_to_index.end()); |
| 155 | + sequence_to_index.unordered_erase(it); |
| 156 | +} |
0 commit comments