Line data Source code
1 : /* Copyright (C) 2021 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 <map>
22 : #include <string>
23 :
24 : class CNetMessage;
25 : class CFileTransferResponseMessage;
26 : class CFileTransferDataMessage;
27 : class CFileTransferAckMessage;
28 : class INetSession;
29 :
30 : // Assume this is sufficiently less than MTU that packets won't get
31 : // fragmented or dropped.
32 : static const size_t DEFAULT_FILE_TRANSFER_PACKET_SIZE = 1024;
33 :
34 : // To improve performance without flooding ENet's internal buffers,
35 : // maintain a small number of in-flight packets.
36 : // Pick numbers so that with e.g. 200ms round-trip latency
37 : // we can hopefully get windowSize*packetSize*1000/200 = 160KB/s bandwidth
38 : static const size_t DEFAULT_FILE_TRANSFER_WINDOW_SIZE = 32;
39 :
40 : // Some arbitrary limit to make it slightly harder to use up all of someone's RAM
41 : static const size_t MAX_FILE_TRANSFER_SIZE = 8*MiB;
42 :
43 : /**
44 : * Asynchronous file-receiving task.
45 : * Other code should subclass this, implement OnComplete(),
46 : * then pass it to CNetFileTransferer::StartTask.
47 : */
48 : class CNetFileReceiveTask
49 : {
50 : public:
51 0 : CNetFileReceiveTask() : m_RequestID(0), m_Length(0) { }
52 0 : virtual ~CNetFileReceiveTask() {}
53 :
54 : /**
55 : * Called when m_Buffer contains the full received data.
56 : */
57 : virtual void OnComplete() = 0;
58 :
59 : // TODO: Ought to have an OnFailure, e.g. when the session drops or there's another error
60 :
61 : /**
62 : * Uniquely identifies the request within the scope of its CNetFileTransferer.
63 : * Set automatically by StartTask.
64 : */
65 : u32 m_RequestID;
66 :
67 : size_t m_Length;
68 :
69 : std::string m_Buffer;
70 : };
71 :
72 : /**
73 : * Handles transferring files between clients and servers.
74 : */
75 0 : class CNetFileTransferer
76 : {
77 : public:
78 0 : CNetFileTransferer(INetSession* session)
79 0 : : m_Session(session), m_NextRequestID(1), m_LastProgressReportTime(0)
80 : {
81 0 : }
82 :
83 : /**
84 : * Should be called when a message is received from the network.
85 : * Returns INFO::SKIPPED if the message is not one that this class handles.
86 : * Returns INFO::OK if the message is handled successfully,
87 : * or ERR::FAIL if handled unsuccessfully.
88 : */
89 : Status HandleMessageReceive(const CNetMessage& message);
90 :
91 : /**
92 : * Registers a file-receiving task.
93 : */
94 : void StartTask(const std::shared_ptr<CNetFileReceiveTask>& task);
95 :
96 : /**
97 : * Registers data to be sent in response to a request.
98 : * (Callers are expected to have their own mechanism for receiving
99 : * requests and deciding what to respond with.)
100 : */
101 : void StartResponse(u32 requestID, const std::string& data);
102 :
103 : /**
104 : * Call frequently (e.g. once per frame) to trigger any necessary
105 : * packet processing.
106 : */
107 : void Poll();
108 :
109 : private:
110 : Status OnFileTransferResponse(const CFileTransferResponseMessage& message);
111 : Status OnFileTransferData(const CFileTransferDataMessage& message);
112 : Status OnFileTransferAck(const CFileTransferAckMessage& message);
113 :
114 : /**
115 : * Asynchronous file-sending task.
116 : */
117 0 : struct CNetFileSendTask
118 : {
119 : u32 requestID;
120 : std::string buffer;
121 : size_t offset;
122 : size_t maxWindowSize;
123 : size_t packetsInFlight;
124 : };
125 :
126 : INetSession* m_Session;
127 :
128 : u32 m_NextRequestID;
129 :
130 : using FileReceiveTasksMap = std::map<u32, std::shared_ptr<CNetFileReceiveTask>>;
131 : FileReceiveTasksMap m_FileReceiveTasks;
132 :
133 : using FileSendTasksMap = std::map<u32, CNetFileSendTask>;
134 : FileSendTasksMap m_FileSendTasks;
135 :
136 : double m_LastProgressReportTime;
137 : };
138 :
139 : #endif // NETFILETRANSFER_H
|