socket_listener.h 928 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef __SOCKET_LISTENER_H__
  2. #define __SOCKET_LISTENER_H__
  3. #include <sys/socket.h>
  4. #include <string>
  5. #include "listen_interface.h"
  6. #define MAX_BUFFER_SIZE (49152)
  7. // typedef void (*MessageReceivedHandler)(SocketListener* listener, int
  8. // socketId,
  9. // std::string msg);
  10. class SocketListener : ListenInterface {
  11. public:
  12. // constructor
  13. SocketListener(std::string ipAddress, int port);
  14. // destructor
  15. ~SocketListener();
  16. // public methods
  17. // Send message to client
  18. void sendMessage(int socket, std::string msg);
  19. // Initialize
  20. bool init();
  21. // Main process loop
  22. void run();
  23. // Cleanup
  24. void cleanup();
  25. void onMessageReceived(int socket_id, std::string message);
  26. private:
  27. // private methods
  28. int createSocket();
  29. int waitForConnection(int listening);
  30. // private members
  31. std::string m_ip_address;
  32. int m_port;
  33. };
  34. #endif // __SOCKET_LISTENER_H__