Changeset 9707 in ntrip


Ignore:
Timestamp:
May 4, 2022, 2:20:06 PM (2 weeks ago)
Author:
stuerze
Message:

initial Ntrip verion 2 upload implementation

Location:
trunk/BNC/src/upload
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/src/upload/bncuploadcaster.cpp

    r8904 r9707  
    2020#include "bnccore.h"
    2121#include "bnctableitem.h"
     22#include "bncsettings.h"
    2223
    2324using namespace std;
     
    3233                                 int rate) {
    3334  _mountpoint    = mountpoint;
    34   _outHost      = outHost;
    35   _outPort      = outPort;
     35  _casterOutHost = outHost;
     36  _casterOutPort = outPort;
    3637  _ntripVersion  = ntripVersion;
     38  (_ntripVersion == "2s") ?  _secure = true : _secure = false;
     39  _postExtension = "";
    3740  _userName      = userName;
    3841  _password      = password;
     
    4144  _iRow          = iRow;
    4245  _rate          = rate;
     46
    4347  if      (_rate < 0) {
    4448    _rate = 0;
     
    4852  }
    4953  _isToBeDeleted = false;
     54
     55  if (!QSslSocket::supportsSsl()) {
     56    BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
     57    deleteSafely();
     58  }
     59
     60  if (_ntripVersion != "1") {
     61    bncSettings settings;
     62    _proxyHost = settings.value("proxyHost").toString();
     63    _proxyPort = settings.value("proxyPort").toInt();
     64    _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
     65    if (_secure) {
     66      _casterOutPort = 443;
     67      _postExtension = QString("https://%1:%2").arg(_casterOutHost).arg(_casterOutPort);
     68    }
     69    else {
     70      _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort);
     71    }
     72  }
    5073
    5174  connect(this, SIGNAL(newMessage(QByteArray,bool)),
     
    80103  }
    81104  if (_outSocket) {
     105    disconnect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
     106               this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
    82107    delete _outSocket;
    83108  }
    84109}
     110
     111//
     112////////////////////////////////////////////////////////////////////////////
     113void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&,
     114                                                    QAuthenticator*) {
     115  emit newMessage("slotProxyAuthenticationRequired", true);
     116}
     117
     118
     119/* TSL/SSL
     120////////////////////////////////////////////////////////////////////////////
     121void bncUploadCaster::slotSslErrors(QList<QSslError> errors) {
     122
     123  QString msg = "SSL Error\n";
     124  QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate();
     125  if (!cert.isNull()) {
     126    msg += QString("Server Certificate Issued by:\n"
     127                   "%1\n%2\nCannot be verified\n")
     128#if QT_VERSION >= 0x050000
     129           .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
     130           .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
     131#else
     132           .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
     133           .arg(cert.issuerInfo(QSslCertificate::Organization));
     134#endif
     135  }
     136  QListIterator<QSslError> it(errors);
     137  while (it.hasNext()) {
     138    const QSslError& err = it.next();
     139    msg += "\n" + err.errorString();
     140  }
     141
     142  BNC_CORE->slotMessage(msg.toLatin1(), true);
     143
     144  if (_sslIgnoreErrors) {
     145    _outSocket->ignoreSslErrors();
     146  }
     147  else {
     148    deleteSafely();
     149  }
     150}
     151*/
    85152
    86153// Endless Loop
     
    97164      QMutexLocker locker(&_mutex);
    98165      if (_outBuffer.size() > 0) {
    99         _outSocket->write(_outBuffer);
    100         _outSocket->flush();
     166        if (_ntripVersion == "1") {
     167          _outSocket->write(_outBuffer);
     168          _outSocket->flush();
     169        }
     170        else {
     171          QString chunkSize = QString("%1").arg(_outBuffer.size(),0,16,QLatin1Char('0'));
     172          QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n";
     173          _outSocket->write(chunkedData);
     174          _outSocket->flush();
     175        }
    101176        emit newBytes(_mountpoint.toLatin1(), _outBuffer.size());
    102177      }
     
    127202    return;
    128203  }
    129 
     204 
    130205  delete _outSocket; _outSocket = 0;
    131206
     
    142217  }
    143218
    144   _outSocket = new QTcpSocket();
    145   _outSocket->setProxy(QNetworkProxy::NoProxy); // Ntrip1: to prevent the usage of system entries
    146   _outSocket->connectToHost(_outHost, _outPort);
     219  _outSocket = new QSslSocket();
     220  if (_sslIgnoreErrors) {
     221    _outSocket->ignoreSslErrors();
     222  }
     223  _outSocket->setProxy(QNetworkProxy::NoProxy); // to prevent the usage of system entries
     224  if (_ntripVersion == "1") {
     225    _outHost = _casterOutHost;
     226    _outPort = _casterOutPort;
     227  }
     228  else {
     229    if (!_proxyHost.isEmpty()) {
     230      _outHost = _proxyHost;
     231      _outPort = _proxyPort;
     232      connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
     233              this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
     234    }
     235  }
     236
     237  if (!_secure) {
     238    _outSocket->connectToHost(_outHost, _outPort);
     239  }
     240  else {
     241    _outSocket->connectToHostEncrypted(_outHost, _outPort);/*
     242    if (!_outSocket->waitForEncrypted()) {
     243        cout << "waitForEncrypted: " << _outSocket->errorString().toStdString().c_str() << endl;;
     244        return;
     245    }*/
     246  }
    147247
    148248  const int timeOut = 5000;  // 5 seconds
    149249  if (!_outSocket->waitForConnected(timeOut)) {
    150250    emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1()
    151          + "(" + _outHost.toLatin1() + "), " + _outSocket->errorString().toLatin1(), true));
     251         + " (" + _outHost.toLatin1()+ ":" + QString("%1) ").arg(_outPort).toLatin1()
     252         + _outSocket->errorString().toLatin1(), true));
    152253    delete _outSocket;
    153254    _outSocket = 0;
     
    155256  }
    156257
    157   QByteArray msg = "SOURCE " + _password.toLatin1() + " /" +
    158                    _mountpoint.toLatin1() + "\r\n" +
    159                    "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
    160 
     258  QByteArray msg;
     259  if (_ntripVersion == "1") {
     260    msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1() + "\r\n" +
     261          "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
     262  }
     263  else {
     264    msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1() + " HTTP/1.1\r\n" +
     265          "Host: " + _casterOutHost.toLatin1() + "\r\n" +
     266          "Ntrip-Version: Ntrip/2.0\r\n" +
     267          "Authorization: Basic " + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n" +
     268          "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n" +
     269          "Connection: close\r\n" +
     270          "Transfer-Encoding: chunked\r\n\r\n";
     271  }
     272  cout << msg.toStdString().c_str();
    161273  _outSocket->write(msg);
    162274  _outSocket->waitForBytesWritten();
    163275
    164276  _outSocket->waitForReadyRead();
    165   QByteArray ans = _outSocket->readLine();
     277  QByteArray ans = _outSocket->readLine();cout << ans.toStdString().c_str() << endl;
    166278
    167279  if (ans.indexOf("OK") == -1) {
    168280    delete _outSocket;
    169281    _outSocket = 0;
    170     emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1(), true));
     282    emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": " + ans.left(ans.length()-2), true));
    171283  }
    172284  else {
  • trunk/BNC/src/upload/bncuploadcaster.h

    r8770 r9707  
    44#include <QDateTime>
    55#include <QMutex>
    6 #include <QTcpSocket>
     6#include <QSslSocket>
    77#include <QNetworkProxy>
    88#include <QThread>
     9#include <QSslError>
     10#include <iostream>
     11
    912
    1013class bncUploadCaster : public QThread {
     
    3134  void newBytes(QByteArray staID, double nbyte);
    3235
     36 private slots:
     37  void slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*);
     38  //void slotSslErrors(QList<QSslError>);
     39
    3340 private:
    3441  void         open();
     
    3845  QString     _outHost;
    3946  int         _outPort;
     47  QString     _casterOutHost;
     48  int         _casterOutPort;
     49  QString     _proxyHost;
     50  int         _proxyPort;
    4051  QString     _userName;
    4152  QString     _password;
    4253  QString     _ntripVersion;
     54  QString     _postExtension;
    4355  bool        _secure;
    44   QTcpSocket* _outSocket;
     56  bool        _sslIgnoreErrors;
     57  QSslSocket* _outSocket;
    4558  int         _sOpenTrial;
    4659  QDateTime   _outSocketOpenTime;
Note: See TracChangeset for help on using the changeset viewer.