Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetFileTransfer.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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 <functional>
22#include <map>
23#include <string>
24#include <unordered_map>
25
26class CNetMessage;
27class CFileTransferResponseMessage;
28class CFileTransferDataMessage;
29class CFileTransferAckMessage;
30class INetSession;
31
32// Assume this is sufficiently less than MTU that packets won't get
33// fragmented or dropped.
34static const size_t DEFAULT_FILE_TRANSFER_PACKET_SIZE = 1024;
35
36// To improve performance without flooding ENet's internal buffers,
37// maintain a small number of in-flight packets.
38// Pick numbers so that with e.g. 200ms round-trip latency
39// we can hopefully get windowSize*packetSize*1000/200 = 160KB/s bandwidth
40static const size_t DEFAULT_FILE_TRANSFER_WINDOW_SIZE = 32;
41
42// Some arbitrary limit to make it slightly harder to use up all of someone's RAM
43static const size_t MAX_FILE_TRANSFER_SIZE = 8*MiB;
44
45/**
46 * Handles transferring files between clients and servers.
47 */
49{
50public:
53 {
54 }
55
56 /**
57 * Should be called when a message is received from the network.
58 * Returns INFO::SKIPPED if the message is not one that this class handles.
59 * Returns INFO::OK if the message is handled successfully,
60 * or ERR::FAIL if handled unsuccessfully.
61 */
63
64 /**
65 * Registers a file-receiving task.
66 */
67 void StartTask(std::function<void(std::string)> task);
68
69 /**
70 * Registers data to be sent in response to a request.
71 * (Callers are expected to have their own mechanism for receiving
72 * requests and deciding what to respond with.)
73 */
74 void StartResponse(u32 requestID, const std::string& data);
75
76 /**
77 * Call frequently (e.g. once per frame) to trigger any necessary
78 * packet processing.
79 */
80 void Poll();
81
82private:
83 Status OnFileTransferResponse(const CFileTransferResponseMessage& message);
84 Status OnFileTransferData(const CFileTransferDataMessage& message);
85 Status OnFileTransferAck(const CFileTransferAckMessage& message);
86
87 /**
88 * Asynchronous file-sending task.
89 */
91 {
93 std::string buffer;
94 size_t offset;
97 };
98
100
102
103
105 {
106 /**
107 * Called when m_Buffer contains the full received data.
108 */
109 std::function<void(std::string)> onComplete;
110
111 // TODO: Ought to have a failure channel, e.g. when the session drops or there's another error.
112
113 size_t length{0};
114
115 std::string buffer;
116 };
117
118 using FileReceiveTasksMap = std::unordered_map<u32, AsyncFileReceiveTask>;
120
121 using FileSendTasksMap = std::map<u32, CNetFileSendTask>;
123
125};
126
127#endif // NETFILETRANSFER_H
static const size_t MAX_FILE_TRANSFER_SIZE
Definition: NetFileTransfer.h:43
static const size_t DEFAULT_FILE_TRANSFER_PACKET_SIZE
Definition: NetFileTransfer.h:34
static const size_t DEFAULT_FILE_TRANSFER_WINDOW_SIZE
Definition: NetFileTransfer.h:40
static const size_t MiB
Definition: alignment.h:107
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:49
void Poll()
Call frequently (e.g.
Definition: NetFileTransfer.cpp:171
CNetFileTransferer(INetSession *session)
Definition: NetFileTransfer.h:51
Status OnFileTransferData(const CFileTransferDataMessage &message)
Definition: NetFileTransfer.cpp:72
Status OnFileTransferAck(const CFileTransferAckMessage &message)
Definition: NetFileTransfer.cpp:120
Status HandleMessageReceive(const CNetMessage &message)
Should be called when a message is received from the network.
Definition: NetFileTransfer.cpp:28
void StartResponse(u32 requestID, const std::string &data)
Registers data to be sent in response to a request.
Definition: NetFileTransfer.cpp:155
u32 m_NextRequestID
Definition: NetFileTransfer.h:101
void StartTask(std::function< void(std::string)> task)
Registers a file-receiving task.
Definition: NetFileTransfer.cpp:144
std::map< u32, CNetFileSendTask > FileSendTasksMap
Definition: NetFileTransfer.h:121
INetSession * m_Session
Definition: NetFileTransfer.h:99
FileSendTasksMap m_FileSendTasks
Definition: NetFileTransfer.h:122
Status OnFileTransferResponse(const CFileTransferResponseMessage &message)
Definition: NetFileTransfer.cpp:46
double m_LastProgressReportTime
Definition: NetFileTransfer.h:124
FileReceiveTasksMap m_FileReceiveTasks
Definition: NetFileTransfer.h:119
std::unordered_map< u32, AsyncFileReceiveTask > FileReceiveTasksMap
Definition: NetFileTransfer.h:118
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:33
Interface for sessions to which messages can be sent.
Definition: NetSession.h:56
i64 Status
Error handling system.
Definition: status.h:173
Definition: NetFileTransfer.h:105
std::function< void(std::string)> onComplete
Called when m_Buffer contains the full received data.
Definition: NetFileTransfer.h:109
size_t length
Definition: NetFileTransfer.h:113
std::string buffer
Definition: NetFileTransfer.h:115
Asynchronous file-sending task.
Definition: NetFileTransfer.h:91
size_t offset
Definition: NetFileTransfer.h:94
size_t packetsInFlight
Definition: NetFileTransfer.h:96
size_t maxWindowSize
Definition: NetFileTransfer.h:95
u32 requestID
Definition: NetFileTransfer.h:92
std::string buffer
Definition: NetFileTransfer.h:93
uint32_t u32
Definition: types.h:39