-
Notifications
You must be signed in to change notification settings - Fork 2
/
utp_socket.h
278 lines (211 loc) · 7.43 KB
/
utp_socket.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#pragma once
#include <cstdint>
#include <deque>
#include <map>
#include <memory>
#include <sstream>
#include <set>
#include "congestion_control.h"
#include "round_trip_meter.h"
#include "utp_socket_c.h"
#include "trip_time_sample.h"
#include "utp_common.h"
#ifdef _MSC_VER
#include <Winsock2.h>
#else
#include <arpa/inet.h>
#endif // _MSC_
namespace rtttech_utp
{
enum RTSocketState { RTSS_IDLE, RTSS_SYN_SENT, RTSS_SYN_RECEIVED, RTSS_ESTABLISHED, RTSS_CLOSING, RTSS_CLOSE_WAIT, RTSS_CLOSED, RTSS_ERROR };
//enum RTCommand { RTC_SYN, RTC_SYN_ACK, RTC_DATA, RTC_ACK, RTC_CLOSE, RTC_RESET, RTC_FEC };
enum FECType { FECT_XOR };
enum RTCommand {
RTC_DATA = 0, // Data packet.
RTC_FIN = 1, // Finalize the connection. This is the last packet.
RTC_STATE = 2, // State packet. Used to transmit an ACK with no data.
RTC_RESET = 3, // Terminate connection forcefully.
RTC_SYN = 4, // Connect SYN
RTC_NUM_STATES, // used for bounds checking
};
enum {
EXT_SACK = 1
};
const uint8_t UTP_VER = 1;
#if (defined(__SVR4) && defined(__sun))
#pragma pack(1)
#else
#pragma pack(push,1)
#endif
/*
uTP header from BEP 29
0 4 8 16 24 32
+-------+-------+---------------+---------------+---------------+
| type | ver | extension | connection_id |
+-------+-------+---------------+---------------+---------------+
| timestamp_microseconds |
+---------------+---------------+---------------+---------------+
| timestamp_difference_microseconds |
+---------------+---------------+---------------+---------------+
| wnd_size |
+---------------+---------------+---------------+---------------+
| seq_nr | ack_nr |
+---------------+---------------+---------------+---------------+
*/
struct UTPHeader
{
std::uint8_t type_ver;
std::uint8_t extension;
std::uint16_t conn_id;
std::uint32_t timestamp;
std::uint32_t timestamp_difference;
std::uint32_t recv_wnd;
std::uint16_t pkt_seq;
std::uint16_t ack_pkt_seq;
int GetType() const { return type_ver >> 4; }
void SetType(uint8_t t) { type_ver = (type_ver & 0xf) | (t << 4); }
int GetVersion() const { return type_ver & 0xf; }
void SetVersion(uint8_t v) { type_ver = (type_ver & 0xf0) | (v & 0xf); }
};
typedef UTPHeader PacketHead;
struct PacketHeadAck {
PacketHead pf;
std::uint8_t ext_next;
std::uint8_t ext_len;
std::uint8_t acks[4];
};
#if (defined(__SVR4) && defined(__sun))
#pragma pack(0)
#else
#pragma pack(pop)
#endif
void HeaderToBigEndian(PacketHead* pHead);
struct PacketCS
{
PacketCS() : buffer(NULL), len(0), pos(0), send_time(0), resend_times(0), ack_time(0),
fec_sent(false), pkt_data_bytes(0), pkt_seq(0), wanted_times(0) {}
~PacketCS() { if (buffer) { delete[] buffer; buffer = NULL; } }
char *buffer;
int32_t len;
int32_t pos;
int64_t send_time;
int64_t ack_time;
int32_t resend_times;
int32_t wanted_times;
uint16_t pkt_seq;
int32_t pkt_data_bytes;
int32_t pkt_head_len;
int32_t pkt_bitmap_bytes;
uint8_t pkt_cmd;
bool fec_sent;
std::string Desc() const
{
std::ostringstream oss;
oss << "pkt_seq:" << pkt_seq;
oss << "len:" << len;
oss << "send_time:" << send_time;
oss << "ack_time:" << ack_time;
oss << "resend_times:" << resend_times;
oss << "wanted_times:" << wanted_times;
return oss.str();
}
};
class UTPSocket
{
public:
UTPSocket(socket_event_callback *cb, send_data_callback *pfsend, UTPClock* clock, bool passive = false);
virtual ~UTPSocket();
int Connect(const char *to, int tolen);
int Recv(char * buffer, int len, int flag);
int Send(const char * buffer, int len, int flag);
void Close();
void OnTick();
int OnPacket(const char* buffer, int len, const char *from, int from_len);
inline RTSocketState State() const { return m_eState; }
uint16_t GetConnID() const { return m_nConnID; }
void SetConnID(uint16_t connid) { m_nConnID = connid; }
bool IsReadable();
bool IsWritable();
bool IsConnected();
int GetError();
bool IsPassive() const { return m_bPassive; }
const std::string& GetRemoteAddr() const { return m_strRemoteAddr; }
inline bool IsDestroyed() const { return m_bDestroyed && (m_eState == RTSS_CLOSED || m_eState == RTSS_ERROR) ; }
int SetSockOpt(int optname, void *optval, int optlen);
int GetSockOpt(int optname, void *optval, int optlen);
void SetUserData(void* userdata);
void* GetUserData();
std::string GetInternalState();
public:
static uint32_t s_nTotalTickTakeTime;
private:
int FillPacketHead(PacketHead* pHeader, RTCommand cmd, int nDataBytes, int nBitmapBytes);
void ChangeToState(RTSocketState s);
int DoSendData(char* buffer, int len, bool withack);
int SendSyn(bool ack = false);
int SendData(const char * buffer, int len, int flag);
int SendState();
int SendClose(bool resp = false);
int SendReset();
void ClearConnection();
void OnSyn(const PacketHead *pHeader, int nPktLen);
void OnState(const PacketHead *pHeader, int nPktLen);
void OnData(const PacketHead *pHeader, int nPktLen);
void OnClose(const PacketHead *pHeader, int nPktLen);
void OnReset(const PacketHead *pHeader, int nPktLen);
int OnAck(const PacketHead *pHeader, int nPktLen);
bool SaveReceivedData(std::shared_ptr<PacketCS> pkt, CircleSeq<uint16_t> pkt_seq, int len);
bool IsPacketValid(const PacketHead* pHeader);
uint32_t GetLeftSendWndBytes();
uint32_t GetLeftRecvWndBytes();
int ResendPacket(PacketCS &pkt);
int GetAllowWriteBytes();
uint32_t GetAckInterval();
private:
bool m_bPassive;
bool m_bDestroyed;
socket_event_callback *m_pfCallBack;
send_data_callback *m_pfSendProc;
void* m_pUserdata;
int m_nError;
std::string m_strRemoteAddr;
RTSocketState m_eState;
uint64_t m_nStateChangeTime;
CircleSeq<uint16_t> m_nLocalSendPktSeq, m_nLocalExpectPktSeq, m_nRemoteAckedPktSeq, m_nRemoteExpectBytesSeq;
CircleSeq<uint16_t> m_nLocalSendBytesSeq, m_nLocalExpectBytesSeq;
CircleSeq<uint16_t> m_nSendSeq;//
uint16_t m_nMtu;
uint32_t m_nRemoteRecvWnd, m_nLocalRecvWnd;
int32_t m_nReadableBytes;
int32_t m_nNotReadableBytes;
int32_t m_nNotAckedDataBytes;
uint16_t m_nConnID;
uint64_t m_nLastReceivedTime;
uint64_t m_nConnTimeoutTime;
uint64_t m_nLastSendAck;
uint32_t m_nAckInterval;
uint64_t m_nLastSendFec;
uint64_t m_nTotalPktNum;
uint64_t m_nFoundLostPktNum;
uint64_t m_nTotalSendTimes;
uint32_t m_nAckTakeTime = 0;
uint32_t m_nTickTakeTime = 0;
uint32_t m_nTheirTimestampDiff = 0;
uint32_t m_nMyTimestampDiff = 0;
bool m_bWaitWrite, m_bWaitRead, m_bWaitConnect;
bool m_bEnableFec;
bool m_bFastAck;
bool m_bDelayAck;
std::shared_ptr<CongestionController> m_ptrCongestionController;
std::shared_ptr<RoundTripMeter> m_ptrRoundTripMeter;
std::shared_ptr<LostCounter> m_ptrPacketLostCounter;
std::shared_ptr<UTPClock> m_ptrClocker;
std::deque<std::shared_ptr<PacketCS>> m_deqNotAckedCmdPacket;//used for sync and close cmd
std::deque<std::shared_ptr<PacketCS>> m_deqNotAckedDataPacket;
//std::deque<std::shared_ptr<PacketCS>> m_deqSendTimeOrderedDataPacket;//send order
std::map<CircleSeq<uint16_t>, std::shared_ptr<PacketCS>> m_mapReceivedPacket;
std::deque<std::shared_ptr<PacketCS>> m_deqReady4ReadPacket;
DelaySampleHistroy m_MyDelayHistory;
DelaySampleHistroy m_RemoteDelayHistory;
};
}