36 #include <QNetworkInterface>
59 return (this->role == rhs.
role)
61 && (this->host == rhs.
host)
62 && (this->port == rhs.
port);
65 bool SocketConnection::ConnectionInfo::isServer()
const {
return role.toLower()==
"server"; }
66 bool SocketConnection::ConnectionInfo::isClient()
const {
return !this->isServer(); }
67 bool SocketConnection::ConnectionInfo::isLocalhostConnection()
const {
return host.toLower().contains(
"localhost"); }
69 QString SocketConnection::ConnectionInfo::getDescription()
const
75 if (!protocol.isEmpty())
76 postfix = QString(
"[%1]").arg(protocol);
77 return QString(
"%1:%2%3").arg(name).arg(port).arg(postfix);
85 SocketConnection::SocketConnection(QObject *parent) :
89 qRegisterMetaType<boost::function<void()> >(
"boost::function<void()>");
90 qRegisterMetaType<CX_SOCKETCONNECTION_STATE>(
"CX_SOCKETCONNECTION_STATE");
97 connect(
mSocket, &QTcpSocket::connected,
this, &SocketConnection::internalConnected);
98 connect(
mSocket, &QTcpSocket::disconnected,
this, &SocketConnection::internalDisconnected);
99 connect(
mSocket, &QTcpSocket::readyRead,
this, &SocketConnection::internalDataAvailable);
102 void (QTcpSocket::* errorOverloaded)(QAbstractSocket::SocketError) = &QTcpSocket::error;
104 connect(
mSocket, errorOverloaded,
this, &SocketConnection::internalError);
183 qint64 writtenBytes =
mSocket->write(data, maxSize);
184 if(writtenBytes != maxSize)
189 void SocketConnection::internalConnected()
195 void SocketConnection::internalDisconnected()
201 void SocketConnection::internalError(QAbstractSocket::SocketError socketError)
203 CX_LOG_ERROR() << QString(
"[%1] Socket error [code=%2]: %3")
205 .arg(QString::number(socketError))
213 return mSocket->state() == QAbstractSocket::ConnectedState;
218 bool enoughBytes =
mSocket->bytesAvailable() >= bytes;
220 CX_LOG_DEBUG() <<
"Want " << bytes <<
" but only "<<
mSocket->bytesAvailable() <<
" are available on the socket atm.";
229 char* charPointer =
reinterpret_cast<char*
>(packPointer);
230 int r =
mSocket->read(charPointer, packSize);
233 CX_LOG_ERROR() <<
"Error when receiving data from socket.";
virtual void setConnectionInfo(ConnectionInfo info)
thread-safe
CX_SOCKETCONNECTION_STATE
bool sendData(const char *data, qint64 maxSize)
not thread-safe
SNW_DEFINE_ENUM_STRING_CONVERTERS_BEGIN(cx, CX_SOCKETCONNECTION_STATE, scsCOUNT)
bool socketReceive(void *packPointer, int packSize) const
bool enoughBytesAvailableOnSocket(int bytes) const
CX_SOCKETCONNECTION_STATE getState()
thread-safe
ConnectionInfo mNextConnectionInfo
info to be used for the next connect(), mutexed.
void stateChange(CX_SOCKETCONNECTION_STATE newState)
CX_SOCKETCONNECTION_STATE mCurrentState
void stateChanged(CX_SOCKETCONNECTION_STATE status)
virtual void requestDisconnect()
not thread-safe: use invoke
ConnectionInfo getConnectionInfo()
thread-safe
virtual void requestConnect()
not thread-safe: use invoke
SocketConnectorPtr createConnector(ConnectionInfo info)
QMutex mNextConnectionInfoMutex
virtual void setProtocol(QString protocolname)=0
void connectionInfoChanged()
boost::shared_ptr< class SocketConnector > SocketConnectorPtr
void stateChanged(CX_SOCKETCONNECTION_STATE)
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
#define assertRunningInObjectThread()
CX_SOCKETCONNECTION_STATE computeState()
SocketConnectorPtr mConnector
SNW_DEFINE_ENUM_STRING_CONVERTERS_END(cx, ORIENTATION_TYPE, otCOUNT)