최종본

This commit is contained in:
2025-05-10 19:44:45 +09:00
parent be6417cd40
commit 8b486008fa
2 changed files with 14 additions and 8 deletions

View File

@@ -400,11 +400,11 @@ void ClientManager::run() {
std::cout << "Commads:" << std::endl; std::cout << "Commads:" << std::endl;
std::cout << "/w <username> : Send direct message to specified user" << std::endl; std::cout << "/w <username> : Send direct message to specified user" << std::endl;
std::cout << "/join <roomname> : Enter specified room" << std::endl; std::cout << "/join <roomname> : Enter specified room" << std::endl;
std::cout << "/leave : Exit current room" << std::endl; std::cout << "/exit : Exit current room" << std::endl;
std::cout << "/create <roomname> : Create room" << std::endl; std::cout << "/create <roomname> : Create room" << std::endl;
std::cout << "/userlist : Print all registred users" << std::endl; std::cout << "/userlist : Print all registred users" << std::endl;
std::cout << "/roomlist : Print all registred rooms" << std::endl; std::cout << "/roomlist : Print all registred rooms" << std::endl;
std::cout << "/exit : Terminate this program" << std::endl; std::cout << "/quit : Terminate this program" << std::endl;
while (true) { while (true) {
std::string input; std::string input;
@@ -453,7 +453,7 @@ void ClientManager::run() {
} }
joinRoom(myID_, findRoomId_[roomName]); joinRoom(myID_, findRoomId_[roomName]);
} }
else if (tokens[0] == "/leave") { else if (tokens[0] == "/exit") {
if (!inRoom_) { if (!inRoom_) {
resourceMutex_.lock(); resourceMutex_.lock();
messageQueue_.push("You are not in any room"); messageQueue_.push("You are not in any room");
@@ -470,7 +470,7 @@ void ClientManager::run() {
getUserList(); getUserList();
else if (tokens[0] == "/roomlist") else if (tokens[0] == "/roomlist")
getRoomList(); getRoomList();
else if (tokens[0] == "/exit") { else if (tokens[0] == "/quit") {
break; break;
} }
else if (inRoom_) { else if (inRoom_) {

View File

@@ -270,7 +270,7 @@ public:
threadPool->enqueueJob(callback, data); threadPool->enqueueJob(callback, data);
} }
}; };
static void iocpWatcher(ThreadPool* threadPool, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) { static void iocpWatcher(ThreadPool* threadPool, int epollfd, int epollDetroyerFd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
struct epoll_event events[FD_SETSIZE]; struct epoll_event events[FD_SETSIZE];
pthread_t tid = pthread_self(); pthread_t tid = pthread_self();
@@ -280,6 +280,9 @@ public:
struct epoll_event current_event = events[i]; struct epoll_event current_event = events[i];
if (current_event.events & EPOLLIN) { if (current_event.events & EPOLLIN) {
if (current_event.data.fd == epollDetroyerFd) {
return;
}
std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(callback); std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(callback);
threadPool->enqueueJob(socketReader, current_event, epollfd, task); threadPool->enqueueJob(socketReader, current_event, epollfd, task);
} }
@@ -290,7 +293,7 @@ public:
if (--nready <= 0) if (--nready <= 0)
break; break;
} }
threadPool->enqueueJob(iocpWatcher, epollfd, callback); threadPool->enqueueJob(iocpWatcher, epollfd, epollDetroyerFd, callback);
}; };
#endif #endif
@@ -307,7 +310,10 @@ public:
#elif __linux__ #elif __linux__
epollfd_ = ::epoll_create(1); epollfd_ = ::epoll_create(1);
epollDetroyerFd = ::eventfd(0, EFD_NONBLOCK); epollDetroyerFd = ::eventfd(0, EFD_NONBLOCK);
::epoll_ctl(epollfd_, EPOLL_CTL_ADD, epollDetroyerFd, NULL); struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = epollDetroyerFd;
::epoll_ctl(epollfd_, EPOLL_CTL_ADD, epollDetroyerFd, &ev);
#endif #endif
auto boundFunc = [callback = std::move(callback)](ThreadPool* __IOCPThread, IOCPPASSINDATA* data) mutable { auto boundFunc = [callback = std::move(callback)](ThreadPool* __IOCPThread, IOCPPASSINDATA* data) mutable {
callback(__IOCPThread, data); callback(__IOCPThread, data);
@@ -327,7 +333,7 @@ public:
#elif __linux__ #elif __linux__
__IOCPThread->respawnWorker(__IOCPThread->threadCount + 1); __IOCPThread->respawnWorker(__IOCPThread->threadCount + 1);
spdlog::info("Spawning 1 Epoll Waiter..."); spdlog::info("Spawning 1 Epoll Waiter...");
__IOCPThread->enqueueJob(iocpWatcher, epollfd_, boundFunc); __IOCPThread->enqueueJob(iocpWatcher, epollfd_, epollDetroyerFd, boundFunc);
#endif #endif
} }