mirror of
https://github.com/huiwan-code/CProxy.git
synced 2026-01-12 00:04:07 +08:00
Merge branch 'main' of github.com:lzs123/CProxy
This commit is contained in:
@@ -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"
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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时,本端会收到RDHUP,EPOLLRDHUP想要被触发,需要显式地在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_();
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <string.h>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include "buffer.h"
|
||||
#include "ctl_conn.h"
|
||||
|
||||
@@ -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>;
|
||||
@@ -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); }
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <sys/epoll.h>
|
||||
#include "buffer.h"
|
||||
#include "conn.h"
|
||||
#include "event_loop.h"
|
||||
|
||||
@@ -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_)),
|
||||
|
||||
@@ -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_;
|
||||
|
||||
Reference in New Issue
Block a user