MumblePacket::MumblePacket(MumblePacketType type, const Message &message) { int msgSize = message.ByteSize(); this->messageData = new char[msgSize]; message.SerializePartialToArray(this->messageData, msgSize); this->packetHeader.packetType = static_cast<short>(type); this->packetHeader.packetLength = message.ByteSize() + 6; //The 6 more bytes are for the header (2 + 4 bytes -> type + size) this->rawData = new char[this->packetHeader.packetLength]; memcpy(this->rawData, static_cast<void *>(&this->packetHeader), 6); //We copy the header. memcpy(this->rawData+6, this->messageData, msgSize); //We copy the message }
void Communication::sendMessage(Socket& socket, const Message& message) { int serialized_size = message.ByteSize(); char *serialized_pb = new char[serialized_size]; message.SerializeToArray(serialized_pb, serialized_size); sendReplyMessage(socket, serialized_pb, serialized_size); delete[] serialized_pb; }
serialized_message serialize_message(Message const &message) { size_t msg_size = message.ByteSize(); size_t size = msg_size + MAX_VARINT_BYTES; std::shared_ptr<uint8> buffer(new uint8[size]); auto it = CodedOutputStream::WriteVarint32ToArray(msg_size, buffer.get()); bool written = message.SerializeToArray(it, msg_size); assert(written); return serialized_message(msg_size + (it - buffer.get()), buffer); }
void CNetworkPlayer::TryDequeueCommands () { while (m_unreliableQueue.size() > 0) { if ((m_nextMessage.ByteSize() + m_unreliableQueue.front().ByteSize()) > SOCKET_MAX_MESSAGE_SIZE) { return; } *m_nextMessage.add_commands() = m_unreliableQueue.front(); m_unreliableQueue.pop(); } }
bool Caster::sendMessage(const Message& message) { int msg_size = message.ByteSize(); QByteArray data; data.resize(msg_size + 4); qToBigEndian<uint32_t>(msg_size, reinterpret_cast<unsigned char*>(data.data())); if (!message.SerializeToArray(data.data() + 4, msg_size)) { return false; } return socket_.write(data) == data.size(); }
int MetaSerializer::Pack(void* obj,Buffer & buffer) { Message* msg = (Message*)obj;//m_pObjFactory->GetPrototype(meta); //const Descriptor* meta = msg->GetDescriptor(); bool ret = msg->SerializeToArray(buffer.pBuffer, buffer.iCap); if(!ret) { LOG_ERROR("meta name = %s pack error !",msg->GetDescriptor()->name().c_str()); buffer.iUsed = 0; return -1; } buffer.iUsed = msg->ByteSize(); return 0; }
void CNetworkPlayer::TryDequeueReliableCommands () { unsigned seq = (m_currentSeq == m_lastSentSeq) ? m_currentSeq + 1 : m_currentSeq; ReliableCommand rc; rc.set_sequence(seq); while (m_reliableQueue.size() > 0) { *rc.mutable_command() = m_reliableQueue.front(); if ((m_nextMessage.ByteSize() + rc.ByteSize()) > SOCKET_MAX_MESSAGE_SIZE) { return; } *m_nextMessage.add_reliablecommands() = rc; m_currentSeq = seq; m_reliableQueue.pop(); } }
bool Robot::send(int msgId, Message &msg) { if (!m_link->isopen()) { // LOG_ERROR << "robot<" << m_robotId << "> [" << m_link->m_localAddr.toIpPort() << "] <-> gatesvr [" << m_link->m_peerAddr.toIpPort() << "] is not open"; return false; } if (!m_isEncrypt) { m_link->send(msgId, msg); return true; } uint32 headSize = sizeof(NetMsgHead); int size = msg.ByteSize(); bool ok = msg.SerializeToArray(m_link->m_net->g_encryptBuf + headSize + EncryptHeadLen, size); if (!ok) { LOG_ERROR << "robot<" << m_robotId << "> [" << m_link->getLocalAddr().toIpPort() << "] <-> gatesvr [" << m_link->getPeerAddr().toIpPort() << "] send msg failed, SerializeToArray error, [len=" << size << "] failed, content = [" << msgtool::getMsgDebugString(msg) << "]"; return false; } // 添加加解密头尾 uint8* decryptBuf = (uint8*)(m_link->m_net->g_encryptBuf + headSize); int decryptBufLen = size + EncryptHeadLen + EncryptTailLen; encrypttool::encrypt(decryptBuf, decryptBufLen, m_encryptKey, sizeof(m_encryptKey)); NetMsgHead* pHeader = (NetMsgHead*)m_link->m_net->g_encryptBuf; int packetLen = msgtool::buildNetHeader(pHeader, msgId, decryptBufLen); if (packetLen <= 0) { LOG_ERROR << "robot<" << m_robotId << "> [" << m_link->getLocalAddr().toIpPort() << "] <-> gatesvr [" << m_link->getPeerAddr().toIpPort() << "] pakcetLen = " << packetLen; return false; } m_link->send(m_link->m_net->g_encryptBuf, packetLen); return true; }
int MetaSerializer::GetPackSize(void* obj) { Message* msg = (Message*)obj;//m_pObjFactory->GetPrototype(meta); return msg->ByteSize(); }