From dd2b5f6dad3d5a80d51b58d4c7e3238ee1fb8986 Mon Sep 17 00:00:00 2001 From: David Solbach Date: Sun, 14 Nov 2010 08:05:16 +0100 Subject: [PATCH] server accepts connections --- buliscores-server/buliscores-server.pro | 6 ++-- buliscores-server/buliscoressrv.cpp | 36 ++++++++++++++++++++++- buliscores-server/buliscoressrv.h | 11 +++++++- buliscores-server/clientconn.cpp | 46 ++++++++++++++++++++++++++++++ buliscores-server/clientconn.h | 47 +++++++++++++++++++++++++++++++ buliscores-server/main.cpp | 17 +++++++++-- 6 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 buliscores-server/clientconn.cpp create mode 100644 buliscores-server/clientconn.h diff --git a/buliscores-server/buliscores-server.pro b/buliscores-server/buliscores-server.pro index 611fd92..179909d 100644 --- a/buliscores-server/buliscores-server.pro +++ b/buliscores-server/buliscores-server.pro @@ -17,7 +17,9 @@ TEMPLATE = app SOURCES += main.cpp \ - buliscoressrv.cpp + buliscoressrv.cpp \ + clientconn.cpp HEADERS += \ - buliscoressrv.h + buliscoressrv.h \ + clientconn.h diff --git a/buliscores-server/buliscoressrv.cpp b/buliscores-server/buliscoressrv.cpp index 05e26d6..2b36d84 100644 --- a/buliscores-server/buliscoressrv.cpp +++ b/buliscores-server/buliscoressrv.cpp @@ -19,8 +19,42 @@ */ #include "buliscoressrv.h" +#include BuliScoresSrv::BuliScoresSrv(QObject *parent) : - QObject(parent) + QObject(parent), + m_Settings(qApp->organizationName(), qApp->applicationName()) { + int port; + + port = m_Settings.value("port", 1959).toInt(); + this->m_tcpServer.listen(QHostAddress::Any, port); + qDebug() << "Server started listening on port: " << port; + + connect(&m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); + } + +void BuliScoresSrv::onNewConnection() +{ + QTcpSocket* sock; + ClientConn* conn; + + + while ((sock = m_tcpServer.nextPendingConnection()) != NULL) { + conn = new ClientConn(sock, this); + + connect(conn, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); + m_Clients.append(conn); + qDebug() << "New Client: " << sock->peerAddress().toString() + << "\tNumber of clients: " << m_Clients.count(); + } +} + +void BuliScoresSrv::onClientDisconnected() +{ + m_Clients.removeOne((ClientConn*)QObject::sender()); + qDebug() << "Client disconnected, number of clients: " << m_Clients.count(); + delete QObject::sender(); +} + diff --git a/buliscores-server/buliscoressrv.h b/buliscores-server/buliscoressrv.h index 0f8aa82..4b06aa3 100644 --- a/buliscores-server/buliscoressrv.h +++ b/buliscores-server/buliscoressrv.h @@ -23,18 +23,27 @@ #include #include +#include + +#include "clientconn.h" class BuliScoresSrv : public QObject { Q_OBJECT private: - QTcpServer m_tcpServer; + QTcpServer m_tcpServer; + QSettings m_Settings; + QList m_Clients; public: explicit BuliScoresSrv(QObject *parent = 0); signals: +private slots: + void onNewConnection(); + void onClientDisconnected(); + public slots: }; diff --git a/buliscores-server/clientconn.cpp b/buliscores-server/clientconn.cpp new file mode 100644 index 0000000..ee9db62 --- /dev/null +++ b/buliscores-server/clientconn.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2010 David Solbach + * + * This file is part of BuliScores. + * + * BuliScores is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * BiliScores is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include + +#include "clientconn.h" + +ClientConn::ClientConn(QTcpSocket* socket, QObject *parent) : + QObject(parent) +{ + m_Socket = socket; + + connect(socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected())); +} + +void ClientConn::onSocketDisconnected() +{ + qDebug() << "Socket disconnected: " << m_Socket->peerAddress().toString(); + emit disconnected(); +} + +void ClientConn::triggerUpdate() +{ + if (m_Socket->isWritable()) { + m_Socket->write("u"); + } else { + qWarning() << "Update trigger msg not send, socket state: " << m_Socket->state(); + } +} diff --git a/buliscores-server/clientconn.h b/buliscores-server/clientconn.h new file mode 100644 index 0000000..3fa9318 --- /dev/null +++ b/buliscores-server/clientconn.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2010 David Solbach + * + * This file is part of BuliScores. + * + * BuliScores is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * BiliScores is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef CLIENTCONN_H +#define CLIENTCONN_H + +#include +#include + +class ClientConn : public QObject +{ + Q_OBJECT +private: + QTcpSocket* m_Socket; + +public: + explicit ClientConn(QTcpSocket* socket, QObject *parent = 0); + +signals: + void disconnected(); + +private slots: + void onSocketDisconnected(); + +public slots: + void triggerUpdate(); + +}; + +#endif // CLIENTCONN_H diff --git a/buliscores-server/main.cpp b/buliscores-server/main.cpp index 0ae31c6..366d3bc 100644 --- a/buliscores-server/main.cpp +++ b/buliscores-server/main.cpp @@ -3,11 +3,13 @@ #include #include +#include "buliscoressrv.h" + void messageHandler(QtMsgType type, const char *msg) { static QFile logfile; static QTextStream fw; - static const QString LOGFILE_PATH = "/tmp/buliscores.log"; + static const QString LOGFILE_PATH = "/tmp/buliscores-srv.log"; static const QtMsgType LOGLEVEL = QtDebugMsg; QString out; @@ -53,7 +55,16 @@ void messageHandler(QtMsgType type, const char *msg) int main(int argc, char *argv[]) { - QCoreApplication a(argc, argv); + QCoreApplication app(argc, argv); + app.setApplicationName("Buli Scores"); + app.setApplicationVersion("0.1"); + app.setOrganizationName("David Solbach"); + + BuliScoresSrv buliscoressrv; + + app.setApplicationName("Buli Scores Server"); + app.setApplicationVersion("0.1"); + app.setOrganizationName("David Solbach"); - return a.exec(); + return app.exec(); } -- 1.7.9.5