Merge branch 'main' of github.com:lzs123/CProxy

This commit is contained in:
lzs123
2022-08-21 13:06:52 +08:00
14 changed files with 30 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
#include "local_conn.h"
#include <fcntl.h>
#include <string.h>
#include <sys/epoll.h>
#include "tunnel.h"
#include "lib/util.h"

View File

@@ -3,6 +3,7 @@
#include <sys/syscall.h>
#include <unistd.h>
#include <memory>
#include <sys/epoll.h>
#include "lib/event_loop.h"
#include "lib/event_loop_thread.h"
#include "lib/tran_conn.h"

View File

@@ -11,24 +11,24 @@ void Channel::HandleEvents() {
if (!revents_) return;
// 对端close回复rst设置channel的quiting为true
if (revents_ & EPOLLHUP) {
peerClosed_ = true;
peer_closed_ = true;
}
// 本端操作一些动作触发了报错比如对端关闭后还继续往对端写write会成功返回但在wait时会收到这个事件
if (revents_ & EPOLLERR) {
errorHandler_();
error_handler_();
}
// 对端调用close时本端会收到RDHUPEPOLLRDHUP想要被触发需要显式地在epoll_ctl调用时设置在events中此时本端可能还有数据可接收
if (revents_ & EPOLLRDHUP) {
peerClosed_ = true;
peer_closed_ = true;
}
// 数据可读
if (revents_ & (EPOLLIN | EPOLLPRI)) {
readHandler_();
read_handler_();
}
// 当发送缓冲区可写且对端没关闭
if ((revents_ & EPOLLOUT) && !peerClosed_) {
writeHandler_();
if ((revents_ & EPOLLOUT) && !peer_closed_) {
write_handler_();
}
postHandler_();
post_handler_();
}

View File

@@ -8,7 +8,7 @@
class Epoll : public EventDispatcher {
public:
Epoll();
~Epoll(){};
virtual ~Epoll(){};
virtual void PollAdd(SP_Channel) override final;
virtual void PollMod(SP_Channel) override final;
virtual void PollDel(SP_Channel) override final;

View File

@@ -2,6 +2,7 @@
#include <string.h>
#include <exception>
#include <functional>
#include <sys/epoll.h>
#include "buffer.h"
#include "ctl_conn.h"

View File

@@ -1,9 +1,16 @@
#pragma once
#include <memory>
#include <vector>
#include "channel.h"
class EventDispatcher {
public:
EventDispatcher(){};
virtual ~EventDispatcher(){};
virtual void PollAdd(SP_Channel) = 0;
virtual void PollMod(SP_Channel) = 0;
virtual void PollDel(SP_Channel) = 0;
virtual std::vector<SP_Channel> WaitForReadyChannels() = 0;
};
};
using SP_EventDispatcher = std::shared_ptr<EventDispatcher>;

View File

@@ -7,7 +7,7 @@
#include <mutex>
#include <vector>
EventLoop::EventLoop() : poller_(new Epoll()){};
EventLoop::EventLoop() : poller_(SP_Epoll(new Epoll())){};
void EventLoop::AddToPoller(SP_Channel channel) { poller_->PollAdd(channel); }

View File

@@ -3,7 +3,7 @@
#include <mutex>
#include "channel.h"
#include "epoll.h"
#include "event_dispatcher.h"
#include "util.h"
class EventLoop {
@@ -15,7 +15,7 @@ class EventLoop {
void RemoveFromPoller(SP_Channel channel);
private:
SP_Epoll poller_;
SP_EventDispatcher poller_;
};
typedef std::shared_ptr<EventLoop> SP_EventLoop;

View File

@@ -13,10 +13,10 @@ EventLoopThread::~EventLoopThread() {
}
}
void EventLoopThread::ThreadFunc() try {
if (!loop_) {
throw "loop_ is null";
if (loop_) {
throw "loop_ is not null";
}
loop_ = SP_EventLoop(new EventLoop());
{
std::unique_lock<std::mutex> lock(mutex_);
started_ = true;

View File

@@ -11,8 +11,7 @@
class EventLoopThread {
public:
EventLoopThread()
: loop_(new EventLoop()),
thread_(std::bind(&EventLoopThread::ThreadFunc, this)),
: thread_(std::bind(&EventLoopThread::ThreadFunc, this)),
mutex_(),
cond_(){};
~EventLoopThread();

View File

@@ -2,6 +2,8 @@
#include <fcntl.h>
#include <netdb.h>
#include <string.h>
#include <sys/epoll.h>
#include "util.h"
#include "spdlog/spdlog.h"

View File

@@ -2,6 +2,7 @@
#include <memory>
#include <mutex>
#include <sys/epoll.h>
#include "buffer.h"
#include "conn.h"
#include "event_loop.h"

View File

@@ -17,8 +17,7 @@
const int SERVER_LISTEN_EPOLL_EVENTS = (EPOLLIN | EPOLLET | EPOLLRDHUP);
Server::Server(int threadNum, int ctlPort, int proxyPort)
: threadNum_(threadNum),
ctlPort_(ctlPort),
: ctlPort_(ctlPort),
proxyPort_(proxyPort),
ctlListenFd_(socketBindListen(ctlPort_)),
proxyListenFd_(socketBindListen(proxyPort_)),

View File

@@ -30,7 +30,6 @@ class Server : public std::enable_shared_from_this<Server> {
UnclaimedProxyMap* getUnclaimedProxyMapByFd(int fd);
private:
int threadNum_;
int ctlPort_;
int proxyPort_;
int ctlListenFd_;