比如我们和前端互通的包大小限制是4K,如果发一个大于4K的包就无法发送,这时候就需要和前端拆包和包。
bool LobbyServer::SendPbMsgToClient(PlayerNode *lpPlayerNode, unsigned short type, void *buf, long len, int end)
{
__log(_ERROR, __FUNCTION__, "type[%d],len[%d],end[%d]", type, len, end);
tagNetMsg pNetMsg;
memcpy(&pNetMsg, buf, sizeof(tagNetMsg));
pNetMsg.header.identity = IDENTIFY_VER;
pNetMsg.header.encode = ENCODE_NONE;
pNetMsg.header.length = len;
pNetMsg.header.version = end;
pNetMsg.header.reserve = ENCODE_AES;
pNetMsg.header.type = type;
len += sizeof(tagMsgHeader);
return SendData(lpPlayerNode, type, &pNetMsg, len);
}
bool LobbyServer::SendBigPbMsgToClient(PlayerNode *lpPlayerNode, unsigned short type, DataPacket &strDataPacket)
{
int test_buff_size = MAX_BUFF_SIZE;
int nLength = strDataPacket.getSize();
int i = 0;
do
{
if (test_buff_size < nLength )
{
tagNetMsg sendDataStr;
memcpy(sendDataStr.buff, strDataPacket.getPositionPtr(i*test_buff_size), test_buff_size);
SendPbMsgToClient(lpPlayerNode, type, &sendDataStr, test_buff_size, 0);
}
else
{
tagNetMsg sendDataStr;
memcpy(sendDataStr.buff, strDataPacket.getPositionPtr(i*test_buff_size), nLength);
SendPbMsgToClient(lpPlayerNode, type, &sendDataStr, nLength, 1);
}
nLength = nLength - test_buff_size;
i++;
} while (0 < nLength);
}