No Description
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.

comgui.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2018 dimercur
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __COMGUI_H__
  18. #define __COMGUI_H__
  19. #include "messages.h"
  20. #include <string>
  21. using namespace std;
  22. /**
  23. * Class used for generating a server and communicating through it with GUI
  24. *
  25. * @brief Communication class with GUI (server)
  26. *
  27. */
  28. class ComGui {
  29. public:
  30. /**
  31. * Constructor
  32. */
  33. ComGui() {}
  34. /**
  35. * Destructor
  36. */
  37. virtual ~ComGui() {}
  38. /**
  39. * Create a server and open a socket over TCP
  40. *
  41. * @param port Port used for communication
  42. * @return Socket number
  43. * @throw std::runtime_error if it fails
  44. */
  45. int Open(int port);
  46. /**
  47. * Close socket and server
  48. */
  49. void Close();
  50. /**
  51. * Wait for a client to connect
  52. * @return Client number
  53. * @throw std::runtime_error if it fails
  54. */
  55. int AcceptClient();
  56. /**
  57. * Send a message to GUI
  58. *
  59. * @param msg Message to send to GUI
  60. * @attention Message given in parameter will be destroyed (delete) after being sent. No need for user to delete message after that.
  61. * @warning Write is not thread safe : check that multiple tasks can't access this method simultaneously
  62. */
  63. void Write(Message* msg);
  64. /**
  65. * Function called at beginning of Write method
  66. * Use it to do some synchronization (call of mutex, for example)
  67. */
  68. virtual void Write_Pre() {}
  69. /**
  70. * Function called at end of Write method
  71. * Use it to do some synchronization (release of mutex, for example)
  72. */
  73. virtual void Write_Post() {}
  74. protected:
  75. /**
  76. * Socket descriptor
  77. */
  78. int socketFD = -1;
  79. /**
  80. * Socket descriptor
  81. */
  82. int clientID = -1;
  83. /**
  84. * Method used internally to convert a message content to a string that can be sent over TCP
  85. * @param msg Message to be converted
  86. * @return A string, image of the message
  87. */
  88. string *MessageToString(Message *msg);
  89. };
  90. #endif /* __COMGUI_H__ */