1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef NETFILETRANSFER_H
19 #define NETFILETRANSFER_H
25 class CFileTransferResponseMessage
;
26 class CFileTransferDataMessage
;
27 class CFileTransferAckMessage
;
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;
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;
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
;
44 * Asynchronous file-receiving task.
45 * Other code should subclass this, implement OnComplete(),
46 * then pass it to CNetFileTransferer::StartTask.
48 class CNetFileReceiveTask
51 CNetFileReceiveTask() : m_RequestID(0), m_Length(0) { }
52 virtual ~CNetFileReceiveTask() {}
55 * Called when m_Buffer contains the full received data.
57 virtual void OnComplete() = 0;
59 // TODO: Ought to have an OnFailure, e.g. when the session drops or there's another error
62 * Uniquely identifies the request within the scope of its CNetFileTransferer.
63 * Set automatically by StartTask.
73 * Handles transferring files between clients and servers.
75 class CNetFileTransferer
78 CNetFileTransferer(INetSession
* session
)
79 : m_Session(session
), m_NextRequestID(1), m_LastProgressReportTime(0)
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.
89 Status
HandleMessageReceive(const CNetMessage
& message
);
92 * Registers a file-receiving task.
94 void StartTask(const std::shared_ptr
<CNetFileReceiveTask
>& task
);
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.)
101 void StartResponse(u32 requestID
, const std::string
& data
);
104 * Call frequently (e.g. once per frame) to trigger any necessary
110 Status
OnFileTransferResponse(const CFileTransferResponseMessage
& message
);
111 Status
OnFileTransferData(const CFileTransferDataMessage
& message
);
112 Status
OnFileTransferAck(const CFileTransferAckMessage
& message
);
115 * Asynchronous file-sending task.
117 struct CNetFileSendTask
122 size_t maxWindowSize
;
123 size_t packetsInFlight
;
126 INetSession
* m_Session
;
130 using FileReceiveTasksMap
= std::map
<u32
, std::shared_ptr
<CNetFileReceiveTask
>>;
131 FileReceiveTasksMap m_FileReceiveTasks
;
133 using FileSendTasksMap
= std::map
<u32
, CNetFileSendTask
>;
134 FileSendTasksMap m_FileSendTasks
;
136 double m_LastProgressReportTime
;
139 #endif // NETFILETRANSFER_H