Skip to content

Commit

Permalink
Merge pull request yedf2#1 from yedf/master
Browse files Browse the repository at this point in the history
update from origin
  • Loading branch information
dq5070410 authored Oct 17, 2016
2 parents fe3fb62 + d02574c commit b0db82f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 10m/10m-cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main(int argc, const char* argv[]) {
int begin_port = atoi(argv[c++]);
int end_port = atoi(argv[c++]);
int conn_count = atoi(argv[c++]);
int create_seconds = atoi(argv[c++]);
float create_seconds = atof(argv[c++]);
int processes = atoi(argv[c++]);
conn_count = conn_count / processes;
int heartbeat_interval = atoi(argv[c++]);
Expand Down
3 changes: 2 additions & 1 deletion README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ example can be found examples/hsha.cc
### openssl supported
asynchronously handle the openssl connection
asynchronously handle the openssl connection. if you have installed openssl, then make will automatically download handy-ssl.
ssl support files are in [handy-ssl](https://github.com/yedf/handy-ssl.git) because of license.
###protobuf supported
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ handy[![Build Status](https://travis-ci.org/yedf/handy.png)](https://travis-ci.o
* linux上使用epoll
* MacOSX上使用kqueue
* [性能测试报告](http://www.oschina.net/p/c11-handy)
* [单机千万并发连接](http://www.cnblogs.com/dongfuye/p/4986301.html)
* [单机千万并发连接](https://zhuanlan.zhihu.com/p/21378825)

###简洁

Expand Down Expand Up @@ -50,7 +50,8 @@ int main(int argc, const char* argv[]) {
###openssl支持
异步连接管理,支持openssl连接
异步连接管理,支持openssl连接,如果实现安装了openssl,能够找到<openssl/ssl.h>,项目会自动下载handy-ssl
由于openssl的开源协议与此不兼容,所以项目文件单独放在[handy-ssl](https://github.com/yedf/handy-ssl.git)
###protobuf支持
Expand Down Expand Up @@ -99,6 +100,7 @@ int main(int argc, const char* argv[]) {
* udp-cli.cc udp的客户端
* udp-svr.cc udp服务器
* udp-hsha.cc udp的半同步半异步服务器
license
====
Use of this source code is governed by a BSD-style
Expand Down
3 changes: 2 additions & 1 deletion handy/conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ void TcpConn::onMsg(CodecBase* codec, const MsgCallBack& cb) {
r = con->codec_->tryDecode(con->getInput(), msg);
if (r < 0) {
con->channel_->close();
break;
} else if (r > 0) {
trace("a msg decoded. origin len %d msg len %ld", r, msg.size());
cb(con, msg);
Expand Down Expand Up @@ -370,4 +371,4 @@ void HSHA::onMsg(CodecBase* codec, const RetMsgCallBack& cb) {
});
}

}
}
4 changes: 2 additions & 2 deletions handy/event_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Channel: private noncopyable {
//关闭通道
void close();

//挂接时间处理器
//挂接事件处理器
void onRead(const Task& readcb) { readcb_ = readcb; }
void onWrite(const Task& writecb) { writecb_ = writecb; }
void onRead(Task&& readcb) { readcb_ = std::move(readcb); }
Expand All @@ -97,4 +97,4 @@ struct Channel: private noncopyable {
std::function<void()> readcb_, writecb_, errorcb_;
};

}
}
2 changes: 1 addition & 1 deletion handy/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void Buffer::expand(size_t len) {

void Buffer::copyFrom(const Buffer& b) {
memcpy(this, &b, sizeof b);
if (size()) {
if (b.buf_) {
buf_ = new char[cap_];
memcpy(data(), b.begin(), b.size());
}
Expand Down
13 changes: 11 additions & 2 deletions handy/udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int UdpServer::bind(const std::string &host, short port, bool reusePort) {
return;
}
int fd = channel_->fd();
ssize_t rn = recvfrom(fd, buf.makeRoom(kUdpPacketSize), buf.space(), 0, (sockaddr*)&raddr, &rsz);
ssize_t rn = recvfrom(fd, buf.makeRoom(kUdpPacketSize), kUdpPacketSize, 0, (sockaddr*)&raddr, &rsz);
if (rn < 0) {
error("udp %d recv failed: %d %s", fd, errno, strerror(errno));
return;
Expand Down Expand Up @@ -90,11 +90,12 @@ UdpConnPtr UdpConn::createConnection(EventBase* base, const string& host, short
con->destHost_ = host;
con->destPort_ = port;
con->peer_ = addr;
con->base_ = base;
Channel* ch = new Channel(base, fd, kReadEvent);
con->channel_ = ch;
ch->onRead([con]{
if (!con->channel_ || con->channel_->fd() < 0) {
return;
return con->close();
}
Buffer input;
int fd = con->channel_->fd();
Expand All @@ -110,6 +111,14 @@ UdpConnPtr UdpConn::createConnection(EventBase* base, const string& host, short
return con;
}

void UdpConn::close() {
if(!channel_)
return;
auto p = channel_;
channel_=NULL;
base_->safeCall([p](){ delete p; });
}

void UdpConn::send(const char *buf, size_t len) {
if (!channel_ || channel_->fd() < 0) {
warn("udp sending %lu bytes to %s after channel closed", len, peer_.toString().data());
Expand Down
3 changes: 2 additions & 1 deletion handy/udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ namespace handy {
void send(const std::string& s) { send(s.data(), s.size()); }
void send(const char* s) { send(s, strlen(s)); }
void onMsg(const UdpCallBack& cb) { cb_ = cb; }
void close() { auto p = channel_; channel_=NULL; delete p; }
void close();
//远程地址的字符串
std::string str() { return peer_.toString(); }
public:
void handleRead(const UdpConnPtr&);
EventBase* base_;
Channel* channel_;
Ip4Addr local_, peer_;
Expand Down

0 comments on commit b0db82f

Please sign in to comment.