forked from linyacool/WebServer
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
40 changed files
with
2,253 additions
and
2,763 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Language: Cpp | ||
BasedOnStyle: Google |
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 |
---|---|---|
@@ -1,64 +1,47 @@ | ||
// @Author Lin Ya | ||
// @Email [email protected] | ||
#include "Channel.h" | ||
#include "Util.h" | ||
#include "Epoll.h" | ||
#include "EventLoop.h" | ||
|
||
#include <unistd.h> | ||
#include <queue> | ||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
#include <queue> | ||
|
||
#include "Epoll.h" | ||
#include "EventLoop.h" | ||
#include "Util.h" | ||
|
||
using namespace std; | ||
|
||
Channel::Channel(EventLoop *loop): | ||
loop_(loop), | ||
events_(0), | ||
lastEvents_(0), | ||
fd_(0) | ||
{ } | ||
Channel::Channel(EventLoop *loop) | ||
: loop_(loop), events_(0), lastEvents_(0), fd_(0) {} | ||
|
||
Channel::Channel(EventLoop *loop, int fd): | ||
loop_(loop), | ||
fd_(fd), | ||
events_(0), | ||
lastEvents_(0) | ||
{ } | ||
Channel::Channel(EventLoop *loop, int fd) | ||
: loop_(loop), fd_(fd), events_(0), lastEvents_(0) {} | ||
|
||
Channel::~Channel() | ||
{ | ||
//loop_->poller_->epoll_del(fd, events_); | ||
//close(fd_); | ||
Channel::~Channel() { | ||
// loop_->poller_->epoll_del(fd, events_); | ||
// close(fd_); | ||
} | ||
|
||
int Channel::getFd() | ||
{ | ||
return fd_; | ||
} | ||
void Channel::setFd(int fd) | ||
{ | ||
fd_ = fd; | ||
} | ||
int Channel::getFd() { return fd_; } | ||
void Channel::setFd(int fd) { fd_ = fd; } | ||
|
||
void Channel::handleRead() | ||
{ | ||
if (readHandler_) | ||
{ | ||
readHandler_(); | ||
} | ||
void Channel::handleRead() { | ||
if (readHandler_) { | ||
readHandler_(); | ||
} | ||
} | ||
|
||
void Channel::handleWrite() | ||
{ | ||
if (writeHandler_) | ||
{ | ||
writeHandler_(); | ||
} | ||
void Channel::handleWrite() { | ||
if (writeHandler_) { | ||
writeHandler_(); | ||
} | ||
} | ||
|
||
void Channel::handleConn() | ||
{ | ||
if (connHandler_) | ||
{ | ||
connHandler_(); | ||
} | ||
void Channel::handleConn() { | ||
if (connHandler_) { | ||
connHandler_(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,131 +1,97 @@ | ||
// @Author Lin Ya | ||
// @Email [email protected] | ||
#pragma once | ||
#include "Timer.h" | ||
#include <string> | ||
#include <unordered_map> | ||
#include <memory> | ||
#include <sys/epoll.h> | ||
#include <functional> | ||
#include <sys/epoll.h> | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
#include "Timer.h" | ||
|
||
class EventLoop; | ||
class HttpData; | ||
|
||
|
||
class Channel | ||
{ | ||
private: | ||
typedef std::function<void()> CallBack; | ||
EventLoop *loop_; | ||
int fd_; | ||
__uint32_t events_; | ||
__uint32_t revents_; | ||
__uint32_t lastEvents_; | ||
|
||
// 方便找到上层持有该Channel的对象 | ||
std::weak_ptr<HttpData> holder_; | ||
|
||
private: | ||
int parse_URI(); | ||
int parse_Headers(); | ||
int analysisRequest(); | ||
|
||
CallBack readHandler_; | ||
CallBack writeHandler_; | ||
CallBack errorHandler_; | ||
CallBack connHandler_; | ||
|
||
public: | ||
Channel(EventLoop *loop); | ||
Channel(EventLoop *loop, int fd); | ||
~Channel(); | ||
int getFd(); | ||
void setFd(int fd); | ||
|
||
void setHolder(std::shared_ptr<HttpData> holder) | ||
{ | ||
holder_ = holder; | ||
} | ||
std::shared_ptr<HttpData> getHolder() | ||
{ | ||
std::shared_ptr<HttpData> ret(holder_.lock()); | ||
return ret; | ||
} | ||
|
||
void setReadHandler(CallBack &&readHandler) | ||
{ | ||
readHandler_ = readHandler; | ||
} | ||
void setWriteHandler(CallBack &&writeHandler) | ||
{ | ||
writeHandler_ = writeHandler; | ||
class Channel { | ||
private: | ||
typedef std::function<void()> CallBack; | ||
EventLoop *loop_; | ||
int fd_; | ||
__uint32_t events_; | ||
__uint32_t revents_; | ||
__uint32_t lastEvents_; | ||
|
||
// 方便找到上层持有该Channel的对象 | ||
std::weak_ptr<HttpData> holder_; | ||
|
||
private: | ||
int parse_URI(); | ||
int parse_Headers(); | ||
int analysisRequest(); | ||
|
||
CallBack readHandler_; | ||
CallBack writeHandler_; | ||
CallBack errorHandler_; | ||
CallBack connHandler_; | ||
|
||
public: | ||
Channel(EventLoop *loop); | ||
Channel(EventLoop *loop, int fd); | ||
~Channel(); | ||
int getFd(); | ||
void setFd(int fd); | ||
|
||
void setHolder(std::shared_ptr<HttpData> holder) { holder_ = holder; } | ||
std::shared_ptr<HttpData> getHolder() { | ||
std::shared_ptr<HttpData> ret(holder_.lock()); | ||
return ret; | ||
} | ||
|
||
void setReadHandler(CallBack &&readHandler) { readHandler_ = readHandler; } | ||
void setWriteHandler(CallBack &&writeHandler) { | ||
writeHandler_ = writeHandler; | ||
} | ||
void setErrorHandler(CallBack &&errorHandler) { | ||
errorHandler_ = errorHandler; | ||
} | ||
void setConnHandler(CallBack &&connHandler) { connHandler_ = connHandler; } | ||
|
||
void handleEvents() { | ||
events_ = 0; | ||
if ((revents_ & EPOLLHUP) && !(revents_ & EPOLLIN)) { | ||
events_ = 0; | ||
return; | ||
} | ||
void setErrorHandler(CallBack &&errorHandler) | ||
{ | ||
errorHandler_ = errorHandler; | ||
if (revents_ & EPOLLERR) { | ||
if (errorHandler_) errorHandler_(); | ||
events_ = 0; | ||
return; | ||
} | ||
void setConnHandler(CallBack &&connHandler) | ||
{ | ||
connHandler_ = connHandler; | ||
if (revents_ & (EPOLLIN | EPOLLPRI | EPOLLRDHUP)) { | ||
handleRead(); | ||
} | ||
|
||
void handleEvents() | ||
{ | ||
events_ = 0; | ||
if ((revents_ & EPOLLHUP) && !(revents_ & EPOLLIN)) | ||
{ | ||
events_ = 0; | ||
return; | ||
} | ||
if (revents_ & EPOLLERR) | ||
{ | ||
if (errorHandler_) errorHandler_(); | ||
events_ = 0; | ||
return; | ||
} | ||
if (revents_ & (EPOLLIN | EPOLLPRI | EPOLLRDHUP)) | ||
{ | ||
handleRead(); | ||
} | ||
if (revents_ & EPOLLOUT) | ||
{ | ||
handleWrite(); | ||
} | ||
handleConn(); | ||
if (revents_ & EPOLLOUT) { | ||
handleWrite(); | ||
} | ||
void handleRead(); | ||
void handleWrite(); | ||
void handleError(int fd, int err_num, std::string short_msg); | ||
void handleConn(); | ||
handleConn(); | ||
} | ||
void handleRead(); | ||
void handleWrite(); | ||
void handleError(int fd, int err_num, std::string short_msg); | ||
void handleConn(); | ||
|
||
void setRevents(__uint32_t ev) | ||
{ | ||
revents_ = ev; | ||
} | ||
void setRevents(__uint32_t ev) { revents_ = ev; } | ||
|
||
void setEvents(__uint32_t ev) | ||
{ | ||
events_ = ev; | ||
} | ||
__uint32_t& getEvents() | ||
{ | ||
return events_; | ||
} | ||
void setEvents(__uint32_t ev) { events_ = ev; } | ||
__uint32_t &getEvents() { return events_; } | ||
|
||
bool EqualAndUpdateLastEvents() | ||
{ | ||
bool ret = (lastEvents_ == events_); | ||
lastEvents_ = events_; | ||
return ret; | ||
} | ||
|
||
__uint32_t getLastEvents() | ||
{ | ||
return lastEvents_; | ||
} | ||
bool EqualAndUpdateLastEvents() { | ||
bool ret = (lastEvents_ == events_); | ||
lastEvents_ = events_; | ||
return ret; | ||
} | ||
|
||
__uint32_t getLastEvents() { return lastEvents_; } | ||
}; | ||
|
||
typedef std::shared_ptr<Channel> SP_Channel; |
Oops, something went wrong.