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