2024-03-09 15:31:43 +01:00
|
|
|
/* Copyright (C) 2024 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2011-10-27 18:46:48 +02:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2011-10-27 18:46:48 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2011-10-27 18:46:48 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-27 18:46:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NETFILETRANSFER_H
|
|
|
|
#define NETFILETRANSFER_H
|
|
|
|
|
2024-03-09 15:31:43 +01:00
|
|
|
#include <functional>
|
2011-12-23 14:01:29 +01:00
|
|
|
#include <map>
|
2020-12-09 15:39:14 +01:00
|
|
|
#include <string>
|
2024-03-09 15:31:43 +01:00
|
|
|
#include <unordered_map>
|
2011-12-23 14:01:29 +01:00
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
class CNetMessage;
|
2019-09-17 16:18:46 +02:00
|
|
|
class CFileTransferResponseMessage;
|
|
|
|
class CFileTransferDataMessage;
|
|
|
|
class CFileTransferAckMessage;
|
2011-10-27 18:46:48 +02:00
|
|
|
class INetSession;
|
|
|
|
|
|
|
|
// Assume this is sufficiently less than MTU that packets won't get
|
|
|
|
// fragmented or dropped.
|
|
|
|
static const size_t DEFAULT_FILE_TRANSFER_PACKET_SIZE = 1024;
|
|
|
|
|
|
|
|
// To improve performance without flooding ENet's internal buffers,
|
|
|
|
// maintain a small number of in-flight packets.
|
|
|
|
// Pick numbers so that with e.g. 200ms round-trip latency
|
|
|
|
// we can hopefully get windowSize*packetSize*1000/200 = 160KB/s bandwidth
|
|
|
|
static const size_t DEFAULT_FILE_TRANSFER_WINDOW_SIZE = 32;
|
|
|
|
|
|
|
|
// Some arbitrary limit to make it slightly harder to use up all of someone's RAM
|
|
|
|
static const size_t MAX_FILE_TRANSFER_SIZE = 8*MiB;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles transferring files between clients and servers.
|
|
|
|
*/
|
|
|
|
class CNetFileTransferer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CNetFileTransferer(INetSession* session)
|
|
|
|
: m_Session(session), m_NextRequestID(1), m_LastProgressReportTime(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should be called when a message is received from the network.
|
|
|
|
* Returns INFO::SKIPPED if the message is not one that this class handles.
|
|
|
|
* Returns INFO::OK if the message is handled successfully,
|
|
|
|
* or ERR::FAIL if handled unsuccessfully.
|
|
|
|
*/
|
2019-09-17 16:18:46 +02:00
|
|
|
Status HandleMessageReceive(const CNetMessage& message);
|
2011-10-27 18:46:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a file-receiving task.
|
|
|
|
*/
|
2024-03-09 15:31:43 +01:00
|
|
|
void StartTask(std::function<void(std::string)> task);
|
2011-10-27 18:46:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers data to be sent in response to a request.
|
|
|
|
* (Callers are expected to have their own mechanism for receiving
|
|
|
|
* requests and deciding what to respond with.)
|
|
|
|
*/
|
|
|
|
void StartResponse(u32 requestID, const std::string& data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call frequently (e.g. once per frame) to trigger any necessary
|
|
|
|
* packet processing.
|
|
|
|
*/
|
|
|
|
void Poll();
|
|
|
|
|
|
|
|
private:
|
2019-09-17 16:18:46 +02:00
|
|
|
Status OnFileTransferResponse(const CFileTransferResponseMessage& message);
|
|
|
|
Status OnFileTransferData(const CFileTransferDataMessage& message);
|
|
|
|
Status OnFileTransferAck(const CFileTransferAckMessage& message);
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
/**
|
|
|
|
* Asynchronous file-sending task.
|
|
|
|
*/
|
|
|
|
struct CNetFileSendTask
|
|
|
|
{
|
|
|
|
u32 requestID;
|
|
|
|
std::string buffer;
|
|
|
|
size_t offset;
|
|
|
|
size_t maxWindowSize;
|
|
|
|
size_t packetsInFlight;
|
|
|
|
};
|
|
|
|
|
|
|
|
INetSession* m_Session;
|
|
|
|
|
|
|
|
u32 m_NextRequestID;
|
|
|
|
|
2024-03-09 15:31:43 +01:00
|
|
|
|
|
|
|
struct AsyncFileReceiveTask
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Called when m_Buffer contains the full received data.
|
|
|
|
*/
|
|
|
|
std::function<void(std::string)> onComplete;
|
|
|
|
|
|
|
|
// TODO: Ought to have a failure channel, e.g. when the session drops or there's another error.
|
|
|
|
|
|
|
|
size_t length{0};
|
|
|
|
|
|
|
|
std::string buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
using FileReceiveTasksMap = std::unordered_map<u32, AsyncFileReceiveTask>;
|
2011-10-27 18:46:48 +02:00
|
|
|
FileReceiveTasksMap m_FileReceiveTasks;
|
|
|
|
|
2019-09-17 16:18:46 +02:00
|
|
|
using FileSendTasksMap = std::map<u32, CNetFileSendTask>;
|
2011-10-27 18:46:48 +02:00
|
|
|
FileSendTasksMap m_FileSendTasks;
|
|
|
|
|
|
|
|
double m_LastProgressReportTime;
|
|
|
|
};
|
|
|
|
|
2011-12-23 14:01:29 +01:00
|
|
|
#endif // NETFILETRANSFER_H
|