Skip to content

Commit

Permalink
buffer copy size 0 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yedf committed Jun 25, 2016
1 parent fe3fb62 commit 816ee32
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/udp-svr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ int main(int argc, const char* argv[]) {
exitif(!svr, "start udp server failed");
svr->onMsg([](const UdpServerPtr& p, Buffer buf, Ip4Addr peer) {
info("echo msg: %s to %s", buf.data(), peer.toString().c_str());
p->sendTo(buf, peer);
p->sendTo("", 0, peer);
//p->sendTo(buf, peer);
});
base.loop();
}
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
4 changes: 2 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 @@ -94,7 +94,7 @@ UdpConnPtr UdpConn::createConnection(EventBase* base, const string& host, short
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 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() { if(!channel_) return; auto p = channel_; channel_=NULL; base_->safeCall([p](){ delete p; }); }
//远程地址的字符串
std::string str() { return peer_.toString(); }
public:
void handleRead(const UdpConnPtr&);
EventBase* base_;
Channel* channel_;
Ip4Addr local_, peer_;
Expand Down

0 comments on commit 816ee32

Please sign in to comment.