Beispiel #1
0
bool Connection::sendMessage(Connection::MessageType type, const google::protobuf::Message& msg, const Message::Deadline& expires, bool prioritized)
{
	size_t bufLimit = prioritized ? SEND_BUF_EMERGENCY : SEND_BUF;
	if (_sndQueue.size() > bufLimit)
		return false;

	auto buf = new Message(expires);
	// Encode
	{
		::google::protobuf::io::StringOutputStream of(&buf->buf);
		::google::protobuf::io::CodedOutputStream stream(&of);
		stream.WriteTag(::google::protobuf::internal::WireFormatLite::MakeTag(type, ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
		stream.WriteVarint32(msg.ByteSize());
		bool encoded = msg.SerializeToCodedStream(&stream);
		BOOST_VERIFY(encoded);
	}
	_sndQueue.enqueue(buf);

	_stats->outgoingMessages += 1;
	_stats->outgoingMessagesCurrent += 1;

	// Push out at once unless _queued;
	if (_sendWaiting == 0)
		trySend();
	return true;
}
Beispiel #2
0
bool Connection::encode(Connection::MessageType type, const google::protobuf::Message &msg) {
    byte* buf = _sendBuf.allocate(MAX_MSG);
    ::google::protobuf::io::ArrayOutputStream of(buf, MAX_MSG);
    ::google::protobuf::io::CodedOutputStream stream(&of);
    stream.WriteTag(::google::protobuf::internal::WireFormatLite::MakeTag(type, ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
    stream.WriteVarint32(msg.ByteSize());
    bool res = msg.SerializeToCodedStream(&stream);
    if (res)
        _sendBuf.charge(stream.ByteCount());
    return res;
}
size_t ProtobufSocketSerializer::serialize(
        const ::google::protobuf::Message &msg, char *&packet) {
    size_t size = PROTOBUF_HEADER_LENGTH + (size_t) msg.ByteSize();
    packet = new char[size]();

    ::google::protobuf::io::ArrayOutputStream aos(packet, (int) size);
    ::google::protobuf::io::CodedOutputStream coded_output(&aos);
    coded_output.WriteLittleEndian32((google::protobuf::uint32) msg.ByteSize());
    msg.SerializeToCodedStream(&coded_output);

    return size;
}