tinyrpc/includes/net/tcp/io_thread.hpp

42 lines
949 B
C++
Raw Normal View History

2024-12-25 19:37:28 +08:00
#pragma once
#include "reactor.hpp"
2024-12-25 19:37:28 +08:00
#include "tcp_connection.hpp"
2025-01-16 14:41:46 +08:00
#include <mutex>
2024-12-25 19:37:28 +08:00
#include <thread>
#include <unordered_map>
2025-01-14 15:27:15 +08:00
#include <memory>
2025-01-16 14:41:46 +08:00
#include <vector>
2024-12-25 19:37:28 +08:00
namespace tinyrpc {
2025-01-21 16:35:26 +08:00
class TcpServer;
2024-12-25 19:37:28 +08:00
class IOThread {
2025-01-16 14:41:46 +08:00
friend class IOThreadPool;
2024-12-25 19:37:28 +08:00
public:
2025-01-21 16:35:26 +08:00
void addClient(TcpServer* ser, int fd);
static IOThread* getThisIoThread();
// void removeFd(int fd);
Reactor* getReactor() {return m_reactor;}
2024-12-25 19:37:28 +08:00
private:
2025-01-16 14:41:46 +08:00
IOThread();
~IOThread();
2024-12-25 19:37:28 +08:00
void mainFunc();
private:
2025-01-14 15:27:15 +08:00
std::unordered_map<int, std::shared_ptr<TcpConnection>> m_clients;
2024-12-25 19:37:28 +08:00
std::thread m_thread;
Reactor* m_reactor{nullptr};
2024-12-25 19:37:28 +08:00
};
2025-01-16 14:41:46 +08:00
class IOThreadPool {
public:
IOThreadPool(int size);
IOThread* getIOThread();
~IOThreadPool();
private:
std::mutex m_mtx;
int m_idx {-1};
const std::vector<IOThread*> m_IOThreads{};
};
2024-12-25 19:37:28 +08:00
}