Implemented basic listening functionality

This commit is contained in:
0xFEEDC0DE64
2018-09-22 02:11:37 +02:00
parent a62aa7ec5a
commit 00f31676dc
6 changed files with 152 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
#include "client.h"
#include <QTcpSocket>
#include <QDebug>
#include "server.h"
Client::Client(QTcpSocket *socket, Server *server) :
QObject(server), m_socket(socket)
{
qDebug() << this << "connected";
m_socket->setParent(this);
connect(m_socket, &QIODevice::readyRead, this, &Client::readyRead);
connect(m_socket, &QAbstractSocket::disconnected, this, &QObject::deleteLater);
}
Client::~Client()
{
qDebug() << this << "disconnected";
}
qint64 Client::write(const char *data, qint64 len)
{
return m_socket->write(data, len);
}
qint64 Client::write(const char *data)
{
return m_socket->write(data);
}
qint64 Client::write(const QByteArray &data)
{
return m_socket->write(data);
}
void Client::readyRead()
{
qDebug() << this << "readyRead" << m_socket->readAll();
}

26
messagingserver/client.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <QObject>
class QTcpSocket;
class Server;
class Client : public QObject
{
Q_OBJECT
public:
explicit Client(QTcpSocket *socket, Server *server);
~Client();
qint64 write(const char *data, qint64 len);
qint64 write(const char *data);
qint64 write(const QByteArray &data);
private Q_SLOTS:
void readyRead();
private:
QTcpSocket *m_socket;
};

View File

@@ -1,11 +1,12 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug>
#include "server.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
qDebug() << "hello from server"; Server server;
return 0; return app.exec();
} }

View File

@@ -7,9 +7,13 @@ TARGET = messagingserver
PROJECT_ROOT = ../.. PROJECT_ROOT = ../..
SOURCES += main.cpp SOURCES += main.cpp \
client.cpp \
server.cpp
HEADERS += HEADERS += \
client.h \
server.h
FORMS += FORMS +=

View File

@@ -0,0 +1,44 @@
#include "server.h"
#include <QTcpServer>
#include <QDateTime>
#include <QTimerEvent>
#include "client.h"
Server::Server(QObject *parent) :
QObject(parent),
m_server(new QTcpServer(this))
{
if(!m_server->listen(QHostAddress::Any, 1234))
qFatal("could not start listening %s", m_server->errorString());
connect(m_server, &QTcpServer::newConnection, this, &Server::newConnection);
m_timerId = startTimer(1000);
}
void Server::timerEvent(QTimerEvent *event)
{
if(event->timerId() == m_timerId)
Q_EMIT distribute(QDateTime::currentDateTime().toString("dd.MM.yyyy HH:mm:ss\r\n").toUtf8());
else
QObject::timerEvent(event);
}
void Server::newConnection()
{
auto socket = m_server->nextPendingConnection();
if(!socket)
{
qWarning() << "null socket received";
return;
}
auto client = new Client(socket, this);
connect(this, &Server::distribute, client, qOverload<const QByteArray &>(&Client::write));
m_clients.insert(client);
connect(client, &QObject::destroyed, this, [this, client](){
m_clients.remove(client);
});
}

30
messagingserver/server.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <QObject>
#include <QSet>
class QTcpServer;
class Client;
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = nullptr);
Q_SIGNALS:
void distribute(const QByteArray &data);
protected:
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
private Q_SLOTS:
void newConnection();
private:
QTcpServer *m_server;
QSet<Client*> m_clients;
int m_timerId;
};