123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #ifndef __SOCKET_LISTENER_H__
- #define __SOCKET_LISTENER_H__
- #include "send_interface.h"
- #include "types.h"
- #include <sys/socket.h>
- #include <atomic>
- #include <condition_variable>
- #include <functional>
- #include <iostream>
- #include <memory>
- #include <mutex>
- #include <queue>
- #include <string>
- #include <thread>
- #include <vector>
- class SocketListener : public SendInterface {
- public:
- class MessageHandler {
- public:
- MessageHandler(std::function<void()> cb) : m_cb(cb) {}
- void operator()() { m_cb(); }
- private:
- std::function<void()> m_cb;
- };
-
- SocketListener(int arg_num, char** args);
-
- ~SocketListener();
-
- virtual void sendMessage(int client_socket_fd,
- std::weak_ptr<char[]> w_buffer_ptr) override;
- MessageHandler createMessageHandler(std::function<void()> cb);
-
- bool init();
-
- void run();
-
- void cleanup();
-
- private:
-
- int createSocket();
- int waitForConnection(int listening);
- void loopCheck();
- void done();
- void handleLoop();
- void detachThreads();
- void pushToQueue(std::function<void()> fn);
- void handleClientSocket(int client_socket_fd,
- SocketListener::MessageHandler message_handler,
- const std::shared_ptr<char[]>& s_buffer_ptr);
-
-
- 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<bool> accepting_tasks;
- std::queue<std::function<void()>> task_queue;
- std::vector<std::thread> thread_pool;
- };
- #endif
|