Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetFileTransfer.h
Go to the documentation of this file.
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
31class CFileTransferAckMessage;
32class CFileTransferDataMessage;
33class CFileTransferResponseMessage;
34class CNetMessage;
35
36// Assume this is sufficiently less than MTU that packets won't get
37// fragmented or dropped.
38static 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
44static 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
47static const size_t MAX_FILE_TRANSFER_SIZE = 8*MiB;
48
49/**
50 * Handles transferring files between clients and servers.
51 */
53{
54public:
55 enum class RequestType
56 {
57 LOADGAME,
58 REJOIN
59 };
60
61 template<typename Session>
62 CNetFileTransferer(Session& session) :
63 m_SendMessage{std::bind_front(&Session::SendMessage, std::ref(session))}
64 {
65 }
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 */
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
93private:
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 */
102 {
103 std::uint32_t requestID;
104 std::string buffer;
105 size_t offset;
109 };
110
111 std::function<bool(const CNetMessage* message)> m_SendMessage;
112
113 std::uint32_t m_NextRequestID{1};
114
115
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>;
132
133 using FileSendTasksMap = std::map<std::uint32_t, CNetFileSendTask>;
135
137};
138
139#endif // NETFILETRANSFER_H
static const size_t MAX_FILE_TRANSFER_SIZE
Definition: NetFileTransfer.h:47
static const size_t DEFAULT_FILE_TRANSFER_PACKET_SIZE
Definition: NetFileTransfer.h:38
static const size_t DEFAULT_FILE_TRANSFER_WINDOW_SIZE
Definition: NetFileTransfer.h:44
static const size_t MiB
Definition: alignment.h:108
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:53
void Poll()
Call frequently (e.g.
Definition: NetFileTransfer.cpp:183
Status OnFileTransferData(const CFileTransferDataMessage &message)
Definition: NetFileTransfer.cpp:79
std::function< bool(const CNetMessage *message)> m_SendMessage
Definition: NetFileTransfer.h:111
Status OnFileTransferAck(const CFileTransferAckMessage &message)
Definition: NetFileTransfer.cpp:129
std::uint32_t m_NextRequestID
Definition: NetFileTransfer.h:113
Status HandleMessageReceive(const CNetMessage &message)
Should be called when a message is received from the network.
Definition: NetFileTransfer.cpp:34
void StartTask(RequestType requestType, std::function< void(std::string)> task)
Registers a file-receiving task.
Definition: NetFileTransfer.cpp:153
FileSendTasksMap m_FileSendTasks
Definition: NetFileTransfer.h:134
std::map< std::uint32_t, CNetFileSendTask > FileSendTasksMap
Definition: NetFileTransfer.h:133
RequestType
Definition: NetFileTransfer.h:56
Status OnFileTransferResponse(const CFileTransferResponseMessage &message)
Definition: NetFileTransfer.cpp:52
double m_LastProgressReportTime
Definition: NetFileTransfer.h:136
void StartResponse(std::uint32_t requestID, const std::string &data)
Registers data to be sent in response to a request.
Definition: NetFileTransfer.cpp:165
CNetFileTransferer(Session &session)
Definition: NetFileTransfer.h:62
FileReceiveTasksMap m_FileReceiveTasks
Definition: NetFileTransfer.h:131
std::unordered_map< std::uint32_t, AsyncFileReceiveTask > FileReceiveTasksMap
Definition: NetFileTransfer.h:130
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:42
Definition: ShaderDefines.cpp:43
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: NetFileTransfer.h:117
std::function< void(std::string)> onComplete
Called when m_Buffer contains the full received data.
Definition: NetFileTransfer.h:121
size_t length
Definition: NetFileTransfer.h:125
std::string buffer
Definition: NetFileTransfer.h:127
Asynchronous file-sending task.
Definition: NetFileTransfer.h:102
Future< std::string > task
Definition: NetFileTransfer.h:108
size_t offset
Definition: NetFileTransfer.h:105
size_t packetsInFlight
Definition: NetFileTransfer.h:107
std::uint32_t requestID
Definition: NetFileTransfer.h:103
size_t maxWindowSize
Definition: NetFileTransfer.h:106
std::string buffer
Definition: NetFileTransfer.h:104