-
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
252cdb6
commit 54280c2
Showing
10 changed files
with
242 additions
and
22 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 |
---|---|---|
|
@@ -30,5 +30,6 @@ | |
*.exe | ||
*.out | ||
*.app | ||
build | ||
build/* | ||
cmake-build-debug/* | ||
.idea |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
#include <typeinfo> | ||
#include "base_engine.h" | ||
#include "io_handler.h" | ||
|
||
bool base_engine::add_handler(const std::shared_ptr<io_handler>& handler) { | ||
if(handler->fd() != -1) { | ||
int fd = handler->fd(); | ||
maybe_resize(fd); | ||
std::weak_ptr<io_handler> _handler = _handlers[fd]; | ||
std::cout << "the handler: " << _handler.lock() << std::endl; | ||
if(_handler.lock() == nullptr) { | ||
// 说明是个全新的handler | ||
if(can_add(handler)){ | ||
_handlers[fd] = handler; | ||
_changes.insert(handler); | ||
return true; | ||
} | ||
return false; | ||
}else { | ||
//已经注册到完成了,所以之需要更新_changes就可以了 | ||
_changes.insert(handler); | ||
return true; | ||
} | ||
|
||
} | ||
return false; | ||
} | ||
|
||
void base_engine::maybe_resize(int max_fd) { | ||
if(_handlers.capacity() < max_fd + 1){ | ||
_handlers.resize(max_fd+1); | ||
} | ||
} |
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,33 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
|
||
#ifndef CZSOCKET_BASE_ENGINE_H | ||
#define CZSOCKET_BASE_ENGINE_H | ||
#include <sys/socket.h> | ||
|
||
#include <map> | ||
#include <set> | ||
#include <vector> | ||
#include <memory> | ||
|
||
class io_handler; | ||
|
||
class base_engine { | ||
public: | ||
base_engine() = default; | ||
virtual ~base_engine() = default; | ||
bool add_handler(const std::shared_ptr<io_handler>& handler); | ||
virtual void wakeup() = 0; | ||
virtual void poll(uint64_t millisecond ) = 0; | ||
protected: | ||
virtual bool can_add(const std::shared_ptr<io_handler>& handler) = 0; | ||
typedef std::set<std::weak_ptr<io_handler>,std::owner_less<>> changes_set; | ||
changes_set _changes; | ||
typedef std::vector<std::weak_ptr<io_handler>> handlers_vector; | ||
handlers_vector _handlers; | ||
void maybe_resize(int max_fd); | ||
}; | ||
|
||
|
||
#endif //CZSOCKET_BASE_ENGINE_H |
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,13 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
|
||
#ifndef CZSOCKET_EVENT_H | ||
#define CZSOCKET_EVENT_H | ||
|
||
#define EV_TIMEOUT 0x01 | ||
#define EV_READ 0x02 | ||
#define EV_WRITE 0x04 | ||
#define EV_CLOSED 0x80 | ||
|
||
#endif //CZSOCKET_EVENT_H |
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,9 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
|
||
#include "io_handler.h" | ||
|
||
void io_handler::handle_events(int events) { | ||
std::cout << "the io_handler handle events:" << events << std::endl; | ||
} |
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,23 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
|
||
#ifndef CZSOCKET_IO_HANDLER_H | ||
#define CZSOCKET_IO_HANDLER_H | ||
#include <sys/socket.h> | ||
#include <iostream> | ||
|
||
class io_handler { | ||
public: | ||
io_handler(int socket,int events): _fd(socket),_events(events){}; | ||
virtual ~io_handler() = default; | ||
int fd(){return _fd;}; | ||
int events(){return _events;}; | ||
virtual void handle_events(int events); | ||
private: | ||
int _fd; | ||
int _events; | ||
}; | ||
|
||
|
||
#endif //CZSOCKET_IO_HANDLER_H |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
#include "event.h" | ||
#include "io_handler.h" | ||
#include "select_engine.h" | ||
|
||
select_engine::~select_engine() noexcept { | ||
|
||
} | ||
|
||
void select_engine::wakeup() { | ||
|
||
} | ||
|
||
bool select_engine::can_add(const std::shared_ptr<io_handler>& handler) { | ||
if(handler->fd() < FD_SETSIZE){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
void select_engine::poll(uint64_t millisecond) { | ||
int _res = 0; | ||
int _events = 0; | ||
int _nfds = 0; | ||
FD_ZERO(&_rfds); | ||
FD_ZERO(&_wfds); | ||
_changes.clear(); | ||
std::shared_ptr<io_handler> _handler; | ||
for(int i = 0; i < _handlers.capacity(); i++){ | ||
_handler = _handlers[i].lock(); | ||
if(_handler != nullptr){ | ||
_events = _handler->events(); | ||
if(_events & EV_READ){ | ||
FD_SET(i,&_rfds); | ||
} | ||
if(_events & EV_WRITE){ | ||
FD_SET(i,&_wfds); | ||
} | ||
_nfds = i; | ||
} | ||
} | ||
if(millisecond > 0){ | ||
_tv.tv_usec = millisecond * 1000; | ||
_res = select(_nfds,&_rfds,&_wfds,NULL,&_tv); | ||
}else { | ||
_res = select(_nfds,&_rfds,&_wfds,NULL,NULL); | ||
} | ||
if(_res > 0 ) { | ||
for (int i = 0; i <= _nfds; i++) { | ||
_events = 0; | ||
if(FD_ISSET(i, &_rfds)){ | ||
_events |= EV_READ; | ||
} | ||
if(FD_ISSET(i,&_wfds)){ | ||
_events |= EV_WRITE; | ||
} | ||
if(_events != 0){ | ||
_handler = _handlers[i].lock(); | ||
if(_handler != nullptr){ | ||
_handler->handle_events(_events); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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,30 @@ | ||
// | ||
// Created by david on 11/28/24. | ||
// | ||
|
||
#ifndef CZSOCKET_SELECT_ENGINE_H | ||
#define CZSOCKET_SELECT_ENGINE_H | ||
#include "base_engine.h" | ||
#include <sys/select.h> | ||
#include <map> | ||
|
||
class io_handler; | ||
|
||
class select_engine: public base_engine { | ||
public: | ||
select_engine() = default; | ||
~select_engine(); | ||
void wakeup() override; | ||
void poll(uint64_t millisecond) override; | ||
protected: | ||
bool can_add(const std::shared_ptr<io_handler>& handler) override; | ||
private: | ||
fd_set _rfds; | ||
fd_set _wfds; | ||
struct timeval _tv; | ||
|
||
|
||
}; | ||
|
||
|
||
#endif //CZSOCKET_SELECT_ENGINE_H |