Skip to content

Commit

Permalink
very base select engine
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAlphaFox committed Nov 28, 2024
1 parent 252cdb6 commit 54280c2
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
*.exe
*.out
*.app
build
build/*
cmake-build-debug/*
.idea
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ project(czsocket VERSION 1.0.1 DESCRIPTION "socket library for chez scheme")
set(CMAKE_CXX_STANDARD 17)

add_library(${PROJECT_NAME} SHARED
src/timer_manager.cpp
src/timer_handler.cpp
src/timer_manager.cpp
src/io_handler.cpp
src/base_engine.cpp
src/select_engine.cpp
)

add_executable(test
Expand Down
36 changes: 36 additions & 0 deletions src/base_engine.cpp
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);
}
}
33 changes: 33 additions & 0 deletions src/base_engine.h
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
13 changes: 13 additions & 0 deletions src/event.h
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
9 changes: 9 additions & 0 deletions src/io_handler.cpp
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;
}
23 changes: 23 additions & 0 deletions src/io_handler.h
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
45 changes: 25 additions & 20 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#include <iostream>
#include <chrono>
#include <thread>
#include "event.h"
#include "timer_handler.h"
#include "timer_manager.h"
#include "io_handler.h"
#include "select_engine.h"

class my_timer_handler :public timer_handler {
public:
Expand Down Expand Up @@ -33,26 +36,28 @@ void log_timer_item(const timer_item& item)


int main() {
std::shared_ptr <timer_manager> _timer_manager = std::make_shared<timer_manager>();
std::shared_ptr <timer_handler> _handler1 = std::make_shared<my_timer_handler>();
std::shared_ptr <timer_handler> _handler2 = std::make_shared<my_timer_handler>();
std::cout << "handler1: " << _handler1 << " handler2: " << _handler2 << std::endl;
_timer_manager->add_item(_handler1, 500, true);
_timer_manager->add_item(_handler2, 400, false);
timer_item _item;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
for(int i = 0; i < 7; i ++){
std::cout << "round: " << i << std::endl;
_timer_manager->get_latest_item(_item);
log_timer_item(_item);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
_timer_manager->remove_first();
_item.handler.reset();
if( i == 5){
_timer_manager->remove_item(_handler1);
}
}

// std::shared_ptr <timer_manager> _timer_manager = std::make_shared<timer_manager>();
// std::shared_ptr <timer_handler> _handler1 = std::make_shared<my_timer_handler>();
// std::shared_ptr <timer_handler> _handler2 = std::make_shared<my_timer_handler>();
// std::cout << "handler1: " << _handler1 << " handler2: " << _handler2 << std::endl;
// _timer_manager->add_item(_handler1, 500, true);
// _timer_manager->add_item(_handler2, 400, false);
// timer_item _item;
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// for(int i = 0; i < 7; i ++){
// std::cout << "round: " << i << std::endl;
// _timer_manager->get_latest_item(_item);
// log_timer_item(_item);
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
// _timer_manager->remove_first();
// _item.handler.reset();
// if( i == 5){
// _timer_manager->remove_item(_handler1);
// }
// }
std::shared_ptr<io_handler> _handler = std::make_shared<io_handler>(1,EV_TIMEOUT| EV_READ| EV_WRITE);
std::shared_ptr<select_engine> _engine = std::make_shared<select_engine>();
_engine->add_handler(_handler);
std::cout << "end of life" << std::endl;
return 0;
}
67 changes: 67 additions & 0 deletions src/select_engine.cpp
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);
}
}
}
}

}
30 changes: 30 additions & 0 deletions src/select_engine.h
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

0 comments on commit 54280c2

Please sign in to comment.