Skip to content

Commit

Permalink
Fixes #31: replace raw pointers by smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicx95 committed Apr 18, 2024
1 parent 18d9f21 commit 0df0281
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 44 deletions.
62 changes: 24 additions & 38 deletions server/chatserver.cpp
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));
}
}
11 changes: 5 additions & 6 deletions server/chatserver.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once

#include <QDebug>
#include <QObject>
#include <QWebSocketServer>
#include <QtWebSockets>
#include <QDebug>

class ChatServer : public QObject
{
class ChatServer : public QObject {
Q_OBJECT
public:
explicit ChatServer(quint16 port, QObject *parent = nullptr);
explicit ChatServer(quint16 port, QObject* parent = nullptr);
~ChatServer() override;

private slots:
Expand All @@ -18,6 +17,6 @@ private slots:
void socketDisconnected();

private:
QWebSocketServer* m_webSocketServer;
QList<QWebSocket*> m_clients;
std::unique_ptr<QWebSocketServer> m_webSocketServer;
std::vector<std::unique_ptr<QWebSocket*>> m_clients;
};
1 change: 1 addition & 0 deletions server/main.cpp
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[]) {
Expand Down

0 comments on commit 0df0281

Please sign in to comment.