ÀÌÁ¦´Â, ´Ù¸¥ ÇÁ·Î±×·¡¸Ó°¡ epollÀÌ ¹ºÁö ¸ô¶óµµ ÀÌ ÁÁÀº ¼º´ÉÀ» ¸¾²¯ ÀÌ¿ëÇÒ ¼ö ÀÖµµ·Ï Ãß»óȸ¦ ½ÃÄѾ߰ڴÙ.
- 1 Class ¼³°èÀÇ °í¹Î
- 1.1 ?TEpoll constructor & destructor ±¸Çö
- 1.2 Add() ¿Í Delete() ÀÇ ±¸Çö
- 1.3 Do() ÇÔ¼öÀÇ ±¸Çö
1 Class ¼³°èÀÇ °í¹Î #
³×Æ®¿öÅ© ¼¹öµµ Çϵµ °¡Áö°¡Áö¶ó¼, ¾î¶² ¼³°è¸¦ ÇÒ Áö °í¹ÎÀ» ÇØ ºÃ´Ù. ¿©·¯°¡Áö ¹æ¹ýµéÀÌ ÀÖ°ÚÁö¸¸,
´ÜÁö EpollÇÔ¼öµéÀ» Ãß»óÈÇϵµ·Ï ¸¸µé¾î º¸±â·Î Çß´Ù.
- ÇÁ·Î±×·¡¸Ó°¡ ¼±È£ÇÏ´Â ¼ÒÄÏÀ̺¥Æ® ó¸® ½ÃÁ¡, Áï epoll_wait ¸¦ ÄÝ ÇÏ´Â ½ÃÁ¡ÀÌ »ç¶÷¸¶´Ù ´Ù¸¦ ¼ö Àֱ⠶§¹®¿¡, Do ¶ó´Â º°µµ public ÇÔ¼ö¸¦ ¸¸µé¾î¼ ÄÝÇϵµ·Ï ÇÑ´Ù.
- epoll¿¡ °ü½É fd ¸¦ Ãß°¡Çϰųª »èÁ¦ÇÏ´Â °ÍÀº ¸ðµÎ ¼öµ¿À¸·Î ÇÑ´Ù.
- listen ¼ÒÄÏÀÇ eventó¸®, Áï acceptµµ µû·Î ó¸®ÇÑ´Ù.
class TEpoll
{
private :
int fd_epoll;
public :
bool is_epoll_init;
TEpoll(const int size);
~TEpoll();
int Add(const int fd);
int Delete(const int fd);
int Do(TPEpollEvent events, const int nwait);
};
1.1 ?TEpoll constructor & destructor ±¸Çö #
»ý¼ºÀÚ¿Í ¼Ò¸êÀÚ ±¸ÇöÀº Ưº°ÇÒ¸¸ÇÑ°Ô ¾ø´Ù. »ý¼ºÀÚ¿¡¼´Â epoll_create ÇØÁÖ°í, ¼Ò¸êÀÚ¿¡¼´Â fd_epoll ¿¡ ´ëÇؼ close 󸮸¦ ÇØÁØ´Ù. °¢ fd¸¶´Ù epoll¿¡¼ deleteÇÒ ÇÊ¿ä´Â ¾ø°í, °¢ fdÀÇ close´Â »ç¿ëÀÚ°¡ ¾Ë¾Æ¼ ÇØÁà¾ß ÇÑ´Ù
TEpoll::TEpoll(const int size)
{
is_epoll_init = (fd_epoll = epoll_create(size)) > 0);
}
TEpoll::~TEpoll()
{
if(is_epoll_init) close(fd_epoll);
}
is_epoll_init boolean º¯¼ö¸¦ µÎ¾î, ÃʱâÈ ½ÇÆнà ´ëÀÀÇϵµ·Ï ÇÏ¿´´Ù. ÃʱâȺ¯¼ö size´Â ÃÖ´ë µ¿Á¢¼ö¸¦ ÀǹÌ.
1.2 Add() ¿Í Delete() ÀÇ ±¸Çö #
// ÀϺΠtypedef
typedef struct epoll_event TEpollEvent;
typedef TEpollEvent * TPEpollEvent;
int TEpoll::Add(const int fd)
{
if(! is_epoll_init) return -1;
TEpollEvent ev;
ev.events = EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLET;
ev.data.fd = fd;
return epoll_ctl(fd_epoll, EPOLL_CTL_ADD, fd, &ev);
}
int TEpoll::Delete(const int fd)
{
if(! is_epoll_init) return -1;
TEpollEvent ev;
ev.events = EPOLLIN | EPOLLOUT | EPOLLERR;
ev.data.fd = fd;
return epoll_ctl(fd_epoll, EPOLL_CTL_DEL, fd, &ev);
}
1.3 Do() ÇÔ¼öÀÇ ±¸Çö #
Do ÇÔ¼ö´Â, Ŭ·¡½º ¿ÜºÎ¿ÍÀÇ °¡Àå Áß¿äÇÑ ÀÎÅÍÆäÀ̽º ÇÔ¼ö·Î ¼³°èÇß´Ù. »ç¿ëÀÚ(ÇÁ·Î±×·¡¸Ó)´Â Àû´çÈ÷ fdµéÀ» »ý¼ºÇؼ
TEpoll:
?:Add()¸¦ ÅëÇØ epollÀÇ °ü½Éfd·Î µî·ÏÀ» ÇÏ°í, ÀÌ Do ÇÔ¼ö¸¦ ÅëÇØ À̺¥Æ®°¡ ¹ß»ýÇÑ fdµéÀ» ¾Ë¾Æ³»µµ·Ï ÇÑ´Ù.
int TEpoll::Do(TPEpollEvent events, const int nwait)
{
if(! is_epoll_init) return -1;
return epoll_wait(fd_epoll, events, MAX_EVENTS, nwait);
}