-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLoop.cc
199 lines (173 loc) · 5.52 KB
/
Loop.cc
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Loop.h"
#include "util/BaseTimer.h"
#include <iostream>
#include "transport/Sender.h"
#include "transport/Receiver.h"
bool LoopEvent::init() {
loop_interval = 5;
bool initRes = session.initSession();
return initRes;
}
bool LoopEvent::run() {
int64_t currentTs = BaseTimer::getCurrentTime();
int64_t lastIntervalTs = currentTs;
Connection *conn = session.getConn();
while (true) {
int64_t recvLen = conn->recvPacket();
if (recvLen > 0) {
char *recvBuff = conn->getBuffer();
BoeHeader header;
if (!header.headerDecode(recvBuff)) {
perror("header process error");
continue;
}
__processMessage(recvBuff, &header);
CLEANUP(recvBuff);
}
currentTs = BaseTimer::getCurrentTime();
if (currentTs - lastIntervalTs >= 5000 * 1000) {
//5ms执行一次
__processHeartbeat(currentTs);
lastIntervalTs = currentTs;
}
}
return true;
}
bool LoopEvent::__processHeartbeat(const int64_t ¤t_ts) {
session.sessHeartbeat(current_ts);
return true;
}
bool LoopEvent::__processMessage(char *recvBuff, BoeHeader *header) {
if (header->mid > MAX_MSG_ID || header->mid < MIN_MSG_ID) {
perror("error message id");
return false;
}
if (session.state == NET_STATE_INTERRUPT) {
/*只有网络连通性正常的条件下,程序才能走到这里*/
session.state = NET_STATE_RECOVER;
}
Connection *conn = session.getConn();
uint32_t uid = header->uid;
switch (header->mid) {
case MSG_CONNECT: {
char *ackBuff = (char *)malloc(sizeof(char) * 100);
memset(ackBuff, 0, 100);
if (BoeMsgProcessor::procConnectMsg(recvBuff, header, ackBuff)) {
session.uid = uid;
IReceiver* recv = session.createReceiver();
if (recv == NULL)
break;
conn->sendPacket(ackBuff);
}
free(ackBuff);
break;
}
case MSG_CONNECT_ACK: {
BoeConnectAckMessage connectAckMsg;
connectAckMsg.Process(recvBuff, header);
if (session.state != SESSION_STATE_CONNECTING) {
return false;
}
if (connectAckMsg.result != 0) {
return false;
}
if (session.getSender() == NULL) {
ISender *sender = session.createSender();
if (sender == NULL)
break;
session.state = SESSION_STATE_CONNECTED;
session.interrupt = NET_STATE_RECOVER;
}
break;
}
case MSG_DISCONNECT: {
BoeDisConnectMessage disconnectMsg;
disconnectMsg.Process(recvBuff, header);
/*构造Disconnect ACK*/
char *disconnectAckBuff = (char *)malloc(sizeof(char) * MTU);
char *p = disconnectAckBuff;
BoeHeader respHeader;
respHeader.uid = session.uid;
respHeader.mid = MSG_DISCONNECT;
respHeader.headerEncode(p);
BoeDisConnectAckMessage disconnectAckMsg;
disconnectAckMsg.cid = disconnectMsg.cid;
disconnectAckMsg.result = 0;
disconnectAckMsg.Build(p + respHeader.header_size);
conn->sendPacket(disconnectAckBuff);
free(disconnectAckBuff);
/*销毁receiver信息*/
if (session.rcid == disconnectMsg.cid && session.getReceiver() != NULL) {
/* 通知上层停止播放 */
session.notify(STOP_PLAY_NOTIFY);
session.destroyReceiver();
session.setReceiver(NULL);
}
break;
}
case MSG_DISCONNECT_ACK: {
BoeDisConnectAckMessage disconnectAckMsg;
disconnectAckMsg.Process(recvBuff, header);
if (session.state == SESSION_STATE_DISCONNECT) {
session.resetSession();
session.notify(DISCONNECT_NOTIFY);
}
break;
}
case MSG_SEGMENT: {
if (session.getReceiver() == NULL)
return false;
char *ackBuff = (char *)malloc(sizeof(char) * MTU);
memset(ackBuff, 0, MTU);
BoeMsgProcessor::procSegmentMsg(&session, recvBuff, header, ackBuff);
break;
}
case MSG_SEGMENT_ACK: {
BoeSegmentAckMessage segmentAckMsg;
segmentAckMsg.Process(recvBuff, header);
/*处理Segment ACK消息*/
BoeSender *sender = session.getSender();
if (sender != NULL) {
sender->procSegmentAck(&segmentAckMsg);
}
break;
}
case MSG_FEEDBACK: {
BoeFeedbackMessage feedbackMsg;
feedbackMsg.Process(recvBuff, header);
BoeSender *sender = session.getSender();
if (sender != NULL) {
sender->procFeedbackMsg(&feedbackMsg);
}
break;
}
case MSG_PING: {
char *ackBuff = (char *)malloc(sizeof(char) * 100);
memset(ackBuff, 0, 100);
BoePingMessage pingMsg;
pingMsg.Process(recvBuff, header);
BoeHeader header;
header.uid = session.uid;
header.mid = MSG_PONG;
header.headerEncode(ackBuff);
ackBuff += header.header_size;
BoePongMessage pongMsg;
pongMsg.ts = pingMsg.ts;
pongMsg.Build(ackBuff);
conn->sendPacket(ackBuff);
free(ackBuff);
}
case MSG_PONG: {
BoePongMessage pongMsg;
pongMsg.Process(recvBuff, header);
int64_t nowTs = BaseTimer::getCurrentTime();
uint32_t keepRtt = 5;
if (nowTs - pongMsg.ts > 5)
keepRtt = (uint32_t)(nowTs - pongMsg.ts);
session.sessCalculateRtt(keepRtt);
}
default:
break;
}
return true;
}