说明:需要传入一个socket和Protobuf对象,传输的时候会先发送一个数据长度,然后才是protobuf的数据
#pragma once
#ifndef __PROTOC_H__
#define __PROTOC_H__
#include "ConnectProtocol.pb.h" //protobuf编译生成的定义文件
#include <iostream>
#include <string>
template<typename T>
inline int ReceiveGPTB(SOCKET & ClientSocket, T & CGPTB)
{
int RecveIn = 0;
unsigned int DataLenth = 0;
unsigned int DataLenth_Img = 0;
char * p_DataLenth = (char *)&DataLenth;
unsigned int UIntSize = sizeof(unsigned int);
unsigned int UIntSize_Img = UIntSize;
int ErrorNum = 0;
int a = 0;
while (UIntSize) {
RecveIn = recv(ClientSocket, (char *)&DataLenth, UIntSize, 0);
if (RecveIn == INVALID_SOCKET) {
printf("Time out\n");
return -1;
}
if (ErrorNum >= 10) {
printf("Time out\n");
return -2;
}
if (RecveIn == 0) {
ErrorNum++;
printf("%d tr