-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_receiver.h
122 lines (102 loc) · 2.8 KB
/
command_receiver.h
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once
#include <chrono>
#include <queue>
#include <bitset>
#include "context2.h"
#include "sock.h"
using namespace std::chrono;
class Command;
class CommandChannel;
class EchoCommand;
class SendCommand;
class NetStat;
class CommandReceiver
{
public:
CommandReceiver(std::shared_ptr<CommandChannel> channel);
virtual int Start() = 0;
virtual int Stop() = 0;
virtual int Send()
{
return -1;
}
virtual int Recv()
{
return -1;
}
virtual int RecvPrivateCommand(std::shared_ptr<Command> private_command);
virtual int SendPrivateCommand() { return 0; }
std::function<void(std::shared_ptr<Command>, std::shared_ptr<NetStat>)> OnStopped;
int GetDataFd() { return data_sock_ ? data_sock_->GetFd() : -1; }
// TODO: optimize
ssize_t out_of_command_packets_ = 0;
protected:
std::string argv_;
std::shared_ptr<Context> context_;
std::shared_ptr<Sock> control_sock_;
std::shared_ptr<Sock> data_sock_;
};
class EchoCommandReceiver : public CommandReceiver
{
public:
EchoCommandReceiver(std::shared_ptr<CommandChannel> channel);
int Start() override;
int Stop() override;
int Send() override;
int Recv() override;
int SendPrivateCommand() override;
private:
ssize_t recv_count_;
ssize_t send_count_;
bool running_;
bool is_stopping_;
std::shared_ptr<EchoCommand> command_;
std::queue<std::string> data_queue_;
ssize_t illegal_packets_;
//ssize_t reorder_packets_;
// TODO: support duplicate packets stat
//ssize_t duplicate_packets_;
//ssize_t timeout_packets_;
char token_;
//uint16_t sequence_;
//std::bitset<MAX_SEQ> packets_;
};
class SendCommandReceiver : public CommandReceiver
{
public:
SendCommandReceiver(std::shared_ptr<CommandChannel> channel);
int Start() override;
int Stop() override;
int Recv() override;
int SendPrivateCommand() override;
private:
std::string buf_;
bool running_;
bool is_stopping_;
std::shared_ptr<SendCommand> command_;
high_resolution_clock::time_point start_;
high_resolution_clock::time_point stop_;
high_resolution_clock::time_point begin_;
high_resolution_clock::time_point end_;
ssize_t recv_count_;
int64_t recv_bytes_;
int64_t speed_;
int64_t max_speed_;
int64_t min_speed_;
ssize_t illegal_packets_;
ssize_t reorder_packets_;
ssize_t duplicate_packets_;
ssize_t timeout_packets_;
ssize_t latest_recv_bytes_;
char token_;
uint16_t sequence_;
std::bitset<MAX_SEQ> packets_;
int64_t avg_delay_ = 0;
int64_t max_delay_ = 0;
int64_t min_delay_ = 0;
int64_t head_avg_delay_ = 0;;
// var_delay_ = varn_delay_/n is variance
uint64_t varn_delay_ = 0;
// std_delay_ is standard deviation
uint64_t std_delay_ = 0;
};