socket_listener.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef __SOCKET_LISTENER_HPP__
  2. #define __SOCKET_LISTENER_HPP__
  3. // Project libraries
  4. #include <interface/listen_interface.hpp>
  5. #include <interface/send_interface.hpp>
  6. #include <task_queue.hpp>
  7. #include <types.hpp>
  8. #include <constants.hpp>
  9. // System libraries
  10. #include <sys/socket.h>
  11. // C++ Libraries
  12. #include <functional>
  13. #include <iostream>
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. /**
  18. * SocketListener
  19. *
  20. * SocketListener is extensible to aid in architecting a socket server
  21. */
  22. class SocketListener : public SendInterface, public ListenInterface {
  23. public:
  24. /* public classes whose instances are used by SocketListener */
  25. /**
  26. * MessageHandler
  27. *
  28. * Instances of this object type wrap a generic, self-contained function and
  29. * behave as callable functions (functors)
  30. * @class
  31. */
  32. class MessageHandler {
  33. public:
  34. MessageHandler(std::function<void(ssize_t)> cb) : m_cb(cb) {}
  35. void operator()(ssize_t size) { m_cb(size); }
  36. private:
  37. std::function<void(ssize_t)> m_cb;
  38. };
  39. // constructor
  40. SocketListener(int arg_num, char** args);
  41. // destructor
  42. ~SocketListener();
  43. /**
  44. * Send a message to a client socket described by its file descriptor
  45. * @param[in] {int} client_socket_fd The client socket file descriptor
  46. * @param[in] {std::string} The message to be sent
  47. */
  48. virtual void sendMessage(int client_socket_fd,
  49. std::weak_ptr<uint8_t[]> w_buffer_ptr) override;
  50. /** overload variants */
  51. void sendMessage(int client_socket_fd, char* message, bool short_message);
  52. void sendMessage(int client_socket_fd, char* message, size_t size);
  53. void sendMessage(int client_socket_fd, const char* message, size_t size);
  54. MessageHandler createMessageHandler(std::function<void(ssize_t)> cb);
  55. /**
  56. * Perform intialization work
  57. */
  58. bool init();
  59. /**
  60. * Main message loop
  61. */
  62. void run();
  63. /**
  64. * Perform any cleanup work
  65. */
  66. void cleanup();
  67. private:
  68. // private methods
  69. int createSocket();
  70. virtual void onMessageReceived(int client_socket_fd,
  71. std::weak_ptr<uint8_t[]> w_buffer_ptr,
  72. ssize_t& size) override;
  73. virtual void onConnectionClose(int client_socket_fd) override;
  74. int waitForConnection(int listening);
  75. void handleClientSocket(int client_socket_fd,
  76. SocketListener::MessageHandler message_handler,
  77. const std::shared_ptr<uint8_t[]>& s_buffer_ptr);
  78. /* private members */
  79. // Server arguments
  80. std::string m_ip_address;
  81. int m_port;
  82. std::unique_ptr<TaskQueue> u_task_queue_ptr;
  83. };
  84. #endif // __SOCKET_LISTENER_HPP__