You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ptserver.cpp 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "ptserver.h"
  2. PTServer::PTServer(QObject *parent) : QTcpServer(parent)
  3. {
  4. connect(this, SIGNAL(newConnection()), this, SLOT(m_newConnection()));
  5. }
  6. PTSocket *PTServer::nextPendingConnection()
  7. {
  8. PTSocket* sock = 0;
  9. if(!m_incomings.isEmpty())
  10. sock = m_incomings.dequeue();
  11. return sock;
  12. }
  13. QList<PTSocket*> PTServer::getClients() const
  14. {
  15. return m_clients;
  16. }
  17. void PTServer::incomingConnection(int handle)
  18. {
  19. PTSocket* sock = new PTSocket(this);
  20. sock->setIsServerSide(true);
  21. sock->setSocketDescriptor(handle);
  22. m_incomings.enqueue(sock);
  23. }
  24. void PTServer::m_newConnection()
  25. {
  26. PTSocket* client = nextPendingConnection();
  27. connect(client, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State)));
  28. m_tempClients.append(client);
  29. }
  30. void PTServer::m_clientStateChanged(PTSocket::State s)
  31. {
  32. PTSocket* client = qobject_cast<PTSocket*>(sender());
  33. if(client == 0)
  34. return;
  35. if(s == PTSocket::Handshaked)
  36. {
  37. m_clients.append(client);
  38. m_tempClients.removeOne(client);
  39. emit newClient(client);
  40. }
  41. else if(s == PTSocket::Disconnected || s == PTSocket::Error)
  42. {
  43. m_tempClients.removeOne(client);
  44. m_clients.removeOne(client);
  45. client->deleteLater();
  46. }
  47. }