Added acknowledge to CameraTcpServer (a client must wait a response before sending images)
git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@378 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
parent
072f6cc8f5
commit
da4ee8637b
@ -17,14 +17,11 @@ CameraTcpServer::CameraTcpServer(quint16 port, QObject *parent) :
|
||||
QTcpServer(parent),
|
||||
blockSize_(0)
|
||||
{
|
||||
this->setMaxPendingConnections(1);
|
||||
if (!this->listen(QHostAddress::Any, port))
|
||||
{
|
||||
UERROR("Unable to start the Camera TCP server: %s", this->errorString().toStdString().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
connect(this, SIGNAL(newConnection()), this, SLOT(addClient()));
|
||||
}
|
||||
|
||||
cv::Mat CameraTcpServer::getImage()
|
||||
@ -83,22 +80,23 @@ quint16 CameraTcpServer::getPort() const
|
||||
return this->serverPort();
|
||||
}
|
||||
|
||||
void CameraTcpServer::addClient()
|
||||
void CameraTcpServer::incomingConnection(int socketDescriptor)
|
||||
{
|
||||
QTcpSocket * client = this->nextPendingConnection();
|
||||
|
||||
QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
|
||||
if(clients.size() > 1)
|
||||
if(clients.size() >= 1)
|
||||
{
|
||||
UWARN("A client is already connected. Only one connection allowed at the same time.");
|
||||
client->close();
|
||||
client->deleteLater();
|
||||
QTcpSocket socket;
|
||||
socket.setSocketDescriptor(socketDescriptor);
|
||||
}
|
||||
else
|
||||
{
|
||||
connect(client, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
|
||||
connect(client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
|
||||
connect(client, SIGNAL(disconnected()), this, SLOT(connectionLost()));
|
||||
QTcpSocket * socket = new QTcpSocket(this);
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
|
||||
connect(socket, SIGNAL(disconnected()), this, SLOT(connectionLost()));
|
||||
socket->setSocketDescriptor(socketDescriptor);
|
||||
socket->write(QByteArray("1"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,14 +23,14 @@ public:
|
||||
QHostAddress getHostAddress() const;
|
||||
quint16 getPort() const;
|
||||
|
||||
protected:
|
||||
virtual void incomingConnection ( int socketDescriptor );
|
||||
|
||||
private Q_SLOTS:
|
||||
void readReceivedData();
|
||||
void displayError(QAbstractSocket::SocketError socketError);
|
||||
void connectionLost();
|
||||
|
||||
private Q_SLOTS:
|
||||
void addClient();
|
||||
|
||||
private:
|
||||
quint64 blockSize_;
|
||||
QVector<cv::Mat> images_;
|
||||
|
||||
@ -99,7 +99,7 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
server.connectToHost(ipAddress, port);
|
||||
|
||||
if(!server.waitForConnected())
|
||||
if(!server.waitForReadyRead())
|
||||
{
|
||||
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), port);
|
||||
return -1;
|
||||
|
||||
@ -148,6 +148,9 @@ int main(int argc, char * argv[])
|
||||
|
||||
QObject::connect(&response, SIGNAL(detectionReceived()), &app, SLOT(quit()));
|
||||
QObject::connect(&response, SIGNAL(disconnected()), &app, SLOT(quit()));
|
||||
QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
|
||||
QObject::connect(&response, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
|
||||
QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
|
||||
|
||||
request.connectToHost(ipAddress, portOut);
|
||||
response.connectToHost(ipAddress, portIn);
|
||||
@ -164,7 +167,6 @@ int main(int argc, char * argv[])
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// publish image
|
||||
std::vector<unsigned char> buf;
|
||||
cv::imencode(".png", image, buf);
|
||||
@ -176,8 +178,11 @@ int main(int argc, char * argv[])
|
||||
out.writeRawData((char*)buf.data(), (int)buf.size());
|
||||
out.device()->seek(0);
|
||||
out << (quint64)(block.size() - sizeof(quint64));
|
||||
request.write(block);
|
||||
printf("Image published, waiting for response...\n");
|
||||
|
||||
if(request.waitForReadyRead())
|
||||
{
|
||||
qint64 bytes = request.write(block);
|
||||
printf("Image published (%d bytes), waiting for response...\n", (int)bytes);
|
||||
QTime time;
|
||||
time.start();
|
||||
|
||||
@ -218,6 +223,13 @@ int main(int argc, char * argv[])
|
||||
else
|
||||
{
|
||||
printf("Failed to receive a response...\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Server is busy...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user