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

Last change on this file since 9959 was 9959, checked in by stuerze, 15 months ago

minor changes to be msvc compatible

File size: 4.7 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
[1761]38 _keepAlive[ 0] = 128;
39 _keepAlive[ 1] = 96;
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
146 std::vector <char> rtpbuffer(12 + reqStr.size());
[1729]147 rtpbuffer[0] = 128;
148 rtpbuffer[1] = 97;
[1730]149 for (int jj = 2; jj <= 11; jj++) {
[1758]150 rtpbuffer[jj] = _keepAlive[jj];
[1730]151 }
[1729]152 for (int ii = 0; ii < reqStr.size(); ii++) {
[9959]153 rtpbuffer[12+ii] = reqStr[ii];
[1729]154 }
155
[1730]156 _udpSocket->writeDatagram(rtpbuffer, 12 + reqStr.size(), _address, _port);
[1761]157
158 // Wait for Reply, read Session Number
159 // -----------------------------------
160 QByteArray repl;
161 waitForReadyRead(repl);
162
163 QTextStream in(repl);
164 QString line = in.readLine();
165 while (!line.isEmpty()) {
166 if (line.indexOf("Session:") == 0) {
[1818]167 _session = line.mid(9).toUInt();
[1761]168 _keepAlive[ 8] = (_session >> 24) & 0xFF;
169 _keepAlive[ 9] = (_session >> 16) & 0xFF;
170 _keepAlive[10] = (_session >> 8) & 0xFF;
171 _keepAlive[11] = (_session) & 0xFF;
172 break;
173 }
174 line = in.readLine();
175 }
176
177 QTimer::singleShot(15000, this, SLOT(slotKeepAlive()));
[1718]178 }
179}
180
[6787]181void bncNetQueryUdp::keepAliveRequest(const QUrl& /* url */ , const QByteArray& /* gga */) {
182}
Note: See TracBrowser for help on using the repository browser.