-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyIkcp.cpp
147 lines (123 loc) · 3.08 KB
/
MyIkcp.cpp
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
/*
* =====================================================================================
*
* Filename: MyIkcp.cpp
*
* Description:
*
* Version: 1.0
* Created: 09/30/2017 11:16:04 AM
* Last Modified: 09/30/2017 11:16:04 AM
* Revision: none
* Compiler: gcc
*
* Author: zt (),
* Organization:
*
* =====================================================================================
*/
#include "MyIkcp.h"
int MyIkcp::Udpoutput ( const char* buf, int len, ikcpcb* kcp, void* user )
{
UdpSocket* p = reinterpret_cast<UdpSocket*> ( user );
p->Send ( reinterpret_cast<const uint8_t*> ( buf ), len );
return 0;
}
MyIkcp::MyIkcp ( uint32_t conv, uint16_t localport, std::string remoteip, uint16_t remoteport )
: m_conv ( conv ), m_localport ( localport ),
m_remoteip ( remoteip ), m_remoteport ( remoteport ),
m_ikcp ( NULL ), m_udpSocket ( NULL )
{
Init();
}
MyIkcp::~MyIkcp()
{
DeInit();
}
void MyIkcp::Init()
{
m_udpSocket = new UdpSocket ( m_localport, m_remoteip, m_remoteport );
m_ikcp = ikcp_create ( m_conv, reinterpret_cast< void* > ( m_udpSocket ) );
m_ikcp->output = Udpoutput;
ikcp_wndsize ( m_ikcp, 128, 128 );
ikcp_nodelay ( m_ikcp, 1, 10, 2, 1 );
m_ikcp->rx_minrto = 10;
m_ikcp->fastresend = 1;
}
void MyIkcp::DeInit()
{
ikcp_release ( m_ikcp );
delete m_udpSocket;
}
int MyIkcp::Recv ( std::vector<uint8_t>& vecuint8 )
{
std::vector<uint8_t> vec ( 2048, 0 );
int len = 0;
int readsum = 0;
vecuint8.resize ( 0 );
vecuint8.reserve ( 4096 );
do
{
if ( ( len = m_udpSocket->Recv ( &vec[0], vec.size() ) ) > 0 )
{
ikcp_input ( m_ikcp, reinterpret_cast<char*> ( &vec[0] ), len );
readsum += len;
vecuint8.insert ( vecuint8.end(), vec.begin(), vec.begin() + len );
}
}
while ( len > 0 );
return readsum;
}
int MyIkcp::Send ( std::vector<uint8_t>& vecuint8 )
{
int len = 0;
int sendsum = 0;
do
{
if ( ( len = ikcp_send ( m_ikcp, reinterpret_cast<char*> ( &vecuint8[0] + sendsum ), vecuint8.size() - sendsum ) ) > 0 )
sendsum += len;
}
while ( len > 0 );
return sendsum;
}
int MyIkcp::Recv ( uint8_t* buf, uint32_t size )
{
int len = 0;
int readsum = 0;
do
{
if ( ( len = m_udpSocket->Recv ( buf, size ) ) > 0 )
{
ikcp_input ( m_ikcp, reinterpret_cast<char*> ( buf ), len );
readsum += len;
}
}
while ( len > 0 );
return readsum;
}
int MyIkcp::Send ( uint8_t* buf, uint32_t size )
{
int len = 0;
int sendsum = 0;
do
{
if ( ( len = ikcp_send ( m_ikcp, reinterpret_cast<char*> ( buf ), size ) ) > 0 )
sendsum += len;
}
while ( len > 0 );
return sendsum;
}
#ifdef TESTKCP
int main ( int argc, char* argv[] )
{
std::vector<uint8_t> vecuint8 ;
vecuint8.resize ( 0 );
vecuint8.reserve ( 2048 );
printf ( "size=%ld %ld\n", vecuint8.size(), vecuint8.capacity() );
vecuint8.resize ( 100 );
printf ( "size=%ld %ld\n", vecuint8.size(), vecuint8.capacity() );
vecuint8.resize ( 200, 0 );
printf ( "size=%ld %ld\n", vecuint8.size(), vecuint8.capacity() );
return 0;
}
#endif