-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #31: replace raw pointers by smart pointers
- Loading branch information
Showing
3 changed files
with
30 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,42 @@ | ||
#include "chatserver.h" | ||
|
||
static QString getIdentifier(QWebSocket* socket) | ||
{ | ||
return QStringLiteral("%1:%2").arg(socket->peerAddress().toString(), | ||
QString::number(socket->peerPort())); | ||
static QString getIdentifier(QWebSocket* socket) { | ||
return QStringLiteral("%1:%2").arg(socket->peerAddress().toString(), QString::number(socket->peerPort())); | ||
} | ||
|
||
ChatServer::ChatServer(quint16 port, QObject *parent) | ||
ChatServer::ChatServer(quint16 port, QObject* parent) | ||
: QObject{parent}, | ||
m_webSocketServer{new QWebSocketServer("Chat Server", | ||
QWebSocketServer::NonSecureMode, | ||
this)} | ||
{ | ||
if(m_webSocketServer->listen(QHostAddress::Any, port)) | ||
{ | ||
m_webSocketServer{std::make_unique<QWebSocketServer>("Chat Server", QWebSocketServer::NonSecureMode, this)} { | ||
if (m_webSocketServer->listen(QHostAddress::Any, port)) { | ||
qInfo() << "Server listening on port " << port; | ||
connect(m_webSocketServer, &QWebSocketServer::newConnection, this, &ChatServer::onNewConnection); | ||
connect(&*m_webSocketServer, &QWebSocketServer::newConnection, this, &ChatServer::onNewConnection); | ||
} | ||
} | ||
|
||
ChatServer::~ChatServer(){ | ||
m_webSocketServer->close(); | ||
} | ||
|
||
void ChatServer::onNewConnection() | ||
{ | ||
auto clientSocket = m_webSocketServer->nextPendingConnection(); | ||
qInfo() << getIdentifier(clientSocket) << " connected!"; | ||
clientSocket->setParent(this); | ||
ChatServer::~ChatServer() { m_webSocketServer->close(); } | ||
|
||
connect(clientSocket, &QWebSocket::textMessageReceived, this, &ChatServer::processMessage); | ||
connect(clientSocket, &QWebSocket::disconnected, this, &ChatServer::socketDisconnected); | ||
void ChatServer::onNewConnection() { | ||
auto clientSocket = std::make_unique<QWebSocket*>(m_webSocketServer->nextPendingConnection()); | ||
qInfo() << getIdentifier(*clientSocket) << " connected!"; | ||
|
||
m_clients << clientSocket; | ||
connect(*clientSocket, &QWebSocket::textMessageReceived, this, &ChatServer::processMessage); | ||
connect(*clientSocket, &QWebSocket::disconnected, this, &ChatServer::socketDisconnected); | ||
m_clients.push_back(std::move(clientSocket)); | ||
} | ||
|
||
void ChatServer::processMessage(const QString& message) | ||
{ | ||
QWebSocket* clientSender = qobject_cast<QWebSocket *>(sender()); | ||
for (QWebSocket* client : std::as_const(m_clients)) { | ||
if (client != clientSender) | ||
client->sendTextMessage(message); | ||
void ChatServer::processMessage(const QString& message) { | ||
const auto& clientSender = std::make_unique<QWebSocket*>(qobject_cast<QWebSocket*>(sender())); | ||
for (const auto& client : std::as_const(m_clients)) { | ||
if (client != clientSender) { | ||
(*client)->sendTextMessage(message); | ||
} | ||
} | ||
} | ||
|
||
void ChatServer::socketDisconnected() | ||
{ | ||
QWebSocket* client = qobject_cast<QWebSocket*>(sender()); | ||
qInfo() << getIdentifier(client) << " disconnected!"; | ||
if(client) | ||
{ | ||
m_clients.removeAll(client); | ||
client->deleteLater(); | ||
void ChatServer::socketDisconnected() { | ||
auto client = std::make_unique<QWebSocket*>(qobject_cast<QWebSocket*>(sender())); | ||
qInfo() << getIdentifier(*client) << " disconnected!"; | ||
if (client) { | ||
m_clients.erase(std::remove(std::begin(m_clients), std::end(m_clients), client), std::end(m_clients)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include <QCoreApplication> | ||
|
||
#include "chatserver.h" | ||
|
||
int main(int argc, char *argv[]) { | ||
|