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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. void PTServer::incomingConnection(int handle)
  14. {
  15. PTSocket* sock = new PTSocket(this);
  16. sock->setIsServerSide(true);
  17. sock->setSocketDescriptor(handle);
  18. m_incomings.enqueue(sock);
  19. }
  20. void PTServer::m_newConnection()
  21. {
  22. PTSocket* client = nextPendingConnection();
  23. connect(client, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State)));
  24. m_tempClients.append(client);
  25. }
  26. void PTServer::m_clientStateChanged(PTSocket::State s)
  27. {
  28. PTSocket* client = qobject_cast<PTSocket*>(sender());
  29. if(client == 0)
  30. return;
  31. if(s == PTSocket::Handshaked)
  32. {
  33. m_clients.append(client);
  34. m_tempClients.removeOne(client);
  35. emit newClient(client);
  36. }
  37. else if(s == PTSocket::Disconnected || s == PTSocket::Error)
  38. {
  39. m_tempClients.removeOne(client);
  40. m_clients.removeOne(client);
  41. client->deleteLater();
  42. }
  43. }