#include "stdafx.h" #include "Connector.h" #include "Session.h" #include "SessionList.h" #include "IoHandler.h" #include "IOCPServer.h" //================================================================================================== Connector::Connector() : io_handler_(NULL), connecting_list_(NULL), shutdown_(false), number_of_threads_(0) { for (int i = 0; i < static_cast(sizeof(thread_handles_) / sizeof(thread_handles_[0])); ++i) { thread_handles_[i] = INVALID_HANDLE_VALUE; } event_handles_[0] = CreateEvent(NULL, false, false, NULL); event_handles_[1] = CreateEvent(NULL, false, false, NULL); } Connector::~Connector() { if (shutdown_ == false) { Shutdown(); } for (DWORD i = 0; i < number_of_threads_; ++i) { HANDLE* handle_it = &thread_handles_[i]; if (*handle_it != INVALID_HANDLE_VALUE) { CloseHandle(*handle_it); *handle_it = INVALID_HANDLE_VALUE; // (f100615.1L) add a routine for an initialization } } CloseHandle(event_handles_[0]); CloseHandle(event_handles_[1]); SAFE_DELETE(connecting_list_); } //================================================================================================== /** @remarks connect¿¡ ¼º°ø½Ã È£ÃâµÇ¸ç ±× ¼¼¼ÇÀ» ConnSuccessList¿¡ Ãß°¡ÇÑ´Ù. @param pServer ¼­¹öÀÇ Æ÷ÀÎÅÍ */ //================================================================================================== void Connector::Init(IoHandler* io_handler, DWORD number_of_threads) { assert(number_of_threads <= MAX_CONNECT_THREAD); io_handler_ = io_handler; number_of_threads_ = min(number_of_threads, MAX_CONNECT_THREAD); // Á¢¼ÓÀ» À§ÇØ ´ë±âÇÒ ¼¼¼Ç Å¥ »ý¼º if (connecting_list_) { delete connecting_list_; } connecting_list_ = new SessionList; // ¿öÄ¿ ¾²·¹µåµéÀ» »ý¼ºÇÑ´Ù. unsigned thread_id; for (DWORD i = 0; i < number_of_threads_; ++i) { thread_handles_[i] = \ (HANDLE)_beginthreadex(NULL, 0, connect_thread, (LPVOID)this, 0, &thread_id); } } //================================================================================================== /** @remarks connect ¿öÄ¿ ¾²·¹µå¸¦ »ý¼ºÇϰí Á¢¼ÓÀ» ½ÃµµÇÑ´Ù. @param session Ä¿³ØÆ®¸¦ ½ÃµµÇϱâ À§ÇÑ ¼¼¼ÇÀÇ Æ÷ÀÎÅÍ @param szIP Á¢¼ÓÀ» ½ÃµµÇÒ IP @param port Á¢¼ÓÇÒ Æ÷Æ® */ //================================================================================================== void Connector::Connect(Session* session) { // ¼¼¼ÇÀ» Á¢¼Ó ´ë±â Å¥¿¡ Ãß°¡ÇÑ´Ù. connecting_list_->Lock(); connecting_list_->push_back(session); connecting_list_->Unlock(); // ¿öÄ¿ ¾²·¹µå¿¡ Ä¿³ØÆ® À̺¥Æ®¸¦ º¸³½´Ù. SetEvent(event_handles_[0]); } //================================================================================================== /** @remarks connect ¿öÄ¿ ¾²·¹µå¸¦ Á¾·á½ÃŲ´Ù. */ //================================================================================================== void Connector::Shutdown() { shutdown_ = true; // ¾²·¹µå Á¾·á À̺¥Æ® for (DWORD i = 0; i < number_of_threads_; ++i) { SetEvent(event_handles_[1]); } // ¸ðµç ¿öÄ¿ ¾²·¹µå°¡ Á¾·áµÉ ¶§±îÁö ±â´Ù¸°´Ù. WaitForMultipleObjects(number_of_threads_, thread_handles_, true, INFINITE); }