VPTissue Reference Manual
Connection.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "Connection.h"
21 
22 #include <QThread>
23 #include <QHostAddress>
24 #include <QDataStream>
25 #include <cassert>
26 
27 namespace SimPT_Parex {
28 
29 #define QDATASTREAM_VERSION QDataStream::Qt_4_7 // Ensure compatibility of data conversions
30 
31 Connection::Connection(QTcpSocket *socket, QObject *parent)
32  : QObject(parent), m_socket(socket), m_size(0)
33 {
34  assert(m_socket);
35  assert(m_socket->state() == QAbstractSocket::ConnectedState);
36 
37  m_socket->setParent(nullptr);
38 
39  connect(m_socket, SIGNAL(readyRead()), this, SLOT(ReadMessage()));
40  connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(HandleError(QAbstractSocket::SocketError)));
41 }
42 
44 {
45  if (IsConnected())
46  m_socket->deleteLater();
47 }
48 
50 {
51  return (m_socket != nullptr &&
52  m_socket->state() == QAbstractSocket::ConnectedState);
53 }
54 
55 std::string Connection::GetPeerAddress() const {
56  return m_socket->peerAddress().toString().toStdString();
57 }
58 
59 int Connection::GetPeerPort() const {
60  return m_socket->peerPort();
61 }
62 
63 void Connection::SendMessage(const QByteArray &message)
64 {
65  assert(m_socket->state() == QAbstractSocket::ConnectedState);
66 
67  QDataStream dataStream(m_socket);
68  dataStream.setVersion(QDATASTREAM_VERSION);
69 
70  dataStream << static_cast<quint32>(message.size()); // Size of the message, to send individual messages over a TCP stream
71  m_socket->write(message);
72 }
73 
74 void Connection::ReadMessage()
75 {
76  // Tries to read messages every time a new segment of the TCP stream arrives
77  // First reads the size (if enough bytes available on the stream)
78  // Then reads the number of bytes indicated by that stream, if available
79  // The size is stored between calls in m_size until the whole message arrives
80 
81  while (m_socket->bytesAvailable() >= qint64(sizeof(quint32))) {
82  while (m_size == 0) {
83  if (m_socket->bytesAvailable() < qint64(sizeof(quint32)))
84  return;
85  QDataStream dataStream(m_socket);
86  dataStream.setVersion(QDATASTREAM_VERSION);
87  dataStream >> m_size;
88  }
89 
90  if (m_socket->bytesAvailable() < m_size)
91  return;
92 
93  QByteArray message = m_socket->read(m_size);
94  m_size = 0;
95 
96  emit ReceivedMessage(message);
97  }
98 }
99 
100 void Connection::HandleError(QAbstractSocket::SocketError socketError)
101 {
102  if (socketError == QAbstractSocket::RemoteHostClosedError) {
103  emit ConnectionClosed();
104  } else {
105  emit Error(m_socket->errorString().toStdString());
106  m_socket->abort();
107  }
108 }
109 
110 } // namespace
Interface for Connection.
void SendMessage(const QByteArray &message)
Sends a message over the connection.
Definition: Connection.cpp:63
bool IsConnected()
Check if still connected.
Definition: Connection.cpp:49
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.
Definition: Connection.cpp:31
void ConnectionClosed()
Signal emitted when the remote host closed the connection.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
virtual ~Connection()
Destructor, closes the connection.
Definition: Connection.cpp:43
see the online Qt documentation