Changeset 1785 in ntrip for trunk/BNC/bncnetqueryudp0.cpp


Ignore:
Timestamp:
Apr 8, 2009, 2:51:36 PM (15 years ago)
Author:
mervart
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/bncnetqueryudp0.cpp

    r1784 r1785  
    1515 * -----------------------------------------------------------------------*/
    1616
     17#include <iostream>
     18
    1719#include "bncnetqueryudp0.h"
    1820
     
    2325bncNetQueryUdp0::bncNetQueryUdp0() {
    2426  _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  }
    2534}
    2635
     
    2837////////////////////////////////////////////////////////////////////////////
    2938bncNetQueryUdp0::~bncNetQueryUdp0() {
     39  delete _eventLoop;
    3040  delete _udpSocket;
    3141}
     
    3444////////////////////////////////////////////////////////////////////////////
    3545void bncNetQueryUdp0::stop() {
    36 #ifndef sparc
    37   if (_udpSocket) {
    38     _udpSocket->abort();
    39   }
    40 #endif
     46  _eventLoop->quit();
    4147  _status = finished;
    4248}
     
    5056////////////////////////////////////////////////////////////////////////////
    5157void bncNetQueryUdp0::waitForReadyRead(QByteArray& outData) {
    52   if (_udpSocket) {
    53     while (true) {
    54       int nBytes = _udpSocket->bytesAvailable();
    55       if (nBytes > 0) {
    56         outData = _udpSocket->readAll();
    57         return;
    58       }
    59       else if (!_udpSocket->waitForReadyRead(_timeOut)) {
    60         delete _udpSocket;
    61         _udpSocket = 0;
    62         _status = error;
    63         emit newMessage(_url.path().toAscii() + " read timeout", true);
    64         return;
    65       }
    66     }
     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  if (datagram.size() > 0) {
     72    outData.append(datagram);
    6773  }
    6874}
     
    8490  }
    8591
    86   delete _udpSocket;
    87   _udpSocket = new QUdpSocket();
    88   _udpSocket->connectToHost(_url.host(), _url.port());
     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  }
    89103}
    90104
Note: See TracChangeset for help on using the changeset viewer.