forked from yedf2/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yedongfu
committed
Dec 27, 2014
1 parent
15d8a9c
commit 1c5bc89
Showing
7 changed files
with
71 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ handy.* | |
a* | ||
*.mk | ||
*.pb.* | ||
ssl | ||
openssl-example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters