void Client::InternalSendMessage(const google::protobuf::Message& _message) { if (state_ != CONNECTED) { LOG(ERROR) << "Client is not connected. Dropping message" << std::endl; return; } // Generate a zero rid. REQID rid = REQID_Generator::generate_zero_reqid(); // Make the outgoing packet sp_int32 byte_size = _message.ByteSize(); sp_uint32 sop = OutgoingPacket::SizeRequiredToPackString(_message.GetTypeName()) + REQID_size + OutgoingPacket::SizeRequiredToPackProtocolBuffer(byte_size); auto opkt = new OutgoingPacket(sop); CHECK_EQ(opkt->PackString(_message.GetTypeName()), 0); CHECK_EQ(opkt->PackREQID(rid), 0); CHECK_EQ(opkt->PackProtocolBuffer(_message, byte_size), 0); Connection* conn = static_cast<Connection*>(conn_); if (conn->sendPacket(opkt, NULL) != 0) { LOG(ERROR) << "Some problem sending message thru the connection. Dropping message" << std::endl; delete opkt; return; } return; }
void Client::SendResponse(REQID _id, const google::protobuf::Message& _response) { sp_int32 byte_size = _response.ByteSize(); sp_uint32 data_size = OutgoingPacket::SizeRequiredToPackString(_response.GetTypeName()) + REQID_size + OutgoingPacket::SizeRequiredToPackProtocolBuffer(byte_size); auto opkt = new OutgoingPacket(data_size); CHECK_EQ(opkt->PackString(_response.GetTypeName()), 0); CHECK_EQ(opkt->PackREQID(_id), 0); CHECK_EQ(opkt->PackProtocolBuffer(_response, byte_size), 0); InternalSendResponse(opkt); return; }
inline void post(const process::UPID& to, const google::protobuf::Message& message) { std::string data; message.SerializeToString(&data); post(to, message.GetTypeName(), data.data(), data.size()); }
void send(const process::UPID& to, const google::protobuf::Message& message) { std::string data; message.SerializeToString(&data); process::Process<T>::send(to, message.GetTypeName(), std::move(data)); }
void ProtobufCodec::fillEmptyBuffer(netlib::Buffer *buf,const google::protobuf::Message &message) { assert(buf->readableSize() == 0); const std::string &typeName = message.GetTypeName(); //类型名字长度包含'/0' int32_t nameLen = static_cast<int32_t>(typeName.size() + 1); //消息长度 int byte_size = message.ByteSize(); buf->makeSureEnough(byte_size); //总长度 int32_t len = nameLen + kHeaderLen + byte_size; buf->appendInt32(len); //将名字长度和名字加入到buffer中去 buf->appendInt32(nameLen); buf->append(typeName.c_str(),nameLen); uint8_t *start = reinterpret_cast<uint8_t *>(buf->getWritePeek()); //将消息从start位置开始序列化 uint8_t *end = message.SerializeWithCachedSizesToArray(start); buf->moveWriteIndex(byte_size); assert(end - start == byte_size); }
std::string CTcpCode::Encode(const google::protobuf::Message& message) { std::string result; result.resize(PKGHEAD_FIELD_SIZE); // 预留消息头的前四个字节 // 添加消息类型长度和消息类型具体内容 const std::string& type_name = message.GetTypeName(); int name_len = static_cast<int>(type_name.size()+1); int be32 = ::htonl(name_len); result.append(reinterpret_cast<char*>(&be32), sizeof(be32)); result.append(type_name.c_str(), name_len); // 将protobuf message协议数据添加至result中 bool succeed = message.AppendToString(&result); if(succeed) { // 计算消息体总长度值,并添加至消息头顶部 int len = ::htonl(result.size()); std::copy(reinterpret_cast<char*>(&len), reinterpret_cast<char*>(&len) + sizeof(len), result.begin()); DLOG(INFO) << "encode message ok, name=" << type_name.c_str() << ", len=" << len; } else { LOG(ERROR) << "encode message error, name=" << type_name.c_str(); result.clear(); } return result; }
static void FillBuffer(std::string &buf, const google::protobuf::Message& message) { typedef google::protobuf::int32 int32; const std::string& typeName = message.GetTypeName(); int32 nameLen = static_cast<int32>(typeName.size()+1); int32 be32 = nameLen; buf.resize(sizeof(int32) * 2 + nameLen); buf.append(reinterpret_cast<char*>(&be32), sizeof be32); buf.append(typeName.c_str(), nameLen); bool succeed = message.AppendToString(&buf); if (succeed) { const char* begin = buf.c_str() + kHeaderLen; // CRC32 or adler32 ?? int32 checkSum = 0;//adler32(1, reinterpret_cast<const Bytef*>(begin), result.size()-kHeaderLen); int32 be32 = checkSum; buf.append(reinterpret_cast<char*>(&be32), sizeof(be32)); int32 len = buf.size() - kHeaderLen; int32 &totalLen = *(int32 *)(buf.data()); totalLen = len; } else { buf.clear(); } }
static T evolve(const google::protobuf::Message& message) { T t; string data; // NOTE: We need to use 'SerializePartialToString' instead of // 'SerializeToString' because some required fields might not be set // and we don't want an exception to get thrown. CHECK(message.SerializePartialToString(&data)) << "Failed to serialize " << message.GetTypeName() << " while evolving to " << t.GetTypeName(); // NOTE: We need to use 'ParsePartialFromString' instead of // 'ParsePartialFromString' because some required fields might not // be set and we don't want an exception to get thrown. CHECK(t.ParsePartialFromString(data)) << "Failed to parse " << t.GetTypeName() << " while evolving from " << message.GetTypeName(); return t; }
void parseMessageToFILE(const google::protobuf::Message& message, FILE* output, int tabcount = 0) { using google::protobuf::FieldDescriptor; using google::protobuf::UnknownField; using google::protobuf::UnknownFieldSet; using google::protobuf::EnumValueDescriptor; auto* reflection = message.GetReflection(); auto* descriptor = message.GetDescriptor(); size_t expected_size = descriptor->field_count(); std::vector< const FieldDescriptor* > ref_fields; // array of known fields in the message UnknownFieldSet unknown_fields; ref_fields.reserve(expected_size); for (int i=0, n=descriptor->field_count(); i<n; ++i) { auto* field_desc = descriptor->field(i); ref_fields.push_back(field_desc); } fprintf(output, "%s\n", /*std::string(tabcount,' ').c_str(),*/ message.GetTypeName().c_str()); ++tabcount; for(const auto& ref : ref_fields) { switch(ref->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32 : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<google::protobuf::int32>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetInt32(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_INT64 : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<google::protobuf::int64>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetInt64(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_UINT32 : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<google::protobuf::uint32>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetUInt32(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_UINT64 : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<google::protobuf::uint64>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetUInt64(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_DOUBLE : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<double>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetDouble(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_FLOAT : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<float>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetFloat(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_BOOL : { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedField<bool>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetBool(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_ENUM : { // Enums are a little akward... if (ref->is_repeated()) { for (int i=0, n=reflection->FieldSize(message, ref); i<n; ++i) { printTypeArray(*reflection->GetEnum(message, ref), ref->camelcase_name(), output, tabcount, i); } }else { printType(*reflection->GetEnum(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_STRING : { //TODO: Output binary as HEX numbers. // can't print bytes if (ref->type() == FieldDescriptor::TYPE_BYTES) { fprintf(output, "%s%s=<BINARY DATA>\n", std::string(tabcount,' ').c_str(), ref->camelcase_name().c_str()); break; } if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedPtrField<std::string>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetString(message, ref), ref->camelcase_name(), output, tabcount); } } break; case FieldDescriptor::CPPTYPE_MESSAGE: { if (ref->is_repeated()) { printRepeatedType(reflection->GetRepeatedPtrField<google::protobuf::Message>(message, ref), ref->camelcase_name(), output, tabcount); }else { printType(reflection->GetMessage(message, ref), ref->camelcase_name(), output, tabcount); } } break; } } }