39 #include <QNetworkInterface>
68 connect(mSocket, &QTcpSocket::connected,
this, &SocketClientConnector::internalConnected);
69 connect(mSocket, &QTcpSocket::disconnected,
this, &SocketClientConnector::internalDisconnected);
81 mSocket->connectToHost(mInfo.
host, mInfo.
port);
92 if (mSocket->state() == QAbstractSocket::ConnectedState)
94 if (mSocket->state() == QAbstractSocket::UnconnectedState)
99 void SocketClientConnector::internalConnected()
105 void SocketClientConnector::internalDisconnected()
140 if (mSocket->state() == QAbstractSocket::ConnectedState)
142 if (mServer->isListening())
144 if (mSocket->state() == QAbstractSocket::UnconnectedState)
149 bool SocketServerConnector::startListen()
159 bool started = mServer->listen(QHostAddress::Any, mInfo.
port);
163 CX_LOG_INFO() << QString(
"Server address: %1").arg(this->getAllServerHostnames().join(
", "));
164 CX_LOG_INFO() << QString(
"Server is listening to port %1").arg(mServer->serverPort());
168 CX_LOG_INFO() << QString(
"Server failed to start. Error: %1").arg(mServer->errorString());
175 void SocketServerConnector::stopListen()
179 if (mServer && mServer->isListening())
181 CX_LOG_INFO() << QString(
"Server stopped listening to port %1").arg(mServer->serverPort());
187 void SocketServerConnector::incomingConnection(qintptr socketDescriptor)
191 if (this->mSocket->state() == QAbstractSocket::ConnectedState)
193 reportError(
"Incoming connection request rejected: The server can only handle a single connection.");
197 int success = mSocket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState);
198 QString clientName = mSocket->localAddress().toString();
204 QStringList SocketServerConnector::getAllServerHostnames()
206 QStringList addresses;
208 foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
210 if (interface.flags().testFlag(QNetworkInterface::IsRunning))
211 foreach (QNetworkAddressEntry entry, interface.addressEntries())
213 if ( interface.hardwareAddress() !=
"00:00:00:00:00:00"
214 && entry.ip().toString() !=
"127.0.0.1"
215 && entry.ip().toString().contains(
".") )
216 addresses << QString(
"%1: %2").arg(interface.name()).arg(entry.ip().toString());
QString qstring_cast(const T &val)
void incoming(qintptr socketDescriptor)
CX_SOCKETCONNECTION_STATE
virtual ~SocketClientConnector()
void reportError(QString msg)
SingleConnectionTcpServer(QObject *parent)
SocketServerConnector(SocketConnection::ConnectionInfo info, QTcpSocket *socket)
QString getDescription() const
virtual ~SocketServerConnector()
void incomingConnection(qintptr socketDescriptor)
virtual CX_SOCKETCONNECTION_STATE getState()
void stateChanged(CX_SOCKETCONNECTION_STATE)
SocketClientConnector(SocketConnection::ConnectionInfo info, QTcpSocket *socket)
virtual CX_SOCKETCONNECTION_STATE getState()
virtual void deactivate()
virtual void deactivate()