server accepts connections
[buliscores] / buliscores-server / buliscoressrv.cpp
index 05e26d6..2b36d84 100644 (file)
  */
 
 #include "buliscoressrv.h"
+#include <QtCore/QCoreApplication>
 
 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();
+}
+