2024-12-25 19:37:28 +08:00
|
|
|
#pragma once
|
2025-01-10 15:00:50 +08:00
|
|
|
#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);
|
2025-01-10 15:00:50 +08:00
|
|
|
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;
|
2025-01-10 15:00:50 +08:00
|
|
|
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
|
|
|
}
|