Ejemplo n.º 1
0
	void ConnectionHandler::SendProtobufMessage(UInt32 opcode, google::protobuf::Message &msg) {
		// send length
		UInt32Bytes length{ByteOrder::toLittleEndian(static_cast<UInt32>(msg.ByteSize()))};
		socket_.sendBytes(length.bytes, 4);
		// send opcode
		UInt32Bytes opcodeBytes{Poco::ByteOrder::toLittleEndian(opcode)};
		socket_.sendBytes(opcodeBytes.bytes, 4);
		// send data
		socket_.sendBytes(msg.SerializeAsString().data(), msg.ByteSize());
	}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
int LinkImpl::serialize(const google::protobuf::Message &message)
{
    auto sz = message.ByteSize();
    if(!message.SerializeToArray(data,sz))
        throw std::runtime_error("Message failed to serialize (probably too big)");
    return sz;
}
Ejemplo n.º 5
0
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);

}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
0
bool sendToClinet(const PkgHead& pkg_head, const google::protobuf::Message& message)
{
	char buff[MAX_BUFF];

	char *p = buff;

	int32_t pkg_head_size = sizeof(pkg_head);
	int32_t pkg_body_size = message.ByteSize();

	int32_t pkg_total_size = pkg_head_size + pkg_body_size;

	__BEGIN_PROC__

	//消息头
	int32_t *header = (int32_t *)p;
	*header = htonl(pkg_total_size);
	p += sizeof(*header);

	int32_t send_len = sizeof(*header) + pkg_total_size;

	if(send_len > MAX_BUFF)
	{
		LOG_ERROR(0, "there is no enough buff. buff size:%d, real size:%d", MAX_BUFF, send_len);
		break;
	}


	//协议包头
	PkgHead *p_pkg_head = (PkgHead *)p;
	*p_pkg_head = pkg_head;
	p_pkg_head->body_len = pkg_body_size;
	p_pkg_head->pack();
	p += sizeof(PkgHead);

	//协议包体
	if (!message.SerializeToArray(p, pkg_body_size))
	{
		LOG_ERROR(0, "message.SerializeToArray failed");
		break;
	}

	int32_t ret = 0;

	ret = skynet_socket_send(0, pkg_head.client_fd, buff, send_len);

	if(-1 == ret)
	{
		LOG_ERROR(0, "skynet_socket_send failed");
		break;
	}

	LOG_DEBUG(0, "send_len:%d  pkg_body_len:%d", send_len, pkg_body_size);

	return true;

	__END_PROC__


	return false;
}
void GridClientProtobufMarshaller::marshalMsg(const ::google::protobuf::Message& msg, int8_t*& pBuffer,
    unsigned long& bufferLength) {
    bufferLength = msg.ByteSize();

    pBuffer = new int8_t[bufferLength];

    msg.SerializeToArray(pBuffer, bufferLength);
}
Ejemplo n.º 9
0
/**
  * Writes a given protobuff message to the buffer (this->buffer) prefixed by a header.
  * @return the total size (header size + message size). Return 0 if the total size is bigger than 'Protos.Core.Settings.max_udp_datagram_size'.
  */
int UDPListener::writeMessageToBuffer(Common::MessageHeader::MessageType type, const google::protobuf::Message& message)
{
   const Common::MessageHeader header(type, message.ByteSize(), this->getOwnID());

   const int nbBytesWritten = Common::Message::writeMessageToBuffer(this->buffer, this->MAX_UDP_DATAGRAM_PAYLOAD_SIZE, header, &message);
   if (!nbBytesWritten)
      L_ERRO(QString("Datagram size too big: %1, max allowed: %2").arg(Common::MessageHeader::HEADER_SIZE + header.getSize()).arg(this->MAX_UDP_DATAGRAM_PAYLOAD_SIZE));

   return nbBytesWritten;
}
Ejemplo n.º 10
0
void Connection::messageToNetwork(const ::google::protobuf::Message &msg, unsigned int msgType, QByteArray &cache) {
	int len = msg.ByteSize();
	if (len > 0x7fffff)
		return;
	cache.resize(len + 6);
	unsigned char *uc = reinterpret_cast<unsigned char *>(cache.data());
	qToBigEndian<quint16>(msgType, & uc[0]);
	qToBigEndian<quint32>(len, & uc[2]);

	msg.SerializeToArray(uc + 6, len);
}
Ejemplo n.º 11
0
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;
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
0
bool WriteMessage(const shared_ptr<UnixSocketConnection> &connection,
                  uint32_t type,
                  const google::protobuf::Message &response) {
  shared_ptr<vector<char> > data(new vector<char>(response.ByteSize() + sizeof(type)));
  char *ptr = &(*data)[0];
  *reinterpret_cast<uint32_t *>(ptr) = type;
  if (!response.SerializeToArray(ptr + sizeof(type), data->size() - sizeof(type))) {
    WARNING_LOG("serialize message to string failed.");
    return false;
  }
  connection->Write(data);
  return true;
}
Ejemplo n.º 14
0
int UDPListener::writeMessageToBuffer(Common::MessageHeader::MessageType type, const google::protobuf::Message& message)
{
   const int bodySize = message.ByteSize();
   Common::MessageHeader header(type, bodySize, this->peerManager->getID());

   if (Common::MessageHeader::HEADER_SIZE + bodySize > static_cast<int>(SETTINGS.get<quint32>("max_udp_datagram_size")))
   {
      L_ERRO(QString("Datagram size too big : %1").arg(Common::MessageHeader::HEADER_SIZE + bodySize));
      return 0;
   }

   Common::MessageHeader::writeHeader(this->buffer, header);
   message.SerializeToArray(this->bodyBuffer, BUFFER_SIZE - Common::MessageHeader::HEADER_SIZE);

   return Common::MessageHeader::HEADER_SIZE + bodySize;
}
void NetworkClient::Send(int packetID, const google::protobuf::Message& msg)
{
	int msgSize = msg.ByteSize();
	int bufferSize = Packet::Header::Size + msgSize;

	Packet::Header header;
	header.packetID = packetID;
	header.length = msgSize;

	auto buffer = std::unique_ptr<char[]>(new char[bufferSize]);
	header.Serialize(buffer.get());

	msg.SerializeToArray(buffer.get() + Packet::Header::Size, msgSize);

	conn->Send(bufferSize, buffer.get());
}
Ejemplo n.º 16
0
void CGroupManagerClient::SendMessageToClient(const ::google::protobuf::Message &msg, s32 msgid)
{
    int count = msg.ByteSize();
    byte buf[MAX_MSG_LEN] = {0};
    *((s32*)buf) = msgid;
    if( msg.SerializeToArray(buf+MSG_ID_LEN, MAX_MSG_LEN) )
    {
        if( !SendPkg( buf, count+MSG_ID_LEN ) )
        {
            SERVER_LOG_ERROR( "CGroupManagerClient,SendMessageToGroupServer,SendPkg" );
        }
    }
    else
    {
        SERVER_LOG_ERROR( "CGroupManagerClient,SendMessageToGroupServer,SerializeToArray" );
    }
}
Ejemplo n.º 17
0
void mumlib::Transport::sendControlMessagePrivate(MessageType type, google::protobuf::Message &message) {


    const uint16_t type_network = htons(static_cast<uint16_t>(type));

    const int size = message.ByteSize();
    const uint32_t size_network = htonl(size);

    const int length = sizeof(type_network) + sizeof(size_network) + size;

    uint8_t buff[MAX_TCP_LENGTH];

    memcpy(buff, &type_network, sizeof(type_network));

    memcpy(buff + sizeof(type_network), &size_network, sizeof(size_network));

    message.SerializeToArray(buff + sizeof(type_network) + sizeof(size_network), size);

    sendSsl(buff, length);
}
Ejemplo n.º 18
0
///
/// \brief ·¢ËÍÊý¾Ý
///
/// \author albert.xu
/// \date 2017/08/05
///
xgc_void CClientSession::Send( xgc_uint16 msgid, ::google::protobuf::Message& msg )
{
#pragma pack(1)
	struct
	{
		MessageHeader h;
		char b[1024 * 4];
	} m;
#pragma pack()

	xgc_uint16 data_size = msg.ByteSize();
	xgc_uint16 pack_size = sizeof( MessageHeader ) + data_size;

	m.h.length = htons( pack_size );
	m.h.message = htons( msgid );

	msg.SerializeToArray( m.b, sizeof( m.b ) );

	SendPacket( handle_, &m, pack_size );
}
Ejemplo n.º 19
0
// Write out the given protobuf to the specified file descriptor by
// first writing out the length of the protobuf followed by the contents.
// NOTE: On error, this may have written partial data to the file.
inline Try<Nothing> write(int fd, const google::protobuf::Message& message)
{
  if (!message.IsInitialized()) {
    return Error("Uninitialized protocol buffer");
  }

  // First write the size of the protobuf.
  uint32_t size = message.ByteSize();
  std::string bytes = std::string((char*) &size, sizeof(size));

  Try<Nothing> result = os::write(fd, bytes);
  if (result.isError()) {
    return Error("Failed to write size: " + result.error());
  }

  if (!message.SerializeToFileDescriptor(fd)) {
    return Error("Failed to write/serialize message");
  }

  return Nothing();
}
Ejemplo n.º 20
0
int CAuth::Send2Server(XYHeaderIn& Header, unsigned int DstID, char SendType, char Flag, const google::protobuf::Message& Message)
{
    BusHeader CurBusHeader;
    int HeaderLen = CurBusHeader.GetHeaderLen() + Header.GetHeaderLen();
    CurBusHeader.PkgLen = HeaderLen + Message.ByteSize();
    CurBusHeader.CmdID = Cmd_Transfer;
    CurBusHeader.SrcID = GetServerID();
    CurBusHeader.DstID = DstID;
    CurBusHeader.SendType = SendType;
    CurBusHeader.Flag = Flag;
    CurBusHeader.Write(m_pSendBuff);

    Header.Write(m_pSendBuff+CurBusHeader.GetHeaderLen());

    if(!Message.SerializeToArray(m_pSendBuff+HeaderLen, XY_PKG_MAX_LEN-HeaderLen))
    {
        XF_LOG_WARN(0, 0, "pack err msg failed");
        return -1;
    }

    int Ret = m_SendQueue.InQueue(m_pSendBuff, CurBusHeader.PkgLen);
    if(Ret == m_SendQueue.E_SHM_QUEUE_FULL)
    {
        XF_LOG_WARN(0, 0, "m_SendQueue InQueue failed, queue full");
        return -1;
    }
    else if (Ret != 0)
    {
        XF_LOG_WARN(0, 0, "m_SendQueue InQueue failed, Ret=%d", Ret);
        return -1;
    }
    else
    {
        XF_LOG_TRACE(0, 0, "m_SendQueue InQueue Success,[%s]", CStrTool::Str2Hex(m_pSendBuff, CurBusHeader.PkgLen));
    }
    
    return 0;
}
Ejemplo n.º 21
0
// Write out the given protobuf to the specified file descriptor by
// first writing out the length of the protobuf followed by the
// contents.
inline Result<bool> write(int fd, const google::protobuf::Message& message)
{
  if (!message.IsInitialized()) {
    LOG(ERROR) << "Failed to write protocol buffer to file, "
               << "protocol buffer is not initialized!";
    return false;
  }

  uint32_t size = message.ByteSize();

  ssize_t length = ::write(fd, (void*) &size, sizeof(size));

  if (length == -1) {
    std::string error = strerror(errno);
    error = error + " (" + __FILE__ + ":" + utils::stringify(__LINE__) + ")";
    return Result<bool>::error(error);
  }

  CHECK(length != 0);
  CHECK(length == sizeof(size)); // TODO(benh): Handle a non-blocking fd?

  return message.SerializeToFileDescriptor(fd);
}
Ejemplo n.º 22
0
/**
This function is used to receive and deserialize a protobuf object from a ZMQ socket.
@param inputSocketToReceiveFrom: This is the socket to receive the object from
@param inputMessageBuffer: This is the buffer to place the received object in
@param inputFlags: The flags to pass to the ZMQ socket
@param inputPreappendedDataBuffer: The buffer to place x bytes before the message in
@param inputPreappendedDataSize: How much data to expect to be preappended
@param inputPostappendedDataBuffer: The buffer to place x bytes before the message in
@param inputPostappendedDataSize: How much data to expect to be postappended
@return: <true if message received, true if message deserialized correctly>

@throws: This function can throw exceptions
*/
std::tuple<bool, bool> pylongps::receiveProtobufMessage(zmq::socket_t &inputSocketToReceiveFrom, google::protobuf::Message &inputMessageBuffer, int inputFlags, char *inputPreappendedDataBuffer, int inputPreappendedDataSize, char *inputPostappendedDataBuffer, int inputPostappendedDataSize)
{
if(inputPreappendedDataSize < 0 || inputPostappendedDataSize < 0)
{
throw SOMException("Negative data size\n", INVALID_FUNCTION_INPUT, __FILE__, __LINE__);
}

if((inputPreappendedDataSize > 0 && inputPreappendedDataBuffer == nullptr) || (inputPostappendedDataSize < 0 && inputPostappendedDataBuffer == nullptr))
{
throw SOMException("Data size > 0 but buffer is nullptr\n", INVALID_FUNCTION_INPUT, __FILE__, __LINE__);
}

bool messageReceived = false;
bool messageDeserialized = false;

std::unique_ptr<zmq::message_t> messageBuffer;

SOM_TRY
messageBuffer.reset(new zmq::message_t);
SOM_CATCH("Error initializing ZMQ message")

SOM_TRY
messageReceived = inputSocketToReceiveFrom.recv(messageBuffer.get(), inputFlags);
SOM_CATCH("Error, unable to receive message\n")

if(messageReceived == false)
{ //Didn't get a message
return std::tuple<bool, bool>(messageReceived, messageDeserialized);
}

//Attempt to retrieve any preappended data
if(messageBuffer->size() < inputPreappendedDataSize)
{ //Message isn't valid (smaller than expected preappended data)
return std::tuple<bool, bool>(messageReceived, messageDeserialized);
}
else if(inputPreappendedDataSize > 0)
{ //Preappended data is expected and the message is large enough to get it
memcpy((void *) inputPreappendedDataBuffer, messageBuffer->data(), inputPreappendedDataSize);
}

//Deserialize message
inputMessageBuffer.ParseFromArray(((char *) messageBuffer->data())+inputPreappendedDataSize, messageBuffer->size()-inputPreappendedDataSize);

if(!inputMessageBuffer.IsInitialized())
{
return std::tuple<bool, bool>(messageReceived, messageDeserialized);
}

if(inputPostappendedDataSize > 0)
{
int protobufMessageSize = inputMessageBuffer.ByteSize();

if((protobufMessageSize+inputPreappendedDataSize+inputPostappendedDataSize) > messageBuffer->size())
{
return std::tuple<bool, bool>(messageReceived, messageDeserialized);
}
else
{
memcpy((void *) inputPostappendedDataBuffer, (void *) (((char *) messageBuffer->data()) + protobufMessageSize + inputPreappendedDataSize), inputPostappendedDataSize);
messageDeserialized = true;
}

}
else
{
messageDeserialized = true;
}

return std::tuple<bool, bool>(messageReceived, messageDeserialized);
}