Esempio n. 1
0
BufferPtr
serializeMsg(const Msg& msg)
{
  int size = msg.ByteSize();
  BufferPtr bytes = std::make_shared<Buffer>(size);
  msg.SerializeToArray(bytes->buf(), size);
  return bytes;
}
Esempio n. 2
0
void SocketClient::sendPackage(void)
{
	Msg msg;
	Packer::packLua2Protobuf(LuaEngine::getInstance()->getLuaStack()->getLuaState(),&msg);
	static union
	{
		struct
		{
			int packageLen;
			int flags;
			int seqId;
			int crc32;
			char data[1];
		};
		char ssssssssss[SocketClientSendBufferLength];
	}sPackage;
	int protobufLength = msg.ByteSize();
	CCAssert(protobufLength <= sizeof(sPackage),"发送缓冲区将溢出!");
	msg.SerializeToArray(sPackage.ssssssssss,SocketClientSendBufferLength);
	int flags = FLAG_COMPRESSED; // 开启加密
	int encryptLength;
	void* encryptData;
	if(isCompressed(flags))
	{
		encryptData = Encrypt::encryptForNet(&encryptLength,sPackage.ssssssssss,protobufLength,(rand()<<16) + (rand()&0xFFFF));
	}
	else
	{
		encryptData = Encrypt::doXorForNet(&encryptLength,sPackage.ssssssssss,protobufLength,(rand()<<16) + (rand()&0xFFFF));
	}
	CCAssert(encryptLength <= SocketClientSendBufferLength-16,"发送缓冲区将溢出!");
	memcpy(sPackage.data,encryptData,encryptLength);	//todo可优化,不拷贝。
	delete[]((char*)encryptData);
	sPackage.packageLen = htonl(encryptLength+12);
	sPackage.flags = htonl(flags);
	sPackage.seqId = htonl(_sendSequenceNumber);
	_sendSequenceNumber+=SEQUENCE_NUMBER_INCREMENT;
	sPackage.crc32 = htonl(crc32(sPackage.data,encryptLength,0));
	int nLen = send(&sPackage,encryptLength+16);
	CCAssert(nLen == encryptLength+16,"");
	CCLOG(__FUNCTION__);
}