forked from venkatarun95/genericCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcc-tcp.cc
208 lines (172 loc) · 5.66 KB
/
pcc-tcp.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
200
201
202
203
204
205
206
207
208
#ifndef WIN32
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <netdb.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#include <wspiapi.h>
#endif
#include <iostream>
#include "udt/udt.h"
#include "pcc.h"
#include <cassert>
#include <chrono>
#include <mutex>
#include "pcc-tcp.hh"
using namespace std;
#ifndef WIN32
void* monitor(void*);
#else
DWORD WINAPI monitor(LPVOID);
#endif
double PCC_TCP::current_timestamp( chrono::high_resolution_clock::time_point &start_time_point ){
using namespace chrono;
high_resolution_clock::time_point cur_time_point = high_resolution_clock::now();
return duration_cast<duration<double>>(cur_time_point - start_time_point).count()*1000; //convert to milliseconds, because that is the scale on which the rats have been trained
}
void PCC_TCP::send_data( double flow_size, bool byte_switched, int flow_id, int src_id )
{
assert(byte_switched);
// use this function to initialize the UDT library
UDT::startup();
flow_id = flow_id; src_id = src_id;
struct addrinfo hints, *local, *peer;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
//hints.ai_socktype = SOCK_DGRAM;
if (0 != getaddrinfo(NULL, "9000", &hints, &local))
{
cout << "incorrect network address.\n" << endl;
return;
}
UDTSOCKET client = UDT::socket(local->ai_family, local->ai_socktype, local->ai_protocol);
// UDT Options
UDT::setsockopt(client, 0, UDT_CC, new CCCFactory<BBCC>, sizeof(CCCFactory<BBCC>));
//UDT::setsockopt(client, 0, UDT_MSS, new int(9000), sizeof(int));
//UDT::setsockopt(client, 0, UDT_SNDBUF, new int(10000000), sizeof(int));
//UDT::setsockopt(client, 0, UDP_SNDBUF, new int(10000000), sizeof(int));
// Windows UDP issue
// For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
#ifdef WIN32
UDT::setsockopt(client, 0, UDT_MSS, new int(1052), sizeof(int));
#endif
/*
UDT::setsockopt(client, 0, UDT_RENDEZVOUS, new bool(true), sizeof(bool));
if (UDT::ERROR == UDT::bind(client, local->ai_addr, local->ai_addrlen))
{
cout << "bind: " << UDT::getlasterror().getErrorMessage() << endl;
return 0;
}
*/
freeaddrinfo(local);
if (0 != getaddrinfo(dstaddr.c_str(), to_string( dstport ).c_str(), &hints, &peer))
{
cout << "incorrect server/peer address. " << dstaddr << ":" << dstport << endl;
return;
}
// connect to the server, implict bind
if (UDT::ERROR == UDT::connect(client, peer->ai_addr, peer->ai_addrlen))
{
cout << "connect: " << UDT::getlasterror().getErrorMessage() << endl;
return;
}
freeaddrinfo(peer);
// using CC method
BBCC* cchandle = NULL;
int temp;
UDT::getsockopt(client, 0, UDT_CC, &cchandle, &temp);
// if (NULL != cchandle)
// cchandle->setRate(1);
int size = flow_size; //CHANGE - by venkat
char* data = new char[size];
// used to allow this function to return after the monitor has printed
mutex run_monitor;
run_monitor.lock();
struct {
UDTSOCKET* client;
mutex* run_monitor;
} compound = {&client, &run_monitor};
#ifndef WIN32
pthread_create(new pthread_t, NULL, monitor, &compound);
#else
CreateThread(NULL, 0, monitor, &client, 0, NULL, &run_monitor);
#endif
chrono::high_resolution_clock::time_point start_time_point = chrono::high_resolution_clock::now();
// while ( )
// {
int ssize = 0;
int ss;
while (ssize < size)
{
if (UDT::ERROR == (ss = UDT::send(client, data + ssize, size - ssize, 0)))
{
cout << "send:" << UDT::getlasterror().getErrorMessage() << endl;
break;
}
ssize += ss;
}
bool tmp = false;
UDT::getsockopt(client, 0, UDT_SNDSYN, &tmp, new int(0));
// cout << "Sockopt: " << tmp << endl;
// cout << current_timestamp( start_time_point ) << " " << duration << endl;
// if (ssize < size)
// break;
// }
UDT::close(client);
run_monitor.try_lock(); // allow monitor to run once
delete [] data;
return;
}
#ifndef WIN32
void* monitor(void* s)
#else
DWORD WINAPI monitor(LPVOID s)
#endif
{
struct TmpCompound{
UDTSOCKET* client;
mutex* run_monitor; // useless for now
} compound = *(TmpCompound*)s;
UDTSOCKET u = *(UDTSOCKET*)compound.client;
UDT::TRACEINFO perf;
// cout << "SendRate(Mb/s)\tRTT(ms)\tCWnd\tPktSndPeriod(us)\tRecvACK\tRecvNAK" << endl;
while (true)//!((mutex*)compound.run_monitor)->try_lock())
{
#ifndef WIN32
usleep(100000);
#else
Sleep(100);
#endif
if (UDT::ERROR == UDT::perfmon(u, &perf))
{
// cout << "perfmon: " << UDT::getlasterror().getErrorMessage() << endl;
break;
}
// cout << perf.mbpsSendRate << "\t\t"
// << perf.msRTT << "\t"
// << perf.pktSentTotal << "\t"
// << perf.pktSndLossTotal << "\t\t\t"
// << perf.pktRecvACKTotal << "\t"
// << perf.pktRecvNAKTotal << endl;
};
std::cout<<"\n\nData Successfully Transmitted\n\tThroughput: "<<\
perf.mbpsRecvRate/8<<" bytes/sec\n\tAverage Delay: "<<perf.msRTT/100.0\
<<" sec/packet\n";
double avg_throughput = -1;
double avg_delay = -1;
std::cout<<"\n\tAvg. Throughput: "<<avg_throughput<<" bytes/sec\n\tAverage Delay: "<<avg_delay<<" sec/packet\n";
((mutex*)compound.run_monitor)->unlock();
#ifndef WIN32
return NULL;
#else
return 0;
#endif
}
PCC_TCP::~PCC_TCP() {
// use this function to release the UDT library
UDT::cleanup();
}