#include "ptserver.h" PTServer::PTServer(QObject *parent) : QTcpServer(parent) { connect(this, SIGNAL(newConnection()), this, SLOT(m_newConnection())); } PTSocket *PTServer::nextPendingConnection() { PTSocket* sock = 0; if(!m_incomings.isEmpty()) sock = m_incomings.dequeue(); return sock; } QList PTServer::getClients() const { return m_clients; } void PTServer::incomingConnection(int handle) { PTSocket* sock = new PTSocket(this); sock->setIsServerSide(true); sock->setSocketDescriptor(handle); m_incomings.enqueue(sock); } void PTServer::m_newConnection() { PTSocket* client = nextPendingConnection(); connect(client, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State))); m_tempClients.append(client); } void PTServer::m_clientStateChanged(PTSocket::State s) { PTSocket* client = qobject_cast(sender()); if(client == 0) return; if(s == PTSocket::Handshaked) { m_clients.append(client); m_tempClients.removeOne(client); emit newClient(client); } else if(s == PTSocket::Disconnected || s == PTSocket::Error) { m_tempClients.removeOne(client); m_clients.removeOne(client); client->deleteLater(); } }