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();
106 QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
107 if(clients.size() >= 1)
109 UWARN(
"A client is already connected. Only one connection allowed at the same time.");
111 socket.setSocketDescriptor(socketDescriptor);
116 QTcpSocket * socket =
new QTcpSocket(
this);
118 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(
displayError(QAbstractSocket::SocketError)));
119 connect(socket, SIGNAL(disconnected()),
this, SLOT(
connectionLost()));
120 socket->setSocketDescriptor(socketDescriptor);
121 socket->write(QByteArray(
"1"));
127 QTcpSocket * client = (QTcpSocket*)sender();
128 QDataStream
in(client);
129 in.setVersion(QDataStream::Qt_4_0);
133 if (client->bytesAvailable() < (int)
sizeof(quint64))
141 if (client->bytesAvailable() < (int)
blockSize_)
147 in.readRawData((
char*)buf.data(),
blockSize_);
148 images_.push_back(cv::imdecode(buf, cv::IMREAD_UNCHANGED));
149 int queue = Settings::getCamera_9queueSize();
150 while(queue > 0 &&
images_.size() > queue)
161 case QAbstractSocket::RemoteHostClosedError:
163 case QAbstractSocket::HostNotFoundError:
164 UWARN(
"CameraTcp: Tcp error: The host was not found. Please " 165 "check the host name and port settings.\n");
167 case QAbstractSocket::ConnectionRefusedError:
168 UWARN(
"CameraTcp: The connection was refused by the peer. " 169 "Make sure your images server is running, " 170 "and check that the host name and port " 171 "settings are correct.");
182 ((QTcpSocket*)sender())->close();
183 sender()->deleteLater();
QVector< cv::Mat > images_
virtual void incomingConnection(int socketDescriptor)
QHostAddress getHostAddress() const
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.