hyperion.ng/libsrc/protoserver/ProtoServer.cpp
johan 9247c577ba Proto server added for better performance of remote application using a binary protocol;
gpio2spi added which can be used to switch the GPIO pins into SPI mode if necessary;


Former-commit-id: 237f495289ce2f4afae49b36684f464937dbd30f
2013-10-11 10:06:24 +02:00

58 lines
1.4 KiB
C++

// system includes
#include <stdexcept>
// project includes
#include <protoserver/ProtoServer.h>
#include "ProtoClientConnection.h"
ProtoServer::ProtoServer(Hyperion *hyperion, uint16_t port) :
QObject(),
_hyperion(hyperion),
_server(),
_openConnections()
{
if (!_server.listen(QHostAddress::Any, port))
{
throw std::runtime_error("Proto server could not bind to port");
}
// Set trigger for incoming connections
connect(&_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
ProtoServer::~ProtoServer()
{
foreach (ProtoClientConnection * connection, _openConnections) {
delete connection;
}
}
uint16_t ProtoServer::getPort() const
{
return _server.serverPort();
}
void ProtoServer::newConnection()
{
QTcpSocket * socket = _server.nextPendingConnection();
if (socket != nullptr)
{
std::cout << "New proto connection" << std::endl;
ProtoClientConnection * connection = new ProtoClientConnection(socket, _hyperion);
_openConnections.insert(connection);
// register slot for cleaning up after the connection closed
connect(connection, SIGNAL(connectionClosed(ProtoClientConnection*)), this, SLOT(closedConnection(ProtoClientConnection*)));
}
}
void ProtoServer::closedConnection(ProtoClientConnection *connection)
{
std::cout << "Proto connection closed" << std::endl;
_openConnections.remove(connection);
// schedule to delete the connection object
connection->deleteLater();
}