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