31 #include <QtNetwork/QTcpSocket> 32 #include <QtNetwork/QNetworkInterface> 33 #include <QtCore/QDataStream> 41 if (!this->listen(QHostAddress::Any, port))
43 UERROR(
"Unable to start the Camera TCP server: %s", this->errorString().toStdString().c_str());
54 int queue = Settings::getCamera_9queueSize();
55 while(queue > 0 &&
images_.size() > queue)
63 if(this->findChildren<QTcpSocket*>().size() == 1)
65 this->findChildren<QTcpSocket*>().first()->waitForReadyRead(100);
72 return this->findChildren<QTcpSocket*>().size() > 0;
77 QHostAddress hostAddress;
79 QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
81 for (
int i = 0; i < ipAddressesList.size(); ++i)
83 if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
85 hostAddress = ipAddressesList.at(i).toString();
91 if (hostAddress.isNull())
93 hostAddress = QHostAddress(QHostAddress::LocalHost);
101 return this->serverPort();
103 #if QT_VERSION >= 0x050000 109 QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
110 if(clients.size() >= 1)
112 UWARN(
"A client is already connected. Only one connection allowed at the same time.");
114 socket.setSocketDescriptor(socketDescriptor);
119 QTcpSocket * socket =
new QTcpSocket(
this);
121 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(
displayError(QAbstractSocket::SocketError)));
122 connect(socket, SIGNAL(disconnected()),
this, SLOT(
connectionLost()));
123 socket->setSocketDescriptor(socketDescriptor);
124 socket->write(QByteArray(
"1"));
130 QTcpSocket * client = (QTcpSocket*)sender();
131 QDataStream
in(client);
132 in.setVersion(QDataStream::Qt_4_0);
136 if (client->bytesAvailable() < (int)
sizeof(quint64))
144 if (client->bytesAvailable() < (int)
blockSize_)
150 in.readRawData((
char*)buf.data(),
blockSize_);
151 images_.push_back(cv::imdecode(buf, cv::IMREAD_UNCHANGED));
152 int queue = Settings::getCamera_9queueSize();
153 while(queue > 0 &&
images_.size() > queue)
164 case QAbstractSocket::RemoteHostClosedError:
166 case QAbstractSocket::HostNotFoundError:
167 UWARN(
"CameraTcp: Tcp error: The host was not found. Please " 168 "check the host name and port settings.\n");
170 case QAbstractSocket::ConnectionRefusedError:
171 UWARN(
"CameraTcp: The connection was refused by the peer. " 172 "Make sure your images server is running, " 173 "and check that the host name and port " 174 "settings are correct.");
185 ((QTcpSocket*)sender())->close();
186 sender()->deleteLater();
QHostAddress getHostAddress() const
QVector< cv::Mat > images_
virtual void incomingConnection(int socketDescriptor)
void displayError(QAbstractSocket::SocketError socketError)
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
CameraTcpServer(quint16 port=0, QObject *parent=0)
ULogger class and convenient macros.