[1779] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Client
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bncNetQueryUdp0
|
---|
| 6 | *
|
---|
| 7 | * Purpose: Blocking Network Requests (plain UDP, no NTRIP)
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 04-Feb-2009
|
---|
| 12 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
[1785] | 17 | #include <iostream>
|
---|
| 18 |
|
---|
[1779] | 19 | #include "bncnetqueryudp0.h"
|
---|
| 20 |
|
---|
| 21 | using namespace std;
|
---|
| 22 |
|
---|
| 23 | // Constructor
|
---|
| 24 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 25 | bncNetQueryUdp0::bncNetQueryUdp0() {
|
---|
| 26 | _udpSocket = 0;
|
---|
[1785] | 27 | _eventLoop = new QEventLoop(this);
|
---|
| 28 |
|
---|
| 29 | _keepAlive[ 0] = 128;
|
---|
| 30 | _keepAlive[ 1] = 96;
|
---|
| 31 | for (int ii = 2; ii <=11; ii++) {
|
---|
| 32 | _keepAlive[ii] = 0;
|
---|
| 33 | }
|
---|
[1779] | 34 | }
|
---|
| 35 |
|
---|
| 36 | // Destructor
|
---|
| 37 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 38 | bncNetQueryUdp0::~bncNetQueryUdp0() {
|
---|
[1785] | 39 | delete _eventLoop;
|
---|
[1779] | 40 | delete _udpSocket;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | //
|
---|
| 44 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 45 | void bncNetQueryUdp0::stop() {
|
---|
[1785] | 46 | _eventLoop->quit();
|
---|
[1783] | 47 | _status = finished;
|
---|
[1779] | 48 | }
|
---|
| 49 |
|
---|
| 50 | //
|
---|
| 51 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 52 | void bncNetQueryUdp0::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | //
|
---|
| 56 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 57 | void bncNetQueryUdp0::waitForReadyRead(QByteArray& outData) {
|
---|
[1785] | 58 |
|
---|
| 59 | // Wait Loop
|
---|
| 60 | // ---------
|
---|
| 61 | if (!_udpSocket->hasPendingDatagrams()) {
|
---|
| 62 | _eventLoop->exec();
|
---|
[1779] | 63 | }
|
---|
[1785] | 64 |
|
---|
| 65 | // Append Data
|
---|
| 66 | // -----------
|
---|
| 67 | QByteArray datagram;
|
---|
| 68 | datagram.resize(_udpSocket->pendingDatagramSize());
|
---|
| 69 | _udpSocket->readDatagram(datagram.data(), datagram.size());
|
---|
| 70 |
|
---|
| 71 | if (datagram.size() > 0) {
|
---|
| 72 | outData.append(datagram);
|
---|
| 73 | }
|
---|
[1779] | 74 | }
|
---|
| 75 |
|
---|
| 76 | // Connect to Caster, send the Request
|
---|
| 77 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1783] | 78 | void bncNetQueryUdp0::startRequest(const QUrl& url, const QByteArray& /* gga */) {
|
---|
[1779] | 79 |
|
---|
| 80 | _status = running;
|
---|
| 81 |
|
---|
| 82 | // Default scheme and path
|
---|
| 83 | // -----------------------
|
---|
| 84 | _url = url;
|
---|
| 85 | if (_url.scheme().isEmpty()) {
|
---|
| 86 | _url.setScheme("http");
|
---|
| 87 | }
|
---|
| 88 | if (_url.path().isEmpty()) {
|
---|
| 89 | _url.setPath("/");
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[1785] | 92 | delete _udpSocket; _udpSocket = 0;
|
---|
| 93 |
|
---|
| 94 | QHostInfo hInfo = QHostInfo::fromName(_url.host());
|
---|
| 95 | if (!hInfo.addresses().isEmpty()) {
|
---|
| 96 | _address = hInfo.addresses().first();
|
---|
| 97 | _udpSocket = new QUdpSocket();
|
---|
| 98 | _udpSocket->bind(0);
|
---|
| 99 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 100 |
|
---|
| 101 | _udpSocket->writeDatagram(_keepAlive, 12, _address, _url.port());
|
---|
| 102 | }
|
---|
[1779] | 103 | }
|
---|
| 104 |
|
---|