#ifndef __SOCKET_LISTENER_H__ #define __SOCKET_LISTENER_H__ // Project libraries #include "send_interface.h" #include "types.h" // System libraries #include // C++ Libraries #include #include #include #include #include #include #include #include #include #include class SocketListener : public SendInterface { public: class MessageHandler { public: MessageHandler(std::function cb) : m_cb(cb) {} void operator()() { m_cb(); } private: std::function m_cb; }; // constructor SocketListener(std::string ip_address, int port); // destructor ~SocketListener(); /** * Send a message to a client socket described by its file descriptor * @param[in] {int} client_socket_fd The client socket file descriptor * @param[in] {std::string} The message to be sent */ virtual void sendMessage(int client_socket_fd, std::weak_ptr w_buffer_ptr) override; MessageHandler createMessageHandler(std::function cb); /** * Perform intialization work */ bool init(); /** * Main message loop */ void run(); /** * Perform any cleanup work */ void cleanup(); // virtual void setMessageHandler(MessageHandler message_handler) override; private: // private methods int createSocket(); int waitForConnection(int listening); void loopCheck(); void done(); void handleLoop(); void detachThreads(); void pushToQueue(std::function fn); void handleClientSocket(int client_socket_fd, SocketListener::MessageHandler message_handler, const std::shared_ptr& s_buffer_ptr); /* private members */ // Server arguments std::string m_ip_address; int m_port; std::thread m_loop_thread; std::mutex m_mutex_lock; std::condition_variable pool_condition; std::atomic accepting_tasks; std::queue> task_queue; std::vector thread_pool; }; #endif // __SOCKET_LISTENER_H__