-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.h
More file actions
92 lines (80 loc) · 2.36 KB
/
Copy pathclient.h
File metadata and controls
92 lines (80 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef CLIENT_H
#define CLIENT_H
#include <sstream>
#include <unordered_map>
#include <memory>
#include <list>
#include <fstream>
#include "data.h"
#include "buffer.h"
#include "socket.h"
#include "transfer.h"
#include "file.h"
constexpr size_t MAX_BUFFER_SIZE = 2500000;
constexpr int MAX_BATCH_SIZE = 512;
class Client;
using ClientContainer = std::shared_ptr<Client>;
using ClientsList = const std::list<ClientContainer>&;
const ConfirmCondition genericConfirmCondition = [](Buffer& buff)
{
if (buff.getSize() > 2)
return buff.getData()[2] == 1;
return false;
};
struct ClientStates
{
enum State {Readable, Writeable, HaveExceptions};
std::unordered_map<int, std::vector<ClientContainer>> clients;
bool isAnyReady = false;
operator bool() { return isAnyReady; }
};
struct FileInitState
{
File file;
unsigned chunksSize = 0;
unsigned chunksCount = 0;
std::string fileName = "";
};
struct FileTransferState
{
std::bitset<MAX_BATCH_SIZE> batchState = 0;
unsigned batchSize = 0;
unsigned chunksTransfered = 0;
unsigned previousChunkId = -1;
unsigned chunkId = 0;
unsigned nextChunkSize = 0;
double downladProgress = 0;
};
struct MachineState
{
bool echoMode = false;
Buffer buff = {MAX_BUFFER_SIZE};
std::stringstream message;
FileInitState fileInitState;
FileTransferState fileTransferState;
MachineState() {}
MachineState& operator=(const MachineState&) { return *this; }
MachineState(const MachineState&) {}
};
class Client
{
protected:
MachineState* state;
bool isStateDeletable = true;
public:
Client();
virtual ~Client();
void setState(MachineState& state);
MachineState& getState();
virtual OperationResult Send(size_t size, Buffer& buff, bool confirm = true) = 0;
virtual OperationResult Send(size_t size, bool confirm = true) = 0;
virtual OperationResult Recieve(size_t size, Buffer& buff, bool confirm = true) = 0;
virtual OperationResult Recieve(size_t size, bool confirm = true) = 0;
virtual OperationResult Peek(size_t size, Buffer& buff) = 0;
virtual OperationResult Peek(size_t size) = 0;
virtual Socket& getSocket() = 0;
virtual bool isReachable() = 0;
virtual void endTransfer() = 0;
};
ClientStates select( ClientsList writeable, ClientsList readable, ClientsList haveExceptions, int timeout);
#endif // CLIENT_H