-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_sender.h
124 lines (97 loc) · 2.85 KB
/
command_sender.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
123
124
#pragma once
#include <memory>
#include <functional>
#include <chrono>
#include "sock.h"
#include "context2.h"
using namespace std::chrono;
class Peer;
class Command;
class CommandChannel;
class EchoCommand;
class SendCommand;
class NetStat;
using SendCommandClazz = class SendCommand;
class CommandSender
{
public:
CommandSender(std::shared_ptr<CommandChannel> channel);
int Start();
int Stop();
int SendCommand();
int RecvCommand();
virtual int SendData() { return 0; };
virtual int RecvData() { return 0; };
int Timeout(int timeout);
void SetTimeout(int timeout) { timeout_ = timeout>0?timeout:-1; };
int GetTimeout() { return timeout_; };
std::function<void(std::shared_ptr<NetStat>)> OnStopped;
protected:
virtual int OnSendCommand();
virtual int OnRecvCommand(std::shared_ptr<Command> command);
virtual int OnStart();
virtual int OnStop(std::shared_ptr<NetStat> result_command);
virtual int OnTimeout() { return 0; };
std::shared_ptr<Sock> control_sock_;
std::shared_ptr<Sock> data_sock_;
std::shared_ptr<Context> context_;
private:
int timeout_;
std::shared_ptr<Command> command_;
bool is_stopping_;
bool is_stopped_;
bool is_starting_;
bool is_started_;
bool is_waiting_result_;
bool is_waiting_ack_;
bool can_start_payload_;
friend class Peer;
DISALLOW_COPY_AND_ASSIGN(CommandSender);
};
class EchoCommandSender : public CommandSender
{
public:
EchoCommandSender(std::shared_ptr<CommandChannel> channel);
int SendData() override;
int RecvData() override;
int OnTimeout() override;
private:
int OnStart() override;
int OnStop(std::shared_ptr<NetStat> netstat) override;
std::shared_ptr<EchoCommand> 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_;
int64_t delay_;
int64_t min_delay_;
int64_t max_delay_;
ssize_t send_packets_;
ssize_t recv_packets_;
std::string data_buf_;
long long illegal_packets_=0;
long long timeout_packets_=0;
uint64_t varn_delay_ = 0;
uint64_t std_delay_ = 0;
};
class SendCommandSender : public CommandSender
{
public:
SendCommandSender(std::shared_ptr<CommandChannel> channel);
int SendData() override;
int RecvData() override;
int OnTimeout() override;
private:
int OnStart() override;
int OnStop(std::shared_ptr<NetStat> netstat) override;
inline bool TryStop();
std::shared_ptr<SendCommandClazz> command_;
bool is_stoping_;
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 send_packets_;
ssize_t send_bytes_;
std::string data_buf_;
};