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 |
|
---|
17 | #include <iostream>
|
---|
18 |
|
---|
19 | #include "bncnetqueryudp0.h"
|
---|
20 |
|
---|
21 | using namespace std;
|
---|
22 |
|
---|
23 | // Constructor
|
---|
24 | ////////////////////////////////////////////////////////////////////////////
|
---|
25 | bncNetQueryUdp0::bncNetQueryUdp0() {
|
---|
26 | _udpSocket = 0;
|
---|
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 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | // Destructor
|
---|
37 | ////////////////////////////////////////////////////////////////////////////
|
---|
38 | bncNetQueryUdp0::~bncNetQueryUdp0() {
|
---|
39 | delete _eventLoop;
|
---|
40 | delete _udpSocket;
|
---|
41 | }
|
---|
42 |
|
---|
43 | //
|
---|
44 | ////////////////////////////////////////////////////////////////////////////
|
---|
45 | void bncNetQueryUdp0::stop() {
|
---|
46 | _eventLoop->quit();
|
---|
47 | _status = finished;
|
---|
48 | }
|
---|
49 |
|
---|
50 | //
|
---|
51 | ////////////////////////////////////////////////////////////////////////////
|
---|
52 | void bncNetQueryUdp0::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
53 | }
|
---|
54 |
|
---|
55 | //
|
---|
56 | ////////////////////////////////////////////////////////////////////////////
|
---|
57 | void bncNetQueryUdp0::waitForReadyRead(QByteArray& outData) {
|
---|
58 |
|
---|
59 | // Wait Loop
|
---|
60 | // ---------
|
---|
61 | if (!_udpSocket->hasPendingDatagrams()) {
|
---|
62 | _eventLoop->exec();
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Append Data
|
---|
66 | // -----------
|
---|
67 | QByteArray datagram;
|
---|
68 | datagram.resize(_udpSocket->pendingDatagramSize());
|
---|
69 | _udpSocket->readDatagram(datagram.data(), datagram.size());
|
---|
70 |
|
---|
71 | outData.append(datagram);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // Connect to Caster, send the Request
|
---|
75 | ////////////////////////////////////////////////////////////////////////////
|
---|
76 | void bncNetQueryUdp0::startRequest(const QUrl& url, const QByteArray& /* gga */) {
|
---|
77 |
|
---|
78 | _status = running;
|
---|
79 |
|
---|
80 | // Default scheme and path
|
---|
81 | // -----------------------
|
---|
82 | _url = url;
|
---|
83 | if (_url.scheme().isEmpty()) {
|
---|
84 | _url.setScheme("http");
|
---|
85 | }
|
---|
86 | if (_url.path().isEmpty()) {
|
---|
87 | _url.setPath("/");
|
---|
88 | }
|
---|
89 |
|
---|
90 | delete _udpSocket; _udpSocket = 0;
|
---|
91 |
|
---|
92 | QHostInfo hInfo = QHostInfo::fromName(_url.host());
|
---|
93 | if (!hInfo.addresses().isEmpty()) {
|
---|
94 | _address = hInfo.addresses().first();
|
---|
95 | _udpSocket = new QUdpSocket();
|
---|
96 | _udpSocket->bind(_url.port());
|
---|
97 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
98 |
|
---|
99 | _udpSocket->writeDatagram(_keepAlive, 12, _address, _url.port());
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|