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; }
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); }
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); }
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; }
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()); }
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" ); } }
/// /// \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 ); }
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); }
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; }