23 #include <QHostAddress>
24 #include <QDataStream>
29 #define QDATASTREAM_VERSION QDataStream::Qt_4_7 // Ensure compatibility of data conversions
32 :
QObject(parent), m_socket(socket), m_size(0)
35 assert(m_socket->state() == QAbstractSocket::ConnectedState);
37 m_socket->setParent(
nullptr);
39 connect(m_socket, SIGNAL(readyRead()),
this, SLOT(ReadMessage()));
40 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(HandleError(QAbstractSocket::SocketError)));
46 m_socket->deleteLater();
51 return (m_socket !=
nullptr &&
52 m_socket->state() == QAbstractSocket::ConnectedState);
55 std::string Connection::GetPeerAddress()
const {
56 return m_socket->peerAddress().toString().toStdString();
59 int Connection::GetPeerPort()
const {
60 return m_socket->peerPort();
65 assert(m_socket->state() == QAbstractSocket::ConnectedState);
67 QDataStream dataStream(m_socket);
68 dataStream.setVersion(QDATASTREAM_VERSION);
70 dataStream << static_cast<quint32>(message.size());
71 m_socket->write(message);
74 void Connection::ReadMessage()
81 while (m_socket->bytesAvailable() >= qint64(
sizeof(quint32))) {
83 if (m_socket->bytesAvailable() < qint64(
sizeof(quint32)))
85 QDataStream dataStream(m_socket);
86 dataStream.setVersion(QDATASTREAM_VERSION);
90 if (m_socket->bytesAvailable() < m_size)
93 QByteArray message = m_socket->read(m_size);
100 void Connection::HandleError(QAbstractSocket::SocketError socketError)
102 if (socketError == QAbstractSocket::RemoteHostClosedError) {
105 emit
Error(m_socket->errorString().toStdString());
Interface for Connection.
void SendMessage(const QByteArray &message)
Sends a message over the connection.
bool IsConnected()
Check if still connected.
void ReceivedMessage(const QByteArray &message)
Signal emitted when a message is received over the connection.
void Error(const std::string &error)
Signal emitted when an error occurred in the connection.
Connection(QTcpSocket *socket, QObject *parent=0)
Constructor.
void ConnectionClosed()
Signal emitted when the remote host closed the connection.
Namespace for SimPT parameter explorer package.
virtual ~Connection()
Destructor, closes the connection.
see the online Qt documentation