Skip to content

Commit

Permalink
mac ok
Browse files Browse the repository at this point in the history
  • Loading branch information
yedf committed Jun 17, 2015
1 parent c1e8007 commit de16718
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 154 deletions.
10 changes: 5 additions & 5 deletions build_config
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ EOF
$TMPDIR/handy_build_config.out
PLATFORM_IS_LITTLE_ENDIAN=$?

$CXX -x c++ - -o $TMPDIR/handy_build_config.out >/dev/null 2>&1 <<EOF
#include <sys/epoll.h>
int main() {}
EOF
[ $? = 0 ] && COMMON_FLAGS="$COMMON_FLAGS -DUSE_EPOLL"
#$CXX -x c++ - -o $TMPDIR/handy_build_config.out >/dev/null 2>&1 <<EOF
##include <sys/epoll.h>
#int main() {}
#EOF
#[ $? = 0 ] && COMMON_FLAGS="$COMMON_FLAGS -DUSE_EPOLL"

$CXX -x c++ - -o $TMPDIR/handy_build_config.out >/dev/null 2>&1 <<EOF
#include <openssl/ssl.h>
Expand Down
15 changes: 5 additions & 10 deletions handy/conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,16 @@ int TcpConn::connect(EventBase* base, const string& host, short port, int timeou
if (timeout) {
TcpConnPtr con = shared_from_this();
base->runAfter(timeout, [con] {
if (con->getState() == Handshaking) { con->close(true); }
if (con->getState() == Handshaking) { con->channel_->close(); }
});
}
return 0;
}

void TcpConn::close(bool cleanupNow) {
void TcpConn::close() {
if (channel_) {
channel_->close();
if (cleanupNow) {
channel_->handleRead();
} else {
TcpConnPtr con = shared_from_this();
getBase()->safeCall([con]{ if (con->channel_) con->channel_->handleRead(); });
}
TcpConnPtr con = shared_from_this();
getBase()->safeCall([con]{ if (con->channel_) con->channel_->close(); });
}
}

Expand Down Expand Up @@ -239,7 +234,7 @@ void TcpConn::onMsg(const MsgCallBack& cb) {
Slice msg;
r = con->codec_->tryDecode(con->getInput(), msg);
if (r < 0) {
con->close(true);
con->channel_->close();
} else if (r > 0) {
trace("a msg decoded. origin len %d msg len %ld", r, msg.size());
cb(con, msg);
Expand Down
6 changes: 3 additions & 3 deletions handy/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace handy {
//Tcp连接,使用引用计数
struct TcpConn: public std::enable_shared_from_this<TcpConn> {
//Tcp连接的四个状态
enum State { Invalid, Handshaking, Connected, Closed, Failed, };
enum State { Invalid=1, Handshaking, Connected, Closed, Failed, };
//Tcp构造函数,实际可用的连接应当通过createConnection创建
TcpConn();
virtual ~TcpConn();
Expand Down Expand Up @@ -54,8 +54,8 @@ namespace handy {
//发送消息
void sendMsg(Slice msg);

//cleanupNow指定是否现在清理相关的channel等
void close(bool cleanupNow=false);
//conn会在下个事件周期进行处理
void close();

//远程地址的字符串
std::string str() { return peer_.toString(); }
Expand Down
26 changes: 14 additions & 12 deletions handy/handy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ void EventsImp::updateIdle(const IdleId& id) {
void EventsImp::refreshNearest(const TimerId* tid){
if (timers_.empty()) {
nextTimeout_ = 1 << 30;
} else {
const TimerId &t = timers_.begin()->first;
nextTimeout_ = t.first - util::timeMilli();
nextTimeout_ = nextTimeout_ < 0 ? 0 : nextTimeout_;
}
const TimerId& t = timers_.begin()->first;
nextTimeout_ = t.first - util::timeMilli();
}

void EventsImp::repeatableTimeout(TimerRepeatable* tr) {
Expand Down Expand Up @@ -259,14 +261,12 @@ Channel::Channel(EventBase* base, int fd, int events): base_(base), fd_(fd), eve
fatalif(net::setNonBlock(fd_) < 0, "channel set non block failed");
static atomic<int64_t> id(0);
id_ = ++id;
base_->imp_->poller_->addChannel(this);
poller_ = base_->imp_->poller_;
poller_->addChannel(this);
}

Channel::~Channel() {
base_->imp_->poller_->removeChannel(this);
if (fd_>=0) {
::close(fd_);
}
Channel::~Channel() {
close();
}

void Channel::enableRead(bool enable) {
Expand All @@ -275,7 +275,7 @@ void Channel::enableRead(bool enable) {
} else {
events_ &= ~kReadEvent;
}
base_->imp_->poller_->updateChannel(this);
poller_->updateChannel(this);
}

void Channel::enableWrite(bool enable) {
Expand All @@ -284,7 +284,7 @@ void Channel::enableWrite(bool enable) {
} else {
events_ &= ~kWriteEvent;
}
base_->imp_->poller_->updateChannel(this);
poller_->updateChannel(this);
}

void Channel::enableReadWrite(bool readable, bool writable) {
Expand All @@ -298,14 +298,16 @@ void Channel::enableReadWrite(bool readable, bool writable) {
} else {
events_ &= ~kWriteEvent;
}
base_->imp_->poller_->updateChannel(this);
poller_->updateChannel(this);
}

void Channel::close() {
if (fd_ >= 0) {
if (fd_>=0) {
trace("close channel %ld fd %d", (long)id_, fd_);
poller_->removeChannel(this);
::close(fd_);
fd_ = -1;
handleRead();
}
}

Expand Down
3 changes: 2 additions & 1 deletion handy/handy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "handy-imp.h"
#include "poller.h"

namespace handy {

Expand Down Expand Up @@ -88,8 +89,8 @@ struct Channel {
void handleRead() { readcb_(); }
void handleWrite() { writecb_(); }
protected:
friend EventsImp;
EventBase* base_;
PollerBase* poller_;
int fd_;
short events_;
int64_t id_;
Expand Down
9 changes: 5 additions & 4 deletions handy/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include "port_posix.h"

using namespace std;

Expand Down Expand Up @@ -108,10 +109,10 @@ void Logger::maybeRotate() {
close(fd);
}

static thread_local long tid;
static thread_local uint64_t tid;
void Logger::logv(int level, const char* file, int line, const char* func, const char* fmt ...) {
if (tid == 0) {
tid = syscall(SYS_gettid);
tid = port::gettid();
}
if (level > level_) {
return;
Expand All @@ -127,15 +128,15 @@ void Logger::logv(int level, const char* file, int line, const char* func, const
struct tm t;
localtime_r(&seconds, &t);
p += snprintf(p, limit - p,
"%04d/%02d/%02d-%02d:%02d:%02d.%06d %ld %s %s:%d ",
"%04d/%02d/%02d-%02d:%02d:%02d.%06d %lx %s %s:%d ",
t.tm_year + 1900,
t.tm_mon + 1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec,
static_cast<int>(now_tv.tv_usec),
tid,
(long)tid,
levelStrs_[level],
file,
line);
Expand Down
Loading

0 comments on commit de16718

Please sign in to comment.