source: flair-src/trunk/tools/FlairGCS/src/UdtSocket.cpp@ 247

Last change on this file since 247 was 247, checked in by Sanahuja Guillaume, 5 years ago

improve gcs disconnections

File size: 9.5 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5#include "UdtSocket.h"
6#include "ConnectionLayout.h"
7#include <stdio.h>
8#include <stdlib.h>
9#include <QApplication>
10#include <QtEndian>
11#include <QDir>
12#include <QDate>
13#include "communication.h"
14#include <zlib.h>
15#include <assert.h>
16#include <QThread>
17#define COMPRESS_CHUNK 1024*100
18
19#ifndef WIN32
20#include <arpa/inet.h>
21#else
22#include <winsock2.h>
23#include <ws2tcpip.h>
24#endif
25
26#define COMPUTE_UDT_STATS_TIMER 2000
27#define HEARTBEAT_TIMER 200
28
29using namespace std;
30
31UdtSocket::UdtSocket(UDTSOCKET socket,QString name): QObject() {
32 this->socket = socket;
33 this->name=name;
34 stop = false;
35 destroySocket=true;
36 socketType=unknown;
37 total_received=0;
38
39 bool blocking = false;
40 if (UDT::setsockopt(socket, 0, UDT_RCVSYN, &blocking, sizeof(bool)) != 0)
41 fprintf(stderr,"UDT::setsockopt error (UDT_RCVSYN)\n");
42
43 heartbeat_timer = new QTimer(this);
44 connect(heartbeat_timer, SIGNAL(timeout()), this, SLOT(heartbeat()));
45 heartbeat_timer->start(HEARTBEAT_TIMER);
46
47 udtstats_timer = new QTimer(this);
48 connect(udtstats_timer, SIGNAL(timeout()), this, SLOT(getUTDStats()));
49 udtstats_timer->start(COMPUTE_UDT_STATS_TIMER);
50}
51
52UdtSocket::~UdtSocket() {
53 heartbeat_timer->stop();
54 udtstats_timer->stop();
55 if(destroySocket) UDT::close(socket);
56fprintf(stderr,"fin udt\n");
57}
58
59void UdtSocket::setName(QString name) {
60 fprintf(stderr," %s is %s\n",this->name.toLocal8Bit().constData(),name.toLocal8Bit().constData());
61 this->name=name;
62}
63
64// send signal to uav, to check connectivity through a watchdog
65// this is necessary because we use udt (udp based), and we cannot check
66// disconnection of ground station
67void UdtSocket::heartbeat(void) {
68 char data = WATCHDOG_HEADER;
69 quint64 sent=write(&data, 1,HEARTBEAT_TIMER,false);
70 if (sent != 1 && UDT::getlasterror().getErrorCode() == 2001) {
71 heartbeat_timer->stop();
72 }
73
74}
75
76void UdtSocket::getUTDStats(void) {
77 float rxRate=((float)total_received/(COMPUTE_UDT_STATS_TIMER/1000))/1000;//in Ko/s
78 total_received=0;
79
80 UDT::TRACEINFO perf;
81 if (UDT::ERROR == UDT::perfmon(socket, &perf)) {
82 fprintf(stderr,"perfmon: %s\n",UDT::getlasterror().getErrorMessage());
83 }/* else {
84 fprintf(stderr,"%s socket stats:\n",name.toLocal8Bit().constData());
85 fprintf(stderr,"total number of sent packets, including retransmissions: %i\n",perf.pktSentTotal);
86 fprintf(stderr,"total number of received packets: %i\n",perf.pktRecvTotal);
87 fprintf(stderr,"total number of lost packets, measured in the sending side: %i\n",perf.pktSndLossTotal);
88 fprintf(stderr,"total number of lost packets, measured in the receiving side: %i\n",perf.pktRcvLossTotal);
89 fprintf(stderr,"total number of retransmitted packets, measured in the sending side: %i\n",perf.pktRetransTotal);
90 fprintf(stderr,"total number of sent ACK packets: %i\n",perf.pktSentACKTotal);
91 fprintf(stderr,"total number of received ACK packets: %i\n",perf.pktRecvACKTotal);
92 fprintf(stderr,"total number of sent NAK packets: %i\n",perf.pktSentNAKTotal);
93 fprintf(stderr,"total number of received NAK packets: %i\n",perf.pktRecvNAKTotal);
94 fprintf(stderr,"round trip time: %fms\n",perf.msRTT);
95
96 }*/
97 stats=QString("rx rate %1kB/s, round trip %2ms, lost packets %3").arg(rxRate,0,'f',3).arg(perf.msRTT,0,'f',3).arg(perf.pktSndLossTotal);
98 UDTStats(stats);
99
100}
101
102QString UdtSocket::getUDTStats() {
103 return stats;
104}
105
106void UdtSocket::kill(void) {
107 stop = true;
108 deleteLater();
109}
110
111void UdtSocket::receiveData(void) {
112 int buf_size;
113 int opt_size;
114 UDT::getsockopt(socket, 0, UDT_RCVBUF, &buf_size, &opt_size);
115 char *buf = (char *)malloc(buf_size);
116 if (!buf) {
117 fprintf(stderr,"error malloc UdtSocket::receiveData buffer\n");
118 return;
119 }
120 char *uncompressbuf=(char *)malloc(COMPRESS_CHUNK);
121 if (!uncompressbuf) {
122 fprintf(stderr,"error malloc UdtSocket::receiveData uncompress buffer\n");
123 free(buf);
124 return;
125 }
126 //ffprintf(stderr,stderr,"receiveData %x\n",thread());
127
128 while (!stop) {
129 QCoreApplication::processEvents();
130
131 //we need to use epoll?
132 //tests are showing that UDT::recvmsg is waiting for the entire timeout before returning
133 //note that in flair::core the behaviour of UDT::recvmsg timeout is as expected
134 //is it a difference between client and server??
135 //tests also show that we need to recreate the eid every time, otherwise wait returns immediately
136 int eid = UDT::epoll_create();
137 if (eid < 0) {
138 fprintf(stderr,"%s: epoll_create error (%s)\n",name.toLocal8Bit().constData(),UDT::getlasterror().getErrorMessage());
139 }
140
141 if (UDT::epoll_add_usock(eid, socket) < 0) {
142 if (UDT::getlasterror().getErrorCode() == 5004) {
143 fprintf(stderr,"disconnected from %s\n",name.toLocal8Bit().constData());
144 heartbeat_timer->stop();
145 deleteLater();
146 stop=true;;
147 } else {
148 fprintf(stderr,"%s: epoll_add_usock error (%s)\n",name.toLocal8Bit().constData(),UDT::getlasterror().getErrorMessage());
149 }
150 }
151
152 int num = 1;
153 UDTSOCKET readfds;
154
155 int rv = UDT::epoll_wait2(eid, &readfds, &num,NULL, NULL,100);
156 if (rv == -1) {
157 if (UDT::getlasterror().getErrorCode() != 6003)
158 fprintf(stderr,"prob %i\n", UDT::getlasterror().getErrorCode());
159 } else if(readfds==socket && num==1 && rv==1) {
160
161 int size;
162 do {
163 size=UDT::recvmsg(socket, buf, buf_size);
164
165 //fprintf(stderr,"recu %i\n",size);
166 if (size > 0) {
167 total_received+=size;
168
169 switch ((unsigned char)buf[0]) {
170 case ZLIB_HEADER: {
171 ssize_t out_size;
172 uncompressBuffer(buf, size, uncompressbuf, &out_size);
173 if((unsigned char)uncompressbuf[0]==XML_HEADER && socketType==unknown) {
174 socketType=gui;
175 QString remoteName=ConnectionLayout::getDocRootName(uncompressbuf, out_size);
176 setName(remoteName);
177 emit newConnectionLayout(remoteName);//connection is Qt::BlockingQueuedConnection
178 }
179 emit dataReady(uncompressbuf, out_size);//connection is Qt::BlockingQueuedConnection, as we have only one buffer
180 break;
181 }
182 case START_SENDING_FILES: {
183 if((unsigned char)uncompressbuf[0]==XML_HEADER && socketType==unknown) {
184 socketType=log;
185 }
186 setName("log files");
187 heartbeat_timer->stop();
188 emit newFileUI(socket);
189 deleteLater();
190 stop=true;
191 destroySocket=false;
192 break;
193 }
194 case XML_HEADER:
195 if(socketType==unknown) {
196 socketType=gui;
197 QString remoteName=ConnectionLayout::getDocRootName(buf, size );
198 setName(remoteName);
199 emit newConnectionLayout(remoteName);
200 }
201 case DATAS_BIG_ENDIAN:
202 case DATAS_LITTLE_ENDIAN:
203 emit dataReady(buf, size );
204 break;
205 case CLOSING_CONNECTION:
206 fprintf(stderr,"%s, connection closed\n",name.toLocal8Bit().constData());
207 emit dataReady(buf, size );
208 stop = true;
209 heartbeat_timer->stop();
210 deleteLater();
211 break;
212 default:
213 fprintf(stderr,"trame non supportée %x\n", buf[0]);
214 }
215 } else {
216 //not necessary to check, watchdog (heartbeat_timer) can do it
217 //if(UDT::getlasterror().getErrorCode()!=6002 && !stop)
218 //fprintf(stderr,"udt socket: %s\n",UDT::getlasterror().getErrorMessage());
219 //UDT::close(socket);//si deconnecté
220 //free(buf);
221 //break;
222 }
223 } while(size>0);
224 } else {
225 //not necessary to check, watchdog (heartbeat_timer) can do it
226 //fprintf(stderr,"udt socket: %s\n",UDT::getlasterror().getErrorMessage());
227 }
228 UDT::epoll_remove_usock(eid, socket);
229 UDT::epoll_release(eid);
230 }
231
232 free(uncompressbuf);
233 free(buf);
234 thread()->quit();
235}
236
237qint64 UdtSocket::write(const char *buf, qint64 size,int ttl,bool inOrder) {
238 qint64 sent = UDT::sendmsg(socket, buf, size, ttl, inOrder);
239 if (sent != size) {
240 fprintf(stderr,"%s, error writting to udt (%s)\n",name.toLocal8Bit().constData(), UDT::getlasterror().getErrorMessage());
241 if (UDT::getlasterror().getErrorCode() == 2001) {
242 fprintf(stderr,"%s, closing connection\n",name.toLocal8Bit().constData());
243 stop = true;
244 heartbeat_timer->stop();
245 deleteLater();
246 }
247 }
248 return sent;
249}
250
251int UdtSocket::uncompressBuffer(char *in, ssize_t in_size, char *out,
252 ssize_t *out_size) {
253 int ret;
254 unsigned have;
255 z_stream strm;
256
257 // allocate inflate state
258 strm.zalloc = Z_NULL;
259 strm.zfree = Z_NULL;
260 strm.opaque = Z_NULL;
261 strm.avail_in = 0;
262 strm.next_in = Z_NULL;
263 ret = inflateInit(&strm);
264 if (ret != Z_OK)
265 return ret;
266
267 strm.avail_in = in_size;
268 strm.next_in = (unsigned char *)in;
269 strm.avail_out = COMPRESS_CHUNK;
270 strm.next_out = (unsigned char *)out;
271
272 ret = inflate(&strm, Z_NO_FLUSH);
273 assert(ret != Z_STREAM_ERROR); // state not clobbered
274 switch (ret) {
275 case Z_NEED_DICT:
276 ret = Z_DATA_ERROR; // and fall through
277 case Z_DATA_ERROR:
278 case Z_MEM_ERROR:
279 (void)inflateEnd(&strm);
280 return ret;
281 }
282 have = COMPRESS_CHUNK - strm.avail_out;
283 *out_size = have;
284
285 // clean up and return
286 (void)inflateEnd(&strm);
287 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
288}
Note: See TracBrowser for help on using the repository browser.