-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a1fb25
commit 38d595d
Showing
8 changed files
with
113 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "Epoll.h" | ||
#include "utils.h" | ||
|
||
|
||
|
||
Epoll::Epoll(int max_fd) | ||
{ | ||
this->_fd = epoll_create(max_fd); | ||
print_error(this->_fd == -1, "epoll_create failed"); | ||
|
||
} | ||
Epoll::~Epoll() | ||
{ | ||
if (this->events != nullptr) | ||
{ | ||
delete[] this->events; | ||
} | ||
::close(this->_fd); | ||
} | ||
|
||
void Epoll::ctl_add(int fd, epoll_event *ev_ptr) | ||
{ | ||
int ret = epoll_ctl(this->_fd, EPOLL_CTL_ADD, fd, ev_ptr); | ||
print_error(ret == -1, "epoll_ctl add failed"); | ||
} | ||
|
||
void Epoll::ctl_mod(int fd, epoll_event *ev_ptr) | ||
{ | ||
int ret = epoll_ctl(this->_fd, EPOLL_CTL_MOD, fd, ev_ptr); | ||
print_error(ret == -1, "epoll_ctl mod failed"); | ||
} | ||
|
||
void Epoll::ctl_del(int fd, epoll_event *ev_ptr) | ||
{ | ||
int ret = epoll_ctl(this->_fd, EPOLL_CTL_DEL, fd, ev_ptr); | ||
print_error(ret == -1, "epoll_ctl del failed"); | ||
} | ||
|
||
int Epoll::wait(int max_event, int timeout) | ||
{ | ||
if (this->events != nullptr) | ||
{ | ||
delete[] this->events; | ||
} | ||
this->events = new epoll_event[max_event]; | ||
int event_num = epoll_wait(this->_fd, events, max_event, timeout); | ||
print_error(event_num == -1, "epoll_wait failed"); | ||
return event_num; | ||
} | ||
|
||
struct epoll_event creat_event(int fd, uint32_t events, bool is_et) | ||
{ | ||
struct epoll_event event; | ||
memset(&event, 0, sizeof(event)); | ||
event.data.fd = fd; | ||
event.events = events; | ||
if (is_et){ event.events |= EPOLLET; } | ||
return event; // 怕返回的是局部變數,所以要用拷貝 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <sys/epoll.h> | ||
#include <string> | ||
#include <cstring> | ||
|
||
|
||
using namespace std; | ||
|
||
class Epoll | ||
{ | ||
int _fd; | ||
|
||
public: | ||
struct epoll_event* events = nullptr; | ||
Epoll(int max_fd=10); | ||
~Epoll(); | ||
void ctl_add(int fd, epoll_event *event); | ||
void ctl_mod(int fd, epoll_event *event); | ||
void ctl_del(int fd, epoll_event *event); | ||
int wait(int max_event, int timeout=-1); | ||
}; | ||
|
||
struct epoll_event create_event(int fd, uint32_t events, bool is_et); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
server: | ||
@echo "Starting server..." | ||
g++ server.cpp utils.cpp Socket.cpp -o output/server | ||
g++ server.cpp utils.cpp Socket.cpp Epoll.cpp -o output/server | ||
g++ client.cpp utils.cpp Socket.cpp -o output/client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,5 +36,8 @@ | |
- study epoll model, Edge Triggered and Level Triggered | ||
- 實作epoll模型 | ||
|
||
4/21 | ||
- 重構socket成一個class | ||
- 重構epoll成一個class | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters