PageRenderTime 91ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Example/Network/ProtoBuf/ProtoBuf/Codec.cpp

http://iocpframework.googlecode.com/
C++ | 250 lines | 10 code | 36 blank | 204 comment | 0 complexity | 072aa62ed0d59c8e17ad798407c32a51 MD5 | raw file
  1. #include "stdafx.h"
  2. #include "Codec.hpp"
  3. #include <google/protobuf/descriptor.h>
  4. #include "../../../../include/Network/TCP.hpp"
  5. //#include <zlib.h> // adler32
  6. using namespace async;
  7. using namespace iocp;
  8. using namespace network;
  9. namespace async
  10. {
  11. //void ProtobufCodec::FillBuffer(std::string &buf, const google::protobuf::Message& message)
  12. //{
  13. // typedef google::protobuf::int32 int32;
  14. // //assert(buf.size() == 0);
  15. // //const std::string& typeName = message.GetTypeName();
  16. // //int32 nameLen = google::protobuf::implicit_cast<int32>(typeName.size() + 1);
  17. // //size_t pos = 0;
  18. // //std::copy(reinterpret_cast<char *>(&nameLen),
  19. // // reinterpret_cast<char *>(&nameLen) + sizeof(nameLen),
  20. // // buf.begin());
  21. // //pos += sizeof(nameLen);
  22. // //std::copy(typeName.begin(), typeName.end(), (buf + pos).begin());
  23. // //pos += nameLen;
  24. // //buf.data()[nameLen - 1] = '0';
  25. // //// code copied from MessageLite::SerializeToArray() and MessageLite::SerializePartialToArray().
  26. // //GOOGLE_DCHECK(message.IsInitialized()) << InitializationErrorMessage("serialize", message);
  27. // //int byte_size = message.ByteSize();
  28. // //buf->ensureWritableBytes(byte_size);
  29. // //uint8_t* start = reinterpret_cast<uint8_t*>(buf->beginWrite());
  30. // //uint8_t* end = message.SerializeWithCachedSizesToArray(start);
  31. // //if (end - start != byte_size)
  32. // //{
  33. // // ByteSizeConsistencyError(byte_size, message.ByteSize(), static_cast<int>(end - start));
  34. // //}
  35. // //buf->hasWritten(byte_size);
  36. // //int32_t checkSum = static_cast<int32_t>(
  37. // // ::adler32(1,
  38. // // reinterpret_cast<const Bytef*>(buf->peek()),
  39. // // static_cast<int>(buf->readableBytes())));
  40. // //buf->appendInt32(checkSum);
  41. // //assert(buf->readableBytes() == sizeof nameLen + nameLen + byte_size + sizeof checkSum);
  42. // //int32_t len = sockets::hostToNetwork32(static_cast<int32_t>(buf->readableBytes()));
  43. // //buf->prepend(&len, sizeof len);
  44. // buf.resize(kHeaderLen);
  45. // const std::string& typeName = message.GetTypeName();
  46. // int32 nameLen = static_cast<int32>(typeName.size()+1);
  47. // int32 be32 = nameLen;
  48. // buf.append(reinterpret_cast<char*>(&be32), sizeof be32);
  49. // buf.append(typeName.c_str(), nameLen);
  50. // bool succeed = message.AppendToString(&buf);
  51. // if (succeed)
  52. // {
  53. // const char* begin = buf.c_str() + kHeaderLen;
  54. // // CRC32 or adler32 ??
  55. // int32 checkSum = 0;//adler32(1, reinterpret_cast<const Bytef*>(begin), result.size()-kHeaderLen);
  56. // int32 be32 = checkSum;
  57. // buf.append(reinterpret_cast<char*>(&be32), sizeof(be32));
  58. // int32 len = buf.size() - kHeaderLen;
  59. // int32 &totalLen = *(int32 *)(buf.data());
  60. // totalLen = len;
  61. // }
  62. // else
  63. // {
  64. // buf.clear();
  65. // }
  66. //}
  67. ////
  68. //// no more google code after this
  69. ////
  70. ////
  71. //// FIXME: merge with RpcCodec
  72. ////
  73. //namespace
  74. //{
  75. // const std::string kNoErrorStr = "NoError";
  76. // const std::string kInvalidLengthStr = "InvalidLength";
  77. // const std::string kCheckSumErrorStr = "CheckSumError";
  78. // const std::string kInvalidNameLenStr= "InvalidNameLen";
  79. // const std::string kUnknownMessageTypeStr = "UnknownMessageType";
  80. // const std::string kParseErrorStr = "ParseError";
  81. // const std::string kUnknownErrorStr = "UnknownError";
  82. //}
  83. //const std::string& ProtobufCodec::ErrorCodeToString(ErrorCode errorCode)
  84. //{
  85. // switch (errorCode)
  86. // {
  87. // case kNoError:
  88. // return kNoErrorStr;
  89. // case kInvalidLength:
  90. // return kInvalidLengthStr;
  91. // case kCheckSumError:
  92. // return kCheckSumErrorStr;
  93. // case kInvalidNameLen:
  94. // return kInvalidNameLenStr;
  95. // case kUnknownMessageType:
  96. // return kUnknownMessageTypeStr;
  97. // case kParseError:
  98. // return kParseErrorStr;
  99. // default:
  100. // return kUnknownErrorStr;
  101. // }
  102. //}
  103. //void ProtobufCodec::_DefaultErrorCallback(network::Tcp::Socket &sock, const iocp::ConstBuffer &buf, ErrorCode code)
  104. //{
  105. // //LOG_ERROR << "ProtobufCodec::defaultErrorCallback - " << errorCodeToString(errorCode);
  106. //
  107. // sock.Close();
  108. //}
  109. //google::protobuf::int32 AsInt32(const char* buf)
  110. //{
  111. // google::protobuf::int32 be32 = 0;
  112. // ::memcpy(&be32, buf, sizeof(be32));
  113. // return be32;
  114. //}
  115. //
  116. //void ProtobufCodec::OnMessage(network::Tcp::Socket &sock, const iocp::ConstBuffer& buf)
  117. //{
  118. // typedef google::protobuf::int32 int32;
  119. //
  120. // size_t total = buf.size();
  121. // size_t readableSize = total;
  122. // while (readableSize >= kMinMessageLen + kHeaderLen)
  123. // {
  124. // const int32 &len = *(int32*)((buf + (total - readableSize)).data());
  125. // if (len > kMaxMessageLen || len < kMinMessageLen)
  126. // {
  127. // errorCallback_(std::tr1::ref(sock), std::tr1::cref(buf), kInvalidLength);
  128. // }
  129. // else if (readableSize >= google::protobuf::implicit_cast<size_t>(len + kHeaderLen))
  130. // {
  131. // ErrorCode errorCode = kNoError;
  132. // MessagePtr message = Parse((buf + (total - readableSize +kHeaderLen)).data(), len, &errorCode);
  133. // if (errorCode == kNoError && message)
  134. // {
  135. // messageCallback_(std::tr1::ref(sock), std::tr1::ref(message));
  136. // readableSize -= (len + kHeaderLen);
  137. // }
  138. // else
  139. // {
  140. // errorCallback_(std::tr1::ref(sock), std::tr1::cref(buf), errorCode);
  141. // }
  142. // }
  143. // else
  144. // {
  145. // break;
  146. // }
  147. // }
  148. //}
  149. //google::protobuf::Message* ProtobufCodec::CreateMessage(const std::string& typeName)
  150. //{
  151. // google::protobuf::Message* message = NULL;
  152. // const google::protobuf::Descriptor* descriptor =
  153. // google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(typeName);
  154. // if (descriptor)
  155. // {
  156. // const google::protobuf::Message* prototype =
  157. // google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor);
  158. // if (prototype)
  159. // {
  160. // message = prototype->New();
  161. // }
  162. // }
  163. // return message;
  164. //}
  165. //MessagePtr ProtobufCodec::Parse(const char* buf, int len, ErrorCode* error)
  166. //{
  167. // MessagePtr message;
  168. // typedef google::protobuf::int32 int32;
  169. // // check sum
  170. // int32 expectedCheckSum = AsInt32(buf + len - kHeaderLen);
  171. // int32 checkSum = static_cast<int32>(0
  172. // /*::adler32(1,
  173. // reinterpret_cast<const Bytef*>(buf),
  174. // static_cast<int>(len - kHeaderLen))*/);
  175. // if (checkSum == expectedCheckSum)
  176. // {
  177. // // get message type name
  178. // int32 nameLen = AsInt32(buf);
  179. // if (nameLen >= 2 && nameLen <= len - 2 * kHeaderLen)
  180. // {
  181. // std::string typeName(buf + kHeaderLen, buf + kHeaderLen + nameLen - 1);
  182. // // create message object
  183. // message.reset(CreateMessage(typeName));
  184. // if (message)
  185. // {
  186. // // parse from buffer
  187. // const char* data = buf + kHeaderLen + nameLen;
  188. // int32 dataLen = len - nameLen - 2 * kHeaderLen;
  189. // if (message->ParseFromArray(data, dataLen))
  190. // {
  191. // *error = kNoError;
  192. // }
  193. // else
  194. // {
  195. // *error = kParseError;
  196. // }
  197. // }
  198. // else
  199. // {
  200. // *error = kUnknownMessageType;
  201. // }
  202. // }
  203. // else
  204. // {
  205. // *error = kInvalidNameLen;
  206. // }
  207. // }
  208. // else
  209. // {
  210. // *error = kCheckSumError;
  211. // }
  212. // return message;
  213. //}
  214. }