1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Client
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bncNetQueryRtp
|
---|
6 | *
|
---|
7 | * Purpose: Blocking Network Requests (NTRIP Version 2 with RTSP)
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 27-Dec-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <iostream>
|
---|
18 | #include <iomanip>
|
---|
19 |
|
---|
20 | #include "bncnetqueryrtp.h"
|
---|
21 |
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 | #define BNCVERSION "1.7"
|
---|
25 |
|
---|
26 | // Constructor
|
---|
27 | ////////////////////////////////////////////////////////////////////////////
|
---|
28 | bncNetQueryRtp::bncNetQueryRtp() {
|
---|
29 | _socket = 0;
|
---|
30 | _udpSocket = 0;
|
---|
31 | _eventLoop = new QEventLoop(this);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Destructor
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | bncNetQueryRtp::~bncNetQueryRtp() {
|
---|
37 | delete _eventLoop;
|
---|
38 | delete _socket;
|
---|
39 | delete _udpSocket;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //
|
---|
43 | ////////////////////////////////////////////////////////////////////////////
|
---|
44 | void bncNetQueryRtp::stop() {
|
---|
45 | _eventLoop->quit();
|
---|
46 | _status = finished;
|
---|
47 | }
|
---|
48 |
|
---|
49 | //
|
---|
50 | ////////////////////////////////////////////////////////////////////////////
|
---|
51 | void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
52 | }
|
---|
53 |
|
---|
54 | //
|
---|
55 | ////////////////////////////////////////////////////////////////////////////
|
---|
56 | void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
|
---|
57 |
|
---|
58 | // Wait Loop
|
---|
59 | // ---------
|
---|
60 | if (!_udpSocket->hasPendingDatagrams()) {
|
---|
61 | _eventLoop->exec();
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Append Data
|
---|
65 | // -----------
|
---|
66 | QByteArray datagram;
|
---|
67 | datagram.resize(_udpSocket->pendingDatagramSize());
|
---|
68 | _udpSocket->readDatagram(datagram.data(), datagram.size());
|
---|
69 |
|
---|
70 | cout << "Read Datagram: size = " << datagram.size() << endl;
|
---|
71 |
|
---|
72 | if (datagram.size() > 12) {
|
---|
73 | outData.append(datagram.mid(12));
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | // Connect to Caster, send the Request
|
---|
78 | ////////////////////////////////////////////////////////////////////////////
|
---|
79 | void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
80 |
|
---|
81 | const int timeOut = 5000;
|
---|
82 |
|
---|
83 | _status = running;
|
---|
84 |
|
---|
85 | delete _socket;
|
---|
86 | _socket = new QTcpSocket();
|
---|
87 |
|
---|
88 | // Default scheme
|
---|
89 | // --------------
|
---|
90 | QUrl urlLoc(url);
|
---|
91 | urlLoc.setScheme("rtsp");
|
---|
92 |
|
---|
93 | // Connect the Socket
|
---|
94 | // ------------------
|
---|
95 | QSettings settings;
|
---|
96 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
97 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
98 |
|
---|
99 | if ( proxyHost.isEmpty() ) {
|
---|
100 | _socket->connectToHost(urlLoc.host(), urlLoc.port());
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Send Request 1
|
---|
107 | // --------------
|
---|
108 | if (_socket->waitForConnected(timeOut)) {
|
---|
109 | QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
|
---|
110 | QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
|
---|
111 | QByteArray userAndPwd;
|
---|
112 |
|
---|
113 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
114 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
115 | passW.toAscii()).toBase64() + "\r\n";
|
---|
116 | }
|
---|
117 |
|
---|
118 | QByteArray clientPort = "7777"; // TODO: make it an option
|
---|
119 | delete _udpSocket;
|
---|
120 | _udpSocket = new QUdpSocket();
|
---|
121 | _udpSocket->bind(clientPort.toInt());
|
---|
122 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
123 |
|
---|
124 | QByteArray reqStr;
|
---|
125 | reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
126 | + "Cseq: 1\r\n"
|
---|
127 | + "Ntrip-Version: Ntrip/2.0\r\n"
|
---|
128 | + "Ntrip-Component: Ntripclient\r\n"
|
---|
129 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
130 | + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
|
---|
131 | + userAndPwd
|
---|
132 | + "\r\n";
|
---|
133 | _socket->write(reqStr, reqStr.length());
|
---|
134 |
|
---|
135 | // Read Server Answer 1
|
---|
136 | // --------------------
|
---|
137 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
138 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
139 | QTextStream in(_socket);
|
---|
140 | QByteArray session;
|
---|
141 | QString line = in.readLine();
|
---|
142 | while (!line.isEmpty()) {
|
---|
143 | if (line.indexOf("Session:") == 0) {
|
---|
144 | session = line.mid(9).toAscii();
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | line = in.readLine();
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Send Request 2
|
---|
151 | // --------------
|
---|
152 | if (!session.isEmpty()) {
|
---|
153 | reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
154 | + "Cseq: 2\r\n"
|
---|
155 | + "Session: " + session + "\r\n"
|
---|
156 | + "\r\n";
|
---|
157 | _socket->write(reqStr, reqStr.length());
|
---|
158 |
|
---|
159 | // Read Server Answer 2
|
---|
160 | // --------------------
|
---|
161 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
162 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
163 | QTextStream in(_socket);
|
---|
164 | line = in.readLine();
|
---|
165 | while (!line.isEmpty()) {
|
---|
166 | if (line.indexOf("200 OK") != -1) {
|
---|
167 | cout << "Connection Established" << endl;
|
---|
168 | return;
|
---|
169 | }
|
---|
170 | line = in.readLine();
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | delete _socket;
|
---|
180 | _socket = 0;
|
---|
181 | _status = error;
|
---|
182 | }
|
---|
183 |
|
---|