30 #include <QtNetwork/QNetworkInterface> 31 #include <QtNetwork/QTcpSocket> 32 #include <QtGui/QTransform> 33 #include <opencv2/highgui/highgui.hpp> 40 if (!this->listen(QHostAddress::Any, port))
42 UERROR(
"Unable to start the TCP server: %s", this->errorString().toStdString().c_str());
46 connect(
this, SIGNAL(newConnection()),
this, SLOT(
addClient()));
51 QHostAddress hostAddress;
53 QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
55 for (
int i = 0; i < ipAddressesList.size(); ++i)
57 if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
59 hostAddress = ipAddressesList.at(i).toString();
65 if (hostAddress.isNull())
67 hostAddress = QHostAddress(QHostAddress::LocalHost);
75 return this->serverPort();
80 QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
83 UINFO(
"TCP server: Publish detected objects");
85 QDataStream out(&block, QIODevice::WriteOnly);
86 out.setVersion(QDataStream::Qt_4_0);
91 out.device()->seek(0);
92 out << (quint16)(block.size() -
sizeof(quint16));
94 for(QList<QTcpSocket*>::iterator iter = clients.begin(); iter!=clients.end(); ++iter)
96 (*iter)->write(block);
103 while(this->hasPendingConnections())
105 QTcpSocket * client = this->nextPendingConnection();
107 connect(client, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(
displayError(QAbstractSocket::SocketError)));
108 connect(client, SIGNAL(disconnected()),
this, SLOT(
connectionLost()));
114 QTcpSocket * client = (QTcpSocket*)sender();
115 QDataStream
in(client);
116 in.setVersion(QDataStream::Qt_4_0);
118 if (
blockSizes_.value(client->socketDescriptor(), 0) == 0)
120 if (client->bytesAvailable() < (int)
sizeof(quint64))
128 if (client->bytesAvailable() < (int)
blockSizes_[client->socketDescriptor()])
145 std::vector<unsigned char> buf(imageSize);
146 in.readRawData((
char*)buf.data(), imageSize);
147 cv::Mat image = cv::imdecode(buf, cv::IMREAD_UNCHANGED);
149 UINFO(
"TCP service: Add %d \"%s\"",
id, fileName.toStdString().c_str());
157 UINFO(
"TCP service: Remove %d",
id);
162 std::vector<unsigned char> buf(
blockSizes_[client->socketDescriptor()]);
163 in.readRawData((
char*)buf.data(),
blockSizes_[client->socketDescriptor()]-
sizeof(quint32));
164 cv::Mat image = cv::imdecode(buf, cv::IMREAD_UNCHANGED);
166 UINFO(
"TCP service: Detect object");
171 UERROR(
"Unknown service type called %d", serviceType);
176 client->write(QByteArray(ok?
"1":
"0"));
183 case QAbstractSocket::RemoteHostClosedError:
185 case QAbstractSocket::HostNotFoundError:
186 UWARN(
"CameraTcp: Tcp error: The host was not found. Please " 187 "check the host name and port settings.\n");
189 case QAbstractSocket::ConnectionRefusedError:
190 UWARN(
"CameraTcp: The connection was refused by the peer. " 191 "Make sure your images server is running, " 192 "and check that the host name and port " 193 "settings are correct.");
204 blockSizes_.remove(((QTcpSocket*)sender())->socketDescriptor());
205 ((QTcpSocket*)sender())->close();
206 sender()->deleteLater();
TcpServer(quint16 port=0, QObject *parent=0)
void displayError(QAbstractSocket::SocketError socketError)
QHostAddress getHostAddress() const
void detectObject(const cv::Mat &)
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
void addObject(const cv::Mat &, int, const QString &)
ULogger class and convenient macros.
QMap< int, quint64 > blockSizes_
void publishDetectionInfo(const find_object::DetectionInfo &info)