00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "netmessage.h"
00021 #include "netbuffer.h"
00022 #include <netinet/in.h>
00023
00024 using namespace oxygen;
00025 using namespace zeitgeist;
00026 using namespace std;
00027 using namespace boost;
00028
00029 NetMessage::NetMessage() : Leaf()
00030 {
00031 }
00032
00033 NetMessage::~NetMessage()
00034 {
00035 }
00036
00037 void NetMessage::PrepareToSend(std::string& msg)
00038 {
00039
00040 unsigned int len = htonl(msg.size());
00041 string prefix((const char*)&len,sizeof(unsigned int));
00042 msg = prefix + msg;
00043 }
00044
00045 bool NetMessage::Extract(shared_ptr<NetBuffer> buffer, std::string& msg)
00046 {
00047
00048 const unsigned int preSz = sizeof(unsigned int);
00049 string& data = buffer->GetData();
00050
00051 if (data.size() < preSz)
00052 {
00053 return false;
00054 }
00055
00056 unsigned int msgLen = ntohl((*(unsigned int*)data.data()));
00057
00058 if (data.size() < (msgLen + preSz))
00059 {
00060
00061 return false;
00062 }
00063
00064
00065 msg = string(data.c_str() + preSz, msgLen);
00066 data.erase(0, preSz + msgLen);
00067
00068
00069 msg += '\0';
00070
00071 return true;
00072 }