explanation:

This commit is contained in:
Hebin Chen 2024-08-03 03:23:33 -07:00
parent 9ec8699eed
commit 7dbe7409ec
2 changed files with 41 additions and 0 deletions

View File

@ -16,6 +16,12 @@
#include "ILog.h" #include "ILog.h"
#include "ITcpServer.h" #include "ITcpServer.h"
#include <memory> #include <memory>
/**
* @brief Get the singleton instance of TcpServerHandle
* @param impl Optional implementation pointer to replace the current instance
* @return The singleton instance of TcpServerHandle
*/
std::shared_ptr<TcpServerHandle> &TcpServerHandle::GetInstance(std::shared_ptr<TcpServerHandle> *impl) std::shared_ptr<TcpServerHandle> &TcpServerHandle::GetInstance(std::shared_ptr<TcpServerHandle> *impl)
{ {
static auto instance = std::make_shared<TcpServerHandle>(); static auto instance = std::make_shared<TcpServerHandle>();
@ -30,11 +36,24 @@ std::shared_ptr<TcpServerHandle> &TcpServerHandle::GetInstance(std::shared_ptr<T
} }
return instance; return instance;
} }
/**
* @brief Add a server
* @param fd File descriptor of the server
* @param server Shared pointer to the ITcpServer instance
*/
void TcpServerHandle::AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server) void TcpServerHandle::AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server)
{ {
LogInfo("AddServer fd = %d\n", fd); LogInfo("AddServer fd = %d\n", fd);
mFd[fd] = server; mFd[fd] = server;
} }
/**
* @brief Get a server by file descriptor
* @param fd File descriptor of the server
* @param server Reference to a shared pointer to store the ITcpServer instance
* @return True if the server is found, false otherwise
*/
bool TcpServerHandle::GetServer(const int &fd, std::shared_ptr<ITcpServer> &server) bool TcpServerHandle::GetServer(const int &fd, std::shared_ptr<ITcpServer> &server)
{ {
auto it = mFd.find(fd); auto it = mFd.find(fd);

View File

@ -20,10 +20,32 @@
class TcpServerHandle class TcpServerHandle
{ {
public: public:
/**
* @brief Default constructor
*/
TcpServerHandle() = default; TcpServerHandle() = default;
/**
* @brief Default destructor
*/
~TcpServerHandle() = default; ~TcpServerHandle() = default;
/**
* @brief Get the singleton instance of TcpServerHandle
* @param impl Optional implementation pointer to replace the current instance
* @return The singleton instance of TcpServerHandle
*/
static std::shared_ptr<TcpServerHandle> &GetInstance(std::shared_ptr<TcpServerHandle> *impl = nullptr); static std::shared_ptr<TcpServerHandle> &GetInstance(std::shared_ptr<TcpServerHandle> *impl = nullptr);
/**
* @brief Add a server
* @param fd File descriptor of the server
* @param server Shared pointer to the ITcpServer instance
*/
void AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server); void AddServer(const int &fd, const std::shared_ptr<ITcpServer> &server);
/**
* @brief Get a server by file descriptor
* @param fd File descriptor of the server
* @param server Reference to a shared pointer to store the ITcpServer instance
* @return True if the server is found, false otherwise
*/
bool GetServer(const int &fd, std::shared_ptr<ITcpServer> &server); bool GetServer(const int &fd, std::shared_ptr<ITcpServer> &server);
private: private: