tinyrpc/includes/net/tcp/io_thread.hpp
2025-01-21 16:35:26 +08:00

42 lines
949 B
C++

#pragma once
#include "reactor.hpp"
#include "tcp_connection.hpp"
#include <mutex>
#include <thread>
#include <unordered_map>
#include <memory>
#include <vector>
namespace tinyrpc {
class TcpServer;
class IOThread {
friend class IOThreadPool;
public:
void addClient(TcpServer* ser, int fd);
static IOThread* getThisIoThread();
// void removeFd(int fd);
Reactor* getReactor() {return m_reactor;}
private:
IOThread();
~IOThread();
void mainFunc();
private:
std::unordered_map<int, std::shared_ptr<TcpConnection>> m_clients;
std::thread m_thread;
Reactor* m_reactor{nullptr};
};
class IOThreadPool {
public:
IOThreadPool(int size);
IOThread* getIOThread();
~IOThreadPool();
private:
std::mutex m_mtx;
int m_idx {-1};
const std::vector<IOThread*> m_IOThreads{};
};
}