Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
yedongfu committed Dec 27, 2014
1 parent 15d8a9c commit 1c5bc89
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ handy.*
a*
*.mk
*.pb.*
ssl
openssl-example
2 changes: 1 addition & 1 deletion handy/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../config.mk

SOURCES=conf.cc daemon.cc file.cc handy.cc http.cc logging.cc net.cc stat-svr.cc \
threads.cc util.cc codec.cc conn.cc
threads.cc util.cc codec.cc conn.cc handy-fw.cc

OBJECTS = $(SOURCES:.cc=.o)

Expand Down
85 changes: 47 additions & 38 deletions handy/codec.cc
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
#include "codec.h"

using namespace std;

namespace handy {

int LineCodec::tryDecode(Slice data, Slice& msg) {
for (size_t i = 0; i < data.size()-1; i ++) {
if (data[i] == '\r' && data[i+1] == '\n') {
msg = Slice(data.data(), i);
return i+2;
}
}
return 0;
}
void LineCodec::encode(Slice msg, Buffer& buf) {
buf.append(msg).append("\r\n");
}

int LengthCodec::tryDecode(Slice data, Slice& msg) {
if (data.size() < 8) {
return 0;
}
int len = net::ntoh(*(int32_t*)(data.data()+4));
if (len > 1024*1024 || memcmp(data.data(), "mBdT", 4) != 0) {
return -1;
}
if ((int)data.size() >= len+8) {
msg = Slice(data.data()+8, len);
return len+8;
}
return 0;
}
void LengthCodec::encode(Slice msg, Buffer& buf) {
buf.append("mBdT").appendValue(net::hton((int32_t)msg.size())).append(msg);
}


#include "codec.h"

using namespace std;

namespace handy {

int LineCodec::tryDecode(Slice data, Slice& msg) {
if (data.size() == 1 && data[0] == 0x04) {
msg = data;
return 1;
}
for (size_t i = 0; i < data.size(); i ++) {
if (data[i] == '\n') {
if (i > 0 && data[i-1] == '\r') {
msg = Slice(data.data(), i-1);
return i+1;
} else {
msg = Slice(data.data(), i);
return i+1;
}
}
}
return 0;
}
void LineCodec::encode(Slice msg, Buffer& buf) {
buf.append(msg).append("\r\n");
}

int LengthCodec::tryDecode(Slice data, Slice& msg) {
if (data.size() < 8) {
return 0;
}
int len = net::ntoh(*(int32_t*)(data.data()+4));
if (len > 1024*1024 || memcmp(data.data(), "mBdT", 4) != 0) {
return -1;
}
if ((int)data.size() >= len+8) {
msg = Slice(data.data()+8, len);
return len+8;
}
return 0;
}
void LengthCodec::encode(Slice msg, Buffer& buf) {
buf.append("mBdT").appendValue(net::hton((int32_t)msg.size())).append(msg);
}


}
18 changes: 11 additions & 7 deletions handy/conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,17 @@ void TcpConn::send(const char* buf, size_t len) {

void TcpConn::onMsg(const MsgCallBack& cb) {
onRead([cb](const TcpConnPtr& con) {
Slice msg;
int r = con->codec_->tryDecode(con->getInput(), msg);
if (r < 0) {
con->close(true);
} else if (r > 0) {
cb(con, msg);
con->getInput().consume(r);
int r = 1;
while (r) {
Slice msg;
r = con->codec_->tryDecode(con->getInput(), msg);
if (r < 0) {
con->close(true);
} else if (r > 0) {
trace("a msg decoded. origin len %d msg len %ld", r, msg.size());
cb(con, msg);
con->getInput().consume(r);
}
}
});
}
Expand Down
7 changes: 5 additions & 2 deletions handy/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ int Daemon::daemonStart(const char* pidfile) {
dup2(fd, 0);
dup2(fd, 1);
close(fd);
static ExitCaller del([=] { unlink(pidfile); });
string pfile = pidfile;
static ExitCaller del([=] {
unlink(pfile.c_str());
});
return 0;
}
return -1;
Expand Down Expand Up @@ -150,7 +153,7 @@ int Daemon::daemonRestart(const char* pidfile) {

void Daemon::daemonProcess(const char* cmd, const char* pidfile) {
int r = 0;
if (strcmp(cmd, "start")==0) {
if (cmd == NULL || strcmp(cmd, "start")==0) {
r = daemonStart(pidfile);
} else if (strcmp(cmd, "stop")==0) {
r = daemonStop(pidfile);
Expand Down
5 changes: 4 additions & 1 deletion handy/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
#define error(...) hlog(Logger::LERROR, __VA_ARGS__)
#define fatal(...) hlog(Logger::LFATAL, __VA_ARGS__)
#define fatalif(b, ...) do { if((b)) { hlog(Logger::LFATAL, __VA_ARGS__); } } while (0)
#define check(b, ...) do { if((b)) { hlog(Logger::LFATAL, ""__VA_ARGS__); } } while (0) //¼ì²éÂß¼­´íÎó
#define check(b, ...) do { if((b)) { hlog(Logger::LFATAL, __VA_ARGS__); } } while (0) //¼ì²éÂß¼­´íÎó
#define exitif(b, ...) do { if ((b)) { hlog(Logger::LERROR, __VA_ARGS__); exit(1); }} while(0)

#define setloglevel(l) Logger::getLogger().setLogLevel(l)
#define setlogfile(n) Logger::getLogger().setFileName(n)

namespace handy {

Expand Down
1 change: 1 addition & 0 deletions handy/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct util {
static int64_t steadyMilli() { return steadyMicro()/1000; }
static std::string readableTime(time_t t);
static int64_t atoi(const char* b, const char* e) { return strtol(b, (char**)&e, 10); }
static int64_t atoi2(const char* b, const char* e) { char** ne = (char**)&e; int64_t v = strtol(b, ne, 10); return ne == &e ? v : -1; }
static int64_t atoi(const char* b) { return atoi(b, b+strlen(b)); }
};

Expand Down

0 comments on commit 1c5bc89

Please sign in to comment.