Ejemplo n.º 1
0
int CSocketInConnection::_receiveSimplePacket(std::vector<char>& packet)
{
	// Returns the number of packets left to read if >=0, -2=error, -1=select time out
	FD_ZERO(&_socketTheSet);
	FD_SET(_socketClient,&_socketTheSet);
#ifdef _WIN32
	int selectResult=select(0,&_socketTheSet,NULL,NULL,NULL); // No timeout!
#endif /* _WIN32 */
#if defined (__linux) || defined (__APPLE__)
	int selectResult=select(_socketClient+1, &_socketTheSet,NULL,NULL,NULL); // No timeout!
#endif /* __linux || __APPLE__ */
	if (selectResult==1)
	{
		//1. Read the header and packet size:
		char headerAndSize[HEADER_LENGTH];
		int totalReceived=0;
		unsigned int startT=_getTimeInMs();
		while(totalReceived!=HEADER_LENGTH)
		{
			int nb=recv(_socketClient,headerAndSize+totalReceived,HEADER_LENGTH-totalReceived,0);
			if (nb<1)
				break;
			totalReceived+=nb;
			if (_getTimeDiffInMs(startT)>3000)
				break;
		}
		// 2. Check if the header is consistent:
		if (totalReceived!=HEADER_LENGTH)
			return(-2); // Error reading
		if ( (headerAndSize[0]!=_headerByte1)||(headerAndSize[1]!=_headerByte2) )
			return(-2); // Error, wrong header
		unsigned short dataLength=((unsigned short*)(headerAndSize+2))[0];
		// 3. Read the data with correct length:
		packet.clear();
		packet.resize(dataLength,0);
		totalReceived=0;
		startT=_getTimeInMs();
		while(totalReceived!=dataLength)
		{
			int nb=recv(_socketClient,&packet[0]+totalReceived,dataLength-totalReceived,0);
			if (nb<1)
				break;
			totalReceived+=nb;
			if (_getTimeDiffInMs(startT)>3000)
				break;
		}
		if (totalReceived!=dataLength)
			return(-2); // wrong size or nothing received
		return(int(((unsigned short*)(headerAndSize+2))[1]));
	}
	if (selectResult==0)
		return(-1);
	return(-2);
}
Ejemplo n.º 2
0
int CSocketOutConnection::_receiveSimplePacket(std::vector<char>& packet)
{ // Returns the number of packets left to read if >=0, otherwise error
	//1. Read the header and packet size:
	char headerAndSize[HEADER_LENGTH];
	int totalReceived=0;
	unsigned short startT=_getTimeInMs();
	while(totalReceived!=HEADER_LENGTH)
	{
		int nb=recv(_socketConn,headerAndSize+totalReceived,HEADER_LENGTH-totalReceived,0);
		if (nb<1)
			break;
		totalReceived+=nb;
		if (_getTimeDiffInMs(startT)>3000)
			break;
	}
	// 2. Check if the header is consistent:
	if (totalReceived!=HEADER_LENGTH)
		return(-1); // Error reading
	if ( (headerAndSize[0]!=_headerByte1)||(headerAndSize[1]!=_headerByte2) )
		return(-1); // Error, wrong header
	unsigned short dataLength=((unsigned short*)(headerAndSize+2))[0];
	// 3. Read the data with correct length:
	packet.clear();
	packet.resize(dataLength,0);
	totalReceived=0;
	startT=_getTimeInMs();
	while(totalReceived!=dataLength)
	{
		int nb=recv(_socketConn,&packet[0]+totalReceived,dataLength-totalReceived,0);
		if (nb<1)
			break;
		totalReceived+=nb;
		if (_getTimeDiffInMs(startT)>3000)
			break;
	}
	if (totalReceived!=dataLength)
		return(-1); // wrong size or nothing received
	return(int(((unsigned short*)(headerAndSize+2))[1]));
}