| 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() {
|
|---|
| 30 | _port = 0;
|
|---|
| 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 | cout << outData.data() << endl;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // Connect to Caster, send the Request
|
|---|
| 82 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 83 | void bncNetQueryUdp::startRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| 84 |
|
|---|
| 85 | _status = running;
|
|---|
| 86 |
|
|---|
| 87 | // Default scheme and path
|
|---|
| 88 | // -----------------------
|
|---|
| 89 | _url = url;
|
|---|
| 90 | if (_url.scheme().isEmpty()) {
|
|---|
| 91 | _url.setScheme("http");
|
|---|
| 92 | }
|
|---|
| 93 | if (_url.path().isEmpty()) {
|
|---|
| 94 | _url.setPath("/");
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | _port = _url.port();
|
|---|
| 98 |
|
|---|
| 99 | delete _udpSocket;
|
|---|
| 100 | _udpSocket = new QUdpSocket();
|
|---|
| 101 | _udpSocket->bind(0);
|
|---|
| 102 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
|---|
| 103 |
|
|---|
| 104 | QHostInfo hInfo = QHostInfo::fromName(url.host());
|
|---|
| 105 |
|
|---|
| 106 | if (!hInfo.addresses().isEmpty()) {
|
|---|
| 107 |
|
|---|
| 108 | _address = hInfo.addresses().first();
|
|---|
| 109 |
|
|---|
| 110 | // Send initial RTP packet for firewall handling
|
|---|
| 111 | // ---------------------------------------------
|
|---|
| 112 | char rtpbuffer[12];
|
|---|
| 113 | rtpbuffer[0] = 128;
|
|---|
| 114 | rtpbuffer[1] = 96;
|
|---|
| 115 | rtpbuffer[2] = 0;
|
|---|
| 116 | rtpbuffer[3] = 0;
|
|---|
| 117 | rtpbuffer[4] = 0;
|
|---|
| 118 | rtpbuffer[5] = 0;
|
|---|
| 119 | rtpbuffer[6] = 0;
|
|---|
| 120 | rtpbuffer[7] = 0;
|
|---|
| 121 | rtpbuffer[8] = 0;
|
|---|
| 122 | rtpbuffer[9] = 0;
|
|---|
| 123 | rtpbuffer[10] = 0;
|
|---|
| 124 | rtpbuffer[11] = 0;
|
|---|
| 125 |
|
|---|
| 126 | _udpSocket->writeDatagram(rtpbuffer, 12, _address, _port);
|
|---|
| 127 |
|
|---|
| 128 | // Send Request
|
|---|
| 129 | // ------------
|
|---|
| 130 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
|---|
| 131 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
|---|
| 132 | QByteArray userAndPwd;
|
|---|
| 133 |
|
|---|
| 134 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
|---|
| 135 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
|---|
| 136 | passW.toAscii()).toBase64() + "\r\n";
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | QByteArray reqStr = "GET " + _url.path().toAscii() + " HTTP/1.1\r\n"
|
|---|
| 140 | + "Host: " + _url.host().toAscii() + "\r\n"
|
|---|
| 141 | + "Ntrip-Version: Ntrip/2.0\r\n"
|
|---|
| 142 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
|---|
| 143 | + "Connection: close\r\n"
|
|---|
| 144 | + userAndPwd
|
|---|
| 145 | + "\r\n";
|
|---|
| 146 |
|
|---|
| 147 | //// // NMEA string to handle VRS stream
|
|---|
| 148 | //// // --------------------------------
|
|---|
| 149 | //// if (!gga.isEmpty()) {
|
|---|
| 150 | //// reqStr += gga + "\r\n";
|
|---|
| 151 | //// }
|
|---|
| 152 |
|
|---|
| 153 | cout << "reqStr > " << reqStr.data() << "<" << endl;
|
|---|
| 154 |
|
|---|
| 155 | rtpbuffer[1] = 97;
|
|---|
| 156 | QByteArray buffer = QByteArray(rtpbuffer) + reqStr;
|
|---|
| 157 |
|
|---|
| 158 | _udpSocket->writeDatagram(buffer, _address, _port);
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|