printerssocketserver.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "iostream"
  2. #include "spdlog/spdlog.h"
  3. #include "printerssocketserver.h"
  4. PrintersSocketServer::PrintersSocketServer()
  5. {
  6. this->running = new QAtomicInteger(0);
  7. this->server = Q_NULLPTR;
  8. }
  9. /**
  10. * @brief PrintersSocketServer::setupServer
  11. */
  12. void PrintersSocketServer::setupServer()
  13. {
  14. auto console = spdlog::stdout_logger_mt("console", true);
  15. console->info("Welcome to spdlog");
  16. this->server = new QWebSocketServer(QStringLiteral("Printers Server"), QWebSocketServer::NonSecureMode);
  17. this->server->listen(QHostAddress("127.0.0.1"), 7800);
  18. if (this->server->isListening())
  19. {
  20. this->running->store(1);
  21. console->info("Server is listening");
  22. }
  23. }
  24. /**
  25. * @brief PrintersSocketServer::isRunning
  26. * @return
  27. */
  28. bool PrintersSocketServer::isRunning()
  29. {
  30. return this->running == 0;
  31. }
  32. /**
  33. * @brief PrintersSocketServer::stop
  34. */
  35. void PrintersSocketServer::stop()
  36. {
  37. if (!this->server->isListening()) {
  38. return;
  39. }
  40. while(this->server->hasPendingConnections())
  41. {
  42. const QTcpSocket &socket = this->server->nextPendingConnection();
  43. socket.close();
  44. }
  45. this->server->close();
  46. this->running->store(0);
  47. }