[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 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <iostream>
|
---|
| 18 | #include <iomanip>
|
---|
| 19 |
|
---|
| 20 | #include "bncnetqueryudp.h"
|
---|
| 21 | #include "bncsettings.h"
|
---|
| 22 |
|
---|
| 23 | using namespace std;
|
---|
| 24 |
|
---|
| 25 | #define BNCVERSION "1.7"
|
---|
| 26 |
|
---|
| 27 | // Constructor
|
---|
| 28 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 29 | bncNetQueryUdp::bncNetQueryUdp() {
|
---|
[1719] | 30 | _port = 0;
|
---|
[1718] | 31 | _udpSocket = 0;
|
---|
| 32 | _eventLoop = new QEventLoop(this);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | // Destructor
|
---|
| 36 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 37 | bncNetQueryUdp::~bncNetQueryUdp() {
|
---|
| 38 | delete _eventLoop;
|
---|
| 39 | delete _udpSocket;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | //
|
---|
| 43 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 44 | void bncNetQueryUdp::stop() {
|
---|
| 45 | _eventLoop->quit();
|
---|
| 46 | _status = finished;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | //
|
---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 51 | void bncNetQueryUdp::slotKeepAlive() {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | //
|
---|
| 55 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 56 | void bncNetQueryUdp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | //
|
---|
| 60 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 61 | void bncNetQueryUdp::waitForReadyRead(QByteArray& outData) {
|
---|
| 62 |
|
---|
| 63 | // Wait Loop
|
---|
| 64 | // ---------
|
---|
| 65 | if (!_udpSocket->hasPendingDatagrams()) {
|
---|
| 66 | _eventLoop->exec();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // Append Data
|
---|
| 70 | // -----------
|
---|
| 71 | QByteArray datagram;
|
---|
| 72 | datagram.resize(_udpSocket->pendingDatagramSize());
|
---|
| 73 | _udpSocket->readDatagram(datagram.data(), datagram.size());
|
---|
| 74 |
|
---|
| 75 | if (datagram.size() > 12) {
|
---|
| 76 | outData.append(datagram.mid(12));
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | // Connect to Caster, send the Request
|
---|
| 81 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 82 | void bncNetQueryUdp::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
| 83 |
|
---|
| 84 | _status = running;
|
---|
| 85 |
|
---|
[1719] | 86 | // Default scheme and path
|
---|
| 87 | // -----------------------
|
---|
| 88 | _url = url;
|
---|
| 89 | if (_url.scheme().isEmpty()) {
|
---|
| 90 | _url.setScheme("http");
|
---|
| 91 | }
|
---|
| 92 | if (_url.path().isEmpty()) {
|
---|
| 93 | _url.setPath("/");
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | _port = _url.port();
|
---|
| 97 |
|
---|
[1718] | 98 | delete _udpSocket;
|
---|
| 99 | _udpSocket = new QUdpSocket();
|
---|
[1723] | 100 | _udpSocket->bind(0);
|
---|
[1718] | 101 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 102 |
|
---|
| 103 | QHostInfo hInfo = QHostInfo::fromName(url.host());
|
---|
| 104 |
|
---|
| 105 | if (!hInfo.addresses().isEmpty()) {
|
---|
[1719] | 106 |
|
---|
| 107 | _address = hInfo.addresses().first();
|
---|
| 108 |
|
---|
| 109 | // Send initial RTP packet for firewall handling
|
---|
| 110 | // ---------------------------------------------
|
---|
| 111 | char rtpbuffer[12];
|
---|
[1721] | 112 | rtpbuffer[0] = 128;
|
---|
| 113 | rtpbuffer[1] = 96;
|
---|
| 114 | rtpbuffer[2] = 0;
|
---|
| 115 | rtpbuffer[3] = 0;
|
---|
| 116 | rtpbuffer[4] = 0;
|
---|
| 117 | rtpbuffer[5] = 0;
|
---|
| 118 | rtpbuffer[6] = 0;
|
---|
| 119 | rtpbuffer[7] = 0;
|
---|
| 120 | rtpbuffer[8] = 0;
|
---|
| 121 | rtpbuffer[9] = 0;
|
---|
| 122 | rtpbuffer[10] = 0;
|
---|
| 123 | rtpbuffer[11] = 0;
|
---|
[1719] | 124 |
|
---|
| 125 | _udpSocket->writeDatagram(rtpbuffer, 12, _address, _port);
|
---|
| 126 |
|
---|
| 127 | // Send Request
|
---|
| 128 | // ------------
|
---|
| 129 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
| 130 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
| 131 | QByteArray userAndPwd;
|
---|
| 132 |
|
---|
| 133 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
| 134 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
| 135 | passW.toAscii()).toBase64() + "\r\n";
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | QByteArray reqStr = "GET " + _url.path().toAscii() + " HTTP/1.0\r\n"
|
---|
| 139 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
| 140 | + userAndPwd + "\r\n";
|
---|
| 141 |
|
---|
| 142 | // NMEA string to handle VRS stream
|
---|
| 143 | // --------------------------------
|
---|
| 144 | if (!gga.isEmpty()) {
|
---|
| 145 | reqStr += gga + "\r\n";
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[1720] | 148 | rtpbuffer[1] = 97;
|
---|
| 149 | QByteArray buffer = QByteArray(rtpbuffer) + reqStr;
|
---|
| 150 |
|
---|
| 151 | _udpSocket->writeDatagram(buffer, _address, _port);
|
---|
[1718] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|