-
Notifications
You must be signed in to change notification settings - Fork 3
/
State.h
132 lines (114 loc) · 2.46 KB
/
State.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
125
126
127
128
129
130
131
132
//
// Created by frank on 17-8-16.
//
#ifndef FDP_STATE_H
#define FDP_STATE_H
#include "Timer.h"
#include "Logger.h"
enum
{
STATE_ESTABLISHED,
STATE_CLOSE_WAIT,
STATE_LAST_ACK,
STATE_FIN_WAIT_1,
STATE_FIN_WAIT_2,
STATE_CLOSING,
STATE_TIME_WAIT,
STATE_NORMAL_CLOSED,
STATE_TIMEOUT_CLOSED
};
class State
{
public:
State(const uint32_t* current, uint32_t msl) :
timeWaitTimer_(current),
current_(current),
msl_(msl),
state_(STATE_ESTABLISHED)
{}
int get() const
{
if (state_ == STATE_TIME_WAIT &&
timeWaitTimer_.timeout() > 0) {
state_ = STATE_NORMAL_CLOSED;
LOG_INFO("%u TIME_WAIT -> CLOSED", *current_);
}
return state_;
}
bool closed() const
{
if (state_ == STATE_TIME_WAIT &&
timeWaitTimer_.timeout() > 0) {
state_ = STATE_NORMAL_CLOSED;
LOG_INFO("%u TIME_WAIT -> CLOSED", *current_);
}
return state_ == STATE_NORMAL_CLOSED ||
state_ == STATE_TIMEOUT_CLOSED;
}
void sendFin()
{
switch (state_) {
case STATE_ESTABLISHED:
state_ = STATE_FIN_WAIT_1;
LOG_INFO(" ESTABLISHED -> FIN_WAIT_1");
break;
case STATE_CLOSE_WAIT:
state_ = STATE_LAST_ACK;
LOG_INFO(" CLOSE_WAIT -> LAST_ACK");
break;
default:
LOG_ERROR(" State::sendFin(): bad state transition %d", state_);
break;
}
}
void recvFin()
{
switch (state_) {
case STATE_ESTABLISHED:
state_ = STATE_CLOSE_WAIT;
LOG_INFO(" ESTABLISHED -> CLOSE_WAIT");
break;
case STATE_FIN_WAIT_1:
state_ = STATE_CLOSING;
LOG_INFO(" FIN_WAIT_1 -> CLOSING");
break;
case STATE_FIN_WAIT_2:
state_ = STATE_TIME_WAIT;
timeWaitTimer_.set(2 * msl_, false);
LOG_INFO(" FIN_WAIT_2 -> TIME_WAIT");
break;
default:
LOG_ERROR(" State::recvFin(): bad state transition %d", state_);
break;
}
}
void recvAck()
{
switch (state_)
{
case STATE_FIN_WAIT_1:
state_ = STATE_FIN_WAIT_2;
LOG_INFO(" FIN_WAIT_1 -> FIN_WAIT_2");
break;
case STATE_LAST_ACK:
state_ = STATE_NORMAL_CLOSED;
LOG_INFO(" LAST_ACK -> CLOSED");
break;
case STATE_CLOSING:
state_ = STATE_TIME_WAIT;
timeWaitTimer_.set(2 * msl_, false);
LOG_INFO(" CLOSING -> TIME_WAIT");
break;
default:
LOG_ERROR("State::recvAck(): bad state transition %d", state_);
break;
}
}
void timeout() { state_ = STATE_TIMEOUT_CLOSED; }
mutable Timer timeWaitTimer_;
private:
const uint32_t* current_;
const uint32_t msl_;
mutable int state_;
};
#endif //FDP_STATE_H