source: ntrip/trunk/BNC/src/bncnetqueryudp.cpp@ 10096

Last change on this file since 10096 was 9961, checked in by stuerze, 22 months ago

minor changes to be msvc compatible

File size: 4.8 KB
RevLine 
[1718]1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncNetQueryUdp
6 *
7 * Purpose: Blocking Network Requests (NTRIP Version 2 with plain UDP)
8 *
9 * Author: L. Mervart
10 *
11 * Created: 04-Feb-2009
12 *
[9959]13 * Changes:
[1718]14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <iomanip>
[1761]19#include <time.h>
[1718]20
[8252]21#include <QHostInfo>
22
[1718]23#include "bncnetqueryudp.h"
24#include "bncsettings.h"
[2011]25#include "bncversion.h"
[1718]26
27using namespace std;
28
[1761]29#define TIME_RESOLUTION 125
[1718]30
31// Constructor
32////////////////////////////////////////////////////////////////////////////
33bncNetQueryUdp::bncNetQueryUdp() {
[1719]34 _port = 0;
[1718]35 _udpSocket = 0;
36 _eventLoop = new QEventLoop(this);
[1756]37
[9961]38 _keepAlive[0] = 128;
39 _keepAlive[1] = 96;
[1761]40 for (int ii = 2; ii <=11; ii++) {
41 _keepAlive[ii] = 0;
[1756]42 }
[1718]43}
44
45// Destructor
46////////////////////////////////////////////////////////////////////////////
47bncNetQueryUdp::~bncNetQueryUdp() {
48 delete _eventLoop;
49 delete _udpSocket;
50}
51
[9959]52//
[1718]53////////////////////////////////////////////////////////////////////////////
54void bncNetQueryUdp::stop() {
55 _eventLoop->quit();
56 _status = finished;
57}
58
[9959]59//
[1718]60////////////////////////////////////////////////////////////////////////////
61void bncNetQueryUdp::slotKeepAlive() {
[1756]62 if (_udpSocket) {
63 _udpSocket->writeDatagram(_keepAlive, 12, _address, _port);
64 }
[1761]65 QTimer::singleShot(15000, this, SLOT(slotKeepAlive()));
[1718]66}
67
[9959]68//
[1718]69////////////////////////////////////////////////////////////////////////////
70void bncNetQueryUdp::waitForRequestResult(const QUrl&, QByteArray&) {
71}
72
[9959]73//
[1718]74////////////////////////////////////////////////////////////////////////////
75void bncNetQueryUdp::waitForReadyRead(QByteArray& outData) {
76
77 // Wait Loop
78 // ---------
79 if (!_udpSocket->hasPendingDatagrams()) {
80 _eventLoop->exec();
81 }
82
83 // Append Data
84 // -----------
85 QByteArray datagram;
86 datagram.resize(_udpSocket->pendingDatagramSize());
87 _udpSocket->readDatagram(datagram.data(), datagram.size());
88
89 if (datagram.size() > 12) {
90 outData.append(datagram.mid(12));
91 }
[1896]92 else {
93 _status = error;
94 }
[1718]95}
96
97// Connect to Caster, send the Request
98////////////////////////////////////////////////////////////////////////////
99void bncNetQueryUdp::startRequest(const QUrl& url, const QByteArray& gga) {
100
101 _status = running;
102
[1719]103 // Default scheme and path
104 // -----------------------
105 _url = url;
106 if (_url.scheme().isEmpty()) {
107 _url.setScheme("http");
108 }
109 if (_url.path().isEmpty()) {
110 _url.setPath("/");
111 }
112
113 _port = _url.port();
114
[1718]115 delete _udpSocket;
116 _udpSocket = new QUdpSocket();
[1723]117 _udpSocket->bind(0);
[1718]118 connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
119
120 QHostInfo hInfo = QHostInfo::fromName(url.host());
121
122 if (!hInfo.addresses().isEmpty()) {
[1719]123
124 _address = hInfo.addresses().first();
125
126 // Send Request
127 // ------------
[8204]128 QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
129 QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
[1719]130 QByteArray userAndPwd;
[9959]131
[1719]132 if(!uName.isEmpty() || !passW.isEmpty()) {
[8204]133 userAndPwd = "Authorization: Basic " + (uName.toLatin1() + ":" +
134 passW.toLatin1()).toBase64() + "\r\n";
[1719]135 }
[9959]136
[8204]137 QByteArray reqStr = "GET " + _url.path().toLatin1() + " HTTP/1.1\r\n"
138 + "Host: " + _url.host().toLatin1() + "\r\n"
[1724]139 + "Ntrip-Version: Ntrip/2.0\r\n"
[6574]140 + "User-Agent: NTRIP BNC/" BNCVERSION " (" BNC_OS ")\r\n";
[1732]141 if (!gga.isEmpty()) {
142 reqStr += "Ntrip-GGA: " + gga + "\r\n";
143 }
144 reqStr += userAndPwd + "Connection: close\r\n\r\n";
[9959]145
[9961]146 char* rtpbuffer;
147 rtpbuffer = (char*)malloc((12 + reqStr.size())*sizeof(char));
[1729]148 rtpbuffer[0] = 128;
149 rtpbuffer[1] = 97;
[1730]150 for (int jj = 2; jj <= 11; jj++) {
[1758]151 rtpbuffer[jj] = _keepAlive[jj];
[1730]152 }
[1729]153 for (int ii = 0; ii < reqStr.size(); ii++) {
[9959]154 rtpbuffer[12+ii] = reqStr[ii];
[1729]155 }
156
[1730]157 _udpSocket->writeDatagram(rtpbuffer, 12 + reqStr.size(), _address, _port);
[1761]158
159 // Wait for Reply, read Session Number
160 // -----------------------------------
161 QByteArray repl;
162 waitForReadyRead(repl);
163
164 QTextStream in(repl);
165 QString line = in.readLine();
166 while (!line.isEmpty()) {
167 if (line.indexOf("Session:") == 0) {
[1818]168 _session = line.mid(9).toUInt();
[1761]169 _keepAlive[ 8] = (_session >> 24) & 0xFF;
170 _keepAlive[ 9] = (_session >> 16) & 0xFF;
171 _keepAlive[10] = (_session >> 8) & 0xFF;
172 _keepAlive[11] = (_session) & 0xFF;
173 break;
174 }
175 line = in.readLine();
176 }
177
178 QTimer::singleShot(15000, this, SLOT(slotKeepAlive()));
[1718]179 }
180}
181
[6787]182void bncNetQueryUdp::keepAliveRequest(const QUrl& /* url */ , const QByteArray& /* gga */) {
183}
Note: See TracBrowser for help on using the repository browser.