Skip to content

Commit

Permalink
🐛 fix write bug
Browse files Browse the repository at this point in the history
  • Loading branch information
markparticle committed Jun 29, 2020
1 parent c3dba7a commit dc531e8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion code/buffer/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ssize_t Buffer::ReadFd(int fd, int* saveErrno) {
iov[1].iov_base = buff;
iov[1].iov_len = sizeof(buff);

const ssize_t len = readv(fd, iov, 2);
const int len = readv(fd, iov, 2);
if(len < 0) {
*saveErrno = errno;
}
Expand Down
11 changes: 6 additions & 5 deletions code/http/httpconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ int HttpConn::GetPort() const {
return addr_.sin_port;
}

size_t HttpConn::read(int* saveErrno) {
size_t len = -1;
ssize_t HttpConn::read(int* saveErrno) {
ssize_t len = -1;
do {
len = readBuff_.ReadFd(fd_, saveErrno);
if (len <= 0) {
Expand All @@ -73,16 +73,16 @@ size_t HttpConn::read(int* saveErrno) {
return len;
}

size_t HttpConn::write(int* saveErrno) {
int len = -1;
ssize_t HttpConn::write(int* saveErrno) {
ssize_t len = -1;
do {
len = writev(fd_, iov_, iovCnt_);
if(len <= 0) {
*saveErrno = errno;
break;
}
if(iov_[0].iov_len + iov_[1].iov_len == 0) { break; } /* 传输结束 */
else if(len > (int)iov_[0].iov_len) {
else if(static_cast<size_t>(len) > iov_[0].iov_len) {
iov_[1].iov_base = (uint8_t*) iov_[1].iov_base + (len - iov_[0].iov_len);
iov_[1].iov_len -= (len - iov_[0].iov_len);
if(iov_[0].iov_len) {
Expand All @@ -101,6 +101,7 @@ size_t HttpConn::write(int* saveErrno) {

void HttpConn::process() {
if(request_.parse(readBuff_)) {
LOG_DEBUG("%s", request_.path().c_str());
response_.Init(srcDir, request_.path(), request_.IsKeepAlive(), 200);
} else {
response_.Init(srcDir, request_.path(), false, 400);
Expand Down
4 changes: 2 additions & 2 deletions code/http/httpconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class HttpConn {

void init(int sockFd, const sockaddr_in& addr);
void reset();
size_t read(int* saveErrno);
size_t write(int* saveErrno);
ssize_t read(int* saveErrno);
ssize_t write(int* saveErrno);

void Close();

Expand Down
4 changes: 2 additions & 2 deletions code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ int main() {
/* 守护进程 后台运行 */
//daemon(1, 0);
WebServer server(
1315, 3, true, false, 5000, /* 端口 ET模式 Proactor/Reactor(使用异步线程池) 优雅退出 timeoutMs */
1316, 3, 5000, true, false, /* 端口 ET模式 timeoutMs Proactor/Reactor(使用异步线程池) 优雅退出 */
3306, "root", "root", "webserver", /* Mysql配置 */
1000, 8, true, 2, 0); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */
100, 8, true, 0, 0); /* 连接池数量 线程池数量 日志开关 日志等级 日志异步队列容量 */
server.Start();
}
20 changes: 10 additions & 10 deletions code/server/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
using namespace std;

WebServer::WebServer(
int port, int trigMode, bool isReactor, bool OptLinger, int timeoutMS,
int port, int trigMode, int timeoutMS, bool isReactor, bool OptLinger,
int sqlPort, const char* sqlUser, const char* sqlPwd,
const char* dbName, int connPoolNum, int threadNum,
bool openLog, int logLevel, int logQueSize):
port_(port), openLinger_(OptLinger), isReactor_(isReactor),
isClose_(false), timeoutMS_(timeoutMS), timer_(new HeapTimer()),
timeoutMS_(timeoutMS), isClose_(false), timer_(new HeapTimer()),
threadpool_(new ThreadPool(threadNum)), epoller_(new Epoller()) {

srcDir_ = getcwd(nullptr, 256);
Expand Down Expand Up @@ -53,6 +53,7 @@ WebServer::~WebServer() {
close(listenFd_);
isClose_ = true;
free(srcDir_);
SqlConnPool::Instance()->ClosePool();
}

void WebServer::InitEventMode_(int trigMode) {
Expand Down Expand Up @@ -81,10 +82,12 @@ void WebServer::InitEventMode_(int trigMode) {
}

void WebServer::Start() {
int timeMS = 0;
int timeMS = -1;
if(!isClose_) { LOG_INFO("========== Server start =========="); }
while(!isClose_) {
if(timeoutMS_ > 0) { timeMS = timer_->GetNextTick(); }
if(timeoutMS_ > 0) {
timeMS = timer_->GetNextTick();
}
int eventCnt = epoller_->Wait(timeMS);
for(int i = 0; i < eventCnt; i++) {
/* 处理事件 */
Expand Down Expand Up @@ -176,18 +179,15 @@ void WebServer::DealWrite_(HttpConn* client) {

void WebServer::ExtentTime_(HttpConn* client) {
assert(client);
if(timeoutMS_ > 0) {
timer_->adjust(client->GetFd(), timeoutMS_);
}
if(timeoutMS_ > 0) { timer_->adjust(client->GetFd(), timeoutMS_); }
}

void WebServer::OnRead_(HttpConn* client) {
assert(client);
int ret = -1;
int readErrno = 0;
ret = client->read(&readErrno);

if(ret <= 0 && !(readErrno & EAGAIN) && !(readErrno & EWOULDBLOCK)) {
if(ret <= 0 && readErrno != EAGAIN) {
CloseConn_(client);
return;
}
Expand All @@ -210,7 +210,7 @@ void WebServer::OnWrite_(HttpConn* client) {
}
}
else if(ret < 0) {
if(writeErrno & EAGAIN) {
if(writeErrno == EAGAIN) {
/* 继续传输 */
epoller_->ModFd(client->GetFd(), connEvent_ | EPOLLOUT);
return;
Expand Down
5 changes: 3 additions & 2 deletions code/server/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class WebServer {
public:
WebServer(
int port, int trigMode, bool isReactor, bool OptLinger, int timeoutMS_,
int port, int trigMode, int timeoutMS, bool isReactor, bool OptLinger,
int sqlPort, const char* sqlUser, const char* sqlPwd,
const char* dbName, int connPoolNum, int threadNum,
bool openLog, int logLevel, int logQueSize);
Expand All @@ -51,13 +51,14 @@ class WebServer {
void OnWrite_(HttpConn* client);

static const int MAX_FD = 65536;

static int SetFdNonblock(int fd);

int port_;
bool openLinger_;
bool isReactor_;
bool isClose_;
int timeoutMS_; /* 毫秒MS */
bool isClose_;
int listenFd_;
char* srcDir_;

Expand Down

0 comments on commit dc531e8

Please sign in to comment.