Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef NETFILETRANSFER_H 19 : #define NETFILETRANSFER_H 20 : 21 : #include "lib/alignment.h" 22 : #include "lib/status.h" 23 : #include "ps/Future.h" 24 : 25 : #include <cstddef> 26 : #include <functional> 27 : #include <map> 28 : #include <string> 29 : #include <unordered_map> 30 : 31 : class CFileTransferAckMessage; 32 : class CFileTransferDataMessage; 33 : class CFileTransferResponseMessage; 34 : class CNetMessage; 35 : 36 : // Assume this is sufficiently less than MTU that packets won't get 37 : // fragmented or dropped. 38 : static const size_t DEFAULT_FILE_TRANSFER_PACKET_SIZE = 1024; 39 : 40 : // To improve performance without flooding ENet's internal buffers, 41 : // maintain a small number of in-flight packets. 42 : // Pick numbers so that with e.g. 200ms round-trip latency 43 : // we can hopefully get windowSize*packetSize*1000/200 = 160KB/s bandwidth 44 : static const size_t DEFAULT_FILE_TRANSFER_WINDOW_SIZE = 32; 45 : 46 : // Some arbitrary limit to make it slightly harder to use up all of someone's RAM 47 : static const size_t MAX_FILE_TRANSFER_SIZE = 8*MiB; 48 : 49 : /** 50 : * Handles transferring files between clients and servers. 51 : */ 52 : class CNetFileTransferer 53 : { 54 : public: 55 : enum class RequestType 56 : { 57 : LOADGAME, 58 : REJOIN 59 : }; 60 : 61 : template<typename Session> 62 4 : CNetFileTransferer(Session& session) : 63 4 : m_SendMessage{std::bind_front(&Session::SendMessage, std::ref(session))} 64 : { 65 4 : } 66 : 67 : /** 68 : * Should be called when a message is received from the network. 69 : * Returns INFO::SKIPPED if the message is not one that this class handles. 70 : * Returns INFO::OK if the message is handled successfully, 71 : * or ERR::FAIL if handled unsuccessfully. 72 : */ 73 : Status HandleMessageReceive(const CNetMessage& message); 74 : 75 : /** 76 : * Registers a file-receiving task. 77 : */ 78 : void StartTask(RequestType requestType, std::function<void(std::string)> task); 79 : 80 : /** 81 : * Registers data to be sent in response to a request. 82 : * (Callers are expected to have their own mechanism for receiving 83 : * requests and deciding what to respond with.) 84 : */ 85 : void StartResponse(std::uint32_t requestID, const std::string& data); 86 : 87 : /** 88 : * Call frequently (e.g. once per frame) to trigger any necessary 89 : * packet processing. 90 : */ 91 : void Poll(); 92 : 93 : private: 94 : Status OnFileTransferResponse(const CFileTransferResponseMessage& message); 95 : Status OnFileTransferData(const CFileTransferDataMessage& message); 96 : Status OnFileTransferAck(const CFileTransferAckMessage& message); 97 : 98 : /** 99 : * Asynchronous file-sending task. 100 : */ 101 1 : struct CNetFileSendTask 102 : { 103 : std::uint32_t requestID; 104 : std::string buffer; 105 : size_t offset; 106 : size_t maxWindowSize; 107 : size_t packetsInFlight; 108 : Future<std::string> task; 109 : }; 110 : 111 : std::function<bool(const CNetMessage* message)> m_SendMessage; 112 : 113 : std::uint32_t m_NextRequestID{1}; 114 : 115 : 116 : struct AsyncFileReceiveTask 117 : { 118 : /** 119 : * Called when m_Buffer contains the full received data. 120 : */ 121 : std::function<void(std::string)> onComplete; 122 : 123 : // TODO: Ought to have a failure channel, e.g. when the session drops or there's another error. 124 : 125 : size_t length{0}; 126 : 127 : std::string buffer; 128 : }; 129 : 130 : using FileReceiveTasksMap = std::unordered_map<std::uint32_t, AsyncFileReceiveTask>; 131 : FileReceiveTasksMap m_FileReceiveTasks; 132 : 133 : using FileSendTasksMap = std::map<std::uint32_t, CNetFileSendTask>; 134 : FileSendTasksMap m_FileSendTasks; 135 : 136 : double m_LastProgressReportTime{0}; 137 : }; 138 : 139 : #endif // NETFILETRANSFER_H