Skip to content

Commit

Permalink
fix the common
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAlphaFox committed Dec 3, 2024
1 parent f46031c commit 7f3c9e3
Show file tree
Hide file tree
Showing 27 changed files with 398 additions and 306 deletions.
15 changes: 7 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ set(CMAKE_C_STANDARD 17)

set(SOURCE_FILES
src/common/cbuffer.cpp
src/common/base_handler.cpp
src/common/timer_handler.cpp
src/common/io_handler.cpp
src/common/wakeup_handler.cpp
src/common/wakeup_operation.cpp
src/common/wakup_handler.cpp
src/common/timer_manager.cpp
src/common/base_engine.cpp
src/reactor.cpp
)
src/common/io_manager.cpp
src/reactor.cpp)

if(UNIX)
set(SOURCE_FILES
${SOURCE_FILES}
src/unix/core.cpp
src/unix/pipe.cpp
src/unix/socket.cpp
src/unix/unix_base_engine.cpp
src/unix/select_engine.cpp
src/unix/tcp_handler.cpp)
src/unix/select_io_manager.cpp
src/unix/tcp_handler.cpp
src/unix/unix_wakeup_handler.cpp)
endif()

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
Expand Down
11 changes: 0 additions & 11 deletions src/common/base_handler.cpp

This file was deleted.

44 changes: 0 additions & 44 deletions src/common/base_handler.h

This file was deleted.

3 changes: 2 additions & 1 deletion src/common/cbuffer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <cstddef>
#include <memory>
#include <string>

class cbuffer {
class cbuffer: public std::enable_shared_from_this<cbuffer> {

public:
cbuffer();
Expand Down
7 changes: 2 additions & 5 deletions src/common/io_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// Created by david on 11/28/24.
//
#include <iostream>

#include "io_handler.h"

io_handler::~io_handler() noexcept
{
}
#include "io_handler.h"
33 changes: 22 additions & 11 deletions src/common/io_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@

#ifndef CZSOCKET_IO_HANDLER_H
#define CZSOCKET_IO_HANDLER_H
#include <iostream>
#include <memory>
#include "timer_handler.h"
class io_manager;
class reactor;

#include "base_handler.h"

class io_handler : virtual public base_handler
class io_handler: public timer_handler
{
public:
explicit io_handler(): io_handler(-1)
io_handler() = default;
virtual ~io_handler() = default;
void watch(int events)
{
_pending_events |= events;
};

explicit io_handler(int fd): base_handler(), _fd(fd)
void unwatch(int events)
{
_pending_events &= ~events;
};

virtual ~io_handler();
int events() const { return _events; };
int pending_events() const { return _pending_events; };
void apply_pending_events() { _events = _pending_events; };
int fd() { return _fd; };
virtual void handle_io(int events) = 0;
protected:

void set_fd(int fd) { _fd = fd; };
virtual void handle_io(int events) {};

private:
int _fd;
int _fd = -1;
int _pending_events = 0;
int _events = 0 ;
};


#endif //CZSOCKET_IO_HANDLER_H
8 changes: 4 additions & 4 deletions src/common/base_engine.cpp → src/common/io_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Created by david on 11/28/24.
//
#include <typeinfo>
#include "base_engine.h"
#include "io_manager.h"

#include "../chez_socket.h"
#include "io_handler.h"

bool base_engine::add_handler(const std::shared_ptr<io_handler>& handler)
bool io_manager::add_handler(const std::shared_ptr<io_handler>& handler)
{
bool ret = false;
if (handler->fd() != -1)
Expand All @@ -32,7 +32,7 @@ bool base_engine::add_handler(const std::shared_ptr<io_handler>& handler)
return ret;
}

void base_engine::remove_handler(const std::shared_ptr<io_handler>& handler)
void io_manager::remove_handler(const std::shared_ptr<io_handler>& handler)
{
if (handler->fd() != -1)
{
Expand All @@ -41,7 +41,7 @@ void base_engine::remove_handler(const std::shared_ptr<io_handler>& handler)
}
}

void base_engine::maybe_resize(int max_fd)
void io_manager::maybe_resize(int max_fd)
{
if (_handlers.capacity() < max_fd + 1)
{
Expand Down
9 changes: 3 additions & 6 deletions src/common/base_engine.h → src/common/io_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
#include <memory>
#include "../chez_socket.h"
class io_handler;
class wakeup_handler;

class base_engine
class io_manager
{
public:
base_engine() = default;
virtual ~base_engine() = default;
io_manager() = default;
virtual ~io_manager() = default;
bool add_handler(const std::shared_ptr<io_handler>& handler);
void remove_handler(const std::shared_ptr<io_handler>& handler);
virtual void wakeup() = 0;
virtual void wakeup(wakeup_handler* handler, void* data,size_t length) = 0;
virtual void poll(uint64_t millisecond) = 0;
protected:
void maybe_resize(int max_fd);
Expand Down
14 changes: 9 additions & 5 deletions src/common/tcp_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@

#ifndef TCP_HANDLER_H
#define TCP_HANDLER_H
#include<string>
#include <sys/socket.h>
#include "io_handler.h"
#include "timer_handler.h"


class tcp_handler: public io_handler{
class tcp_handler: protected io_handler {
public:
tcp_handler(int socket);
tcp_handler(int socket,bool connected = false);;
tcp_handler() = default;
~tcp_handler();
void do_connect(const struct sockaddr* addr, unsigned int addr_len);
protected:
void handle_io(int events) final;
void handle_timeout() override {};
void handle_reactor_attached() override{};
private:
void make_socket();

private:
bool _connected = false;
bool _bind = false;
int _domain = AF_UNSPEC;
struct sockaddr *_addr = nullptr;

};

Expand Down
4 changes: 1 addition & 3 deletions src/common/timer_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//
// Created by david on 11/28/24.
//

#include "timer_handler.h"

timer_handler::~timer_handler()
{
detach_reactor();
}
22 changes: 17 additions & 5 deletions src/common/timer_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@

#ifndef CHEZ_SOCKET_TIMER_HANDLER_H
#define CHEZ_SOCKET_TIMER_HANDLER_H
#include <string>
#include <iostream>

#include <memory>
#include "../chez_socket.h"
#include "base_handler.h"
class reactor;

class timer_handler : virtual public base_handler
class timer_handler: public std::enable_shared_from_this<timer_handler>
{
friend class reactor;
public:
explicit timer_handler()=default;
virtual ~timer_handler();
virtual void handle_timer(){};
virtual void handle_timeout() = 0 ;
protected:
virtual void handle_reactor_attached() =0;
std::shared_ptr<reactor> attached_reactor() const { return _reactor; };
void attach_reactor(const std::shared_ptr<reactor>& r)
{
_reactor = r;
handle_reactor_attached();
};
void detach_reactor() { _reactor = nullptr; };
private:
std::shared_ptr<reactor> _reactor;
};


Expand Down
5 changes: 0 additions & 5 deletions src/common/wakeup_handler.cpp

This file was deleted.

24 changes: 13 additions & 11 deletions src/common/wakeup_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
#ifndef WAKEUP_HANDLER_H
#define WAKEUP_HANDLER_H
#include <memory>
#include "base_handler.h"
class wakeup_handler;
#include "io_handler.h"
class wakeup_operation;
struct wakeup_msg
{
wakeup_handler* handler;
wakeup_operation* operation;
size_t length;
void* data;
};

class wakeup_handler: virtual public base_handler {
friend class reactor;
class wakeup_handler: protected io_handler {
public:
wakeup_handler() = default;
virtual ~wakeup_handler() = default;
virtual void handle_wakeup(const wakeup_msg& msg) {

};

wakeup_handler() = default;
virtual ~wakeup_handler() = default;
virtual void wakeup() = 0;
virtual void wakeup(wakeup_operation* operation, void* data,size_t length) = 0;
protected:
virtual void handle_wakeup() = 0;
void handle_reactor_attached() final;
void handle_io(int events) final;
void handle_timeout() final;
};


Expand Down
10 changes: 10 additions & 0 deletions src/common/wakeup_operation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by david on 12/3/24.
//

#include "wakeup_operation.h"

void wakeup_operation::handle_wakeup(void *data, size_t size)
{

}
19 changes: 19 additions & 0 deletions src/common/wakeup_operation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by david on 12/3/24.
//

#ifndef WAKEUP_OPERATION_H
#define WAKEUP_OPERATION_H
#include <unistd.h>
#include <cstdint>

class wakeup_operation {
public:
wakeup_operation() = default;
~wakeup_operation() = default;
virtual void handle_wakeup(void* data, size_t size);
};



#endif //WAKEUP_OPERATION_H
26 changes: 26 additions & 0 deletions src/common/wakup_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by david on 12/3/24.
//

#include <cstring>

#include "wakeup_handler.h"
#include "../reactor.h"

void wakeup_handler::handle_timeout()
{
io_handler::handle_timeout();
}


void wakeup_handler::handle_io(int events)
{
if (events & EV_READ)
{
handle_wakeup();
}
}
void wakeup_handler::handle_reactor_attached()
{
attached_reactor()->add_io_handler(std::dynamic_pointer_cast<io_handler>(shared_from_this()),EV_READ);
}
Loading

0 comments on commit 7f3c9e3

Please sign in to comment.