Example #1
0
// Send some bytes over the wire
bool Client::SendBytes(char* pVals, int iLen) {
	int i = 0;

	if (send(m_iSock, (char *) pVals, iLen, 0) == -1) {
		perror("Client::SendBytes, send bytes");
		return false;
	}

	if (VERBOSE) {
		printf("Client: sending %d bytes - ", iLen);

		for (i = 0; i < iLen; i++)
			printf("%d ", pVals[i]);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!RecvAck())
		return false;
	if (!SendAck())
		return false;
#endif

	return true;
}
Example #2
0
int Client::RecvFile(const char* pFilePath, int iLen) {
	int i = 0;
	int j = 0;
	char* pTemp = NULL;
	int iTotalBytes = 0;
	int iNumBytes = 0;
	bool bEnd = false;

	pTemp = (char *) m_pBuffer;

	FILE *fp = fopen(pFilePath, "w");
	int ret;
	printf("iLen is: %d\n", iLen);

	// We receiving the incoming bytes one byte at a time.
	while (!bEnd) {
		if ((iNumBytes = recv(m_iSock, pTemp, CLIENT_BUFF_SIZE, 0)) == -1) {
			perror("Client::RecvFile, recv");
			return -1;
		}
		/*for (i = 0; i < iNumBytes; i++) {
			//pVals[j] = pTemp[i];
			ret = fputc((int)pTemp[i], fp);
			if (ret == EOF) {
				perror("Client::RecvFile, fputc");
				return -1;
			}
			j++;
		}*/
		fwrite(pTemp, 1, iNumBytes, fp);
		fflush(fp);

		iTotalBytes += iNumBytes;
		if (iTotalBytes == iLen)
			bEnd = true;
	}

	ret = fclose(fp);
	if (ret == EOF) {
		perror("Client::RecvFile, fclose");
	}

	if (VERBOSE) {
		printf("Client: received file of %d bytes - ", iTotalBytes);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!SendAck())
		return -1;
	if (!RecvAck())
		return -1;
#endif

	return iTotalBytes;
}
Example #3
0
// Receive some floats, returns number of floats received.
int Client::RecvFloats(float* pVals, int iLen) {
	int i = 0;
	int j = 0;
	char* pTemp = NULL;
	char* pResult = NULL;
	int iTotalBytes = 0;
	int iNumBytes = 0;
	bool bEnd = false;

	pTemp = (char *) m_pBuffer;
	pResult = (char *) m_pBuffer2;

	// We receiving the incoming floats one byte at a time.
	while (!bEnd) {
		if ((iNumBytes = recv(m_iSock, pTemp, CLIENT_BUFF_SIZE, 0)) == -1) {
			perror("Client::RecvFloats, recv");
			return -1;
		}
		for (i = 0; i < iNumBytes; i++) {
			pResult[j] = pTemp[i];
			j++;
		}

		iTotalBytes += iNumBytes;
		if (iTotalBytes == (iLen * sizeof(float)))
			bEnd = true;
	}

	// Now we need to put the array of bytes into the array of floats
	char* ptr = NULL;
	int iNum = j / sizeof(float);

	ptr = (char *) pVals;

	for (i = 0; i < j; i++) {
		ptr[i] = pResult[i];
	}

	if (VERBOSE) {
		printf("Client: received %d floats - ", iNum);
		for (i = 0; i < iNum; i++)
			printf("%f ", pVals[i]);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!SendAck())
		return -1;
	if (!RecvAck())
		return -1;
#endif

	return iNum;

}
Example #4
0
// Send some floats over the wire
bool Server::SendDoubles(double* pVals, int iLen)
{
	double*	buff	= NULL;
	char*	ptr		= NULL;
	char*	valptr	= NULL;
	int		i		= 0;
	int		j		= 0;

	// We may need to reverse the byte order, oh joy
	if (m_bReverse)
	{
		// Set the buff pointer to our previously allocated spot
		buff	= (double *) m_pBuffer;
		ptr		= (char *)  buff;
		valptr	= (char *)  pVals;

		// We need to reverse the order of each set of bytes
		for (i = 0; i < iLen; i++)
		{
			for (j=0; j < sizeof(double); j++)
				ptr[i * sizeof(double) + j] = (char) valptr[(i + 1) * sizeof(double) - j - 1];
		}
		if (send(m_iSock, (char *) buff, sizeof(double) * iLen, 0) == -1)
		{
			perror("Server::SendDoubles, send double");              		
			return false;
		}
	}
	else
	{
		if (send(m_iSock, (char *) pVals, sizeof(double) * iLen, 0) == -1)
		{
			perror("Server::SendDoubles, send double");
			return false;
		}
	}

	if (VERBOSE)
	{ 
		printf("Server: sending %d doubles - ", iLen);                       
		for (i = 0; i < iLen; i++)
			printf("%0.3f ", pVals[i]);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!RecvAck())
		return false;
	if (!SendAck())
		return false;
#endif

	return true;
}
Example #5
0
// Receive some bytes, returns number of bytes received.
int Server::RecvBytes(char* pVals, int iLen)
{
	int			i				= 0;
	int			j				= 0;
	char*		pTemp			= NULL;
	int			iTotalBytes		= 0;
	int			iNumBytes		= 0;
	bool		bEnd			= false;

	pTemp = (char *) m_pBuffer;

	// We receiving the incoming ints one byte at a time.
	while (!bEnd)
	{
		if ((iNumBytes = recv(m_iSock, pTemp, SERVER_BUFF_SIZE, 0)) == -1)
		{
			perror("Server::RecvBytes, recv");
			return -1;
		}
		for (i = 0; i < iNumBytes; i++)
		{
			pVals[j] = pTemp[i];
			j++;
		}

		iTotalBytes += iNumBytes;
		if (iTotalBytes == iLen)
			bEnd = true;
	}

	if (VERBOSE)
	{
		printf("Server: received %d bytes - ", iTotalBytes);
		for (i = 0; i < iTotalBytes; i++)
			printf("%d ", pVals[i]);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!SendAck())
		return -1;
	if (!RecvAck())
		return -1;
#endif

	return iTotalBytes;
}
Example #6
0
// Receive a string, returns num of bytes received.
int Server::RecvString(char* pStr, int iMax, char chTerm)
{
	int		iNumBytes	= 0;
	bool	bEnd		= false;
	int		i			= 0;
	int		j			= 0;
	int		iLastRead	= 0;

	// Set the temp buffer to our already allocated spot
	char* pTemp = (char *) m_pBuffer;

	// This is annoying, but the java end is sending a char
	// at a time, so we recv some chars (probably 1), append
	// it to our str string, then carry on until we see
	// the terminal character.
	while (!bEnd)
	{
		if ((iLastRead = recv(m_iSock, pTemp, SERVER_BUFF_SIZE, 0)) == -1)
		{
			perror("Server::RecvString, recv");
			return -1;
		}
		for (i = 0; i < iLastRead; i++)
		{
			pStr[j] = pTemp[i];	
			j++;
		}
		if ((pTemp[i - 1] == chTerm) || (j == (iMax - 1)))
			bEnd = true;

		iNumBytes += iLastRead;
	}

	pStr[j] = '\0';

	if (VERBOSE) 
		printf("Server: received '%s'\n", pStr);                       	

#ifdef DEBUG_ACK
	if (!SendAck())
		return -1;
	if (!RecvAck())
		return -1;
#endif

	return iNumBytes;
}
Example #7
0
// Send a string to the socket
bool Client::SendString(char* pStr) {
	if (send(m_iSock, (char *) pStr, strlen(pStr), 0) == -1) {
		perror("Client::SendString, send");
		return false;
	}

	if (VERBOSE)
		printf("Client: sending string '%s'\n", pStr);

#ifdef DEBUG_ACK
	if (!RecvAck())
		return false;
	if (!SendAck())
		return false;
#endif

	return true;
}
Example #8
0
// Send some floats over the wire
bool Client::SendFloats(float* pVals, int iLen) {
	int i = 0;

	if (send(m_iSock, (char *) pVals, sizeof(float) * iLen, 0) == -1) {
		perror("Client::SendFloats, send floats");
		return false;
	}

	if (VERBOSE) {
		printf("Client: sending %d floats - ", iLen);
		for (i = 0; i < iLen; i++)
			printf("%0.3f ", pVals[i]);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!RecvAck())
		return false;
	if (!SendAck())
		return false;
#endif

	return true;
}
/* Send
 * ----------------------------------------------------------------------------
 */
ECommError CLogBlock::Send (BYTE byCommand, BYTE *pData, UINT uLen)
{
	ASSERT(m_pPhys);

	// Build block frame
	if (m_pDataFrame == NULL)
	{
		m_pDataFrame = (XFrame *)new char [HD_FRAME + m_Para.uBlockLen];
	}

	memset (m_pDataFrame, 0x00, HD_FRAME);

	m_pDataFrame->BLK.CMD.byCommand	= byCommand;

	USHORT	uBlock		= 0;
	UINT	uBlockLen	= 0;
	UINT	uDataLen	= 0;
	BOOL	bLast		= FALSE;
	BYTE	byType		= BT_DATA;

	UINT	uProgMax	= uLen / m_Para.uBlockLen + ((uLen % m_Para.uBlockLen) > 0 ? 1 : 0);
	UINT	uProgCur	= 0;

	while (! bLast)
	{
		PROGRESS(uProgCur, uProgMax);
		
		// Block count starts with 1!
		uBlock++;
		
		m_pDataFrame->byType = byType;

		// Build data length
		uDataLen = (uLen - (uBlock - 1) * m_Para.uBlockLen) <= m_Para.uBlockLen ? 
				   (uLen - (uBlock - 1) * m_Para.uBlockLen) : m_Para.uBlockLen;
		
		// Build block length
		uBlockLen	= uDataLen + HD_FRAME;
		bLast		= (uBlock * m_Para.uBlockLen >= uLen);

		m_pDataFrame->BLK.byLast	= (uBlock * m_Para.uBlockLen >= uLen);
		m_pDataFrame->BLK.uBlock	= uBlock;			
		m_pDataFrame->BLK.uLen		= (USHORT)uDataLen;

		if (pData != NULL)
		{
			memcpy(m_pDataFrame->BLK.CMD.pData, pData + m_Para.uBlockLen * (uBlock - 1), uDataLen);
		}

		BuildCRC(m_pDataFrame);

		// Convert Little -> Big Endian
		HtoT(m_pDataFrame);

		ECommError eError = ceError;

		for (UINT i = 0; (i < m_uRepeatOnError && eError != ceAck) || i == 0; i++)
		{

			TRACE(_T(">>> %-8s %3d %3d     "), byCommand <= LAST_CMD_VALUE ? szCommandText[byCommand] : _T("Invalid"), uBlock, uDataLen);
			TrcPrint(TRC_INTERFACE, _T(">>> %-8s %3d %3d     "), byCommand <= LAST_CMD_VALUE ? szCommandText[byCommand] : _T("Invalid"), uBlock, uDataLen);

			// Send block
			eError = m_pPhys->Send((BYTE *)m_pDataFrame, HD_FRAME + uDataLen);

			if (eError != ceOK)
			{
				// Communication error sending data block.
				SleepEx (m_uWaitResend, FALSE);
				m_pPhys->Flush();
				
				continue;
			}

			// Wait for acknowledge
			eError = RecvAck (uBlock);

			if (eError != ceAck && eError != ceNAck)
			{
				// Communication Error receiving ACK / NACK.
				SleepEx (m_uWaitResend, FALSE);
				m_pPhys->Flush();
				
				continue;
			}
		
		} // end for 

		if (eError != ceAck)
		{
			// A block could not be transmitted correctly. 
			PROGRESS_LAST(uProgMax);
			return eError;
		}
	
		uProgCur++;
		
	} // end while

	PROGRESS(uProgCur, uProgMax);

	return ceOK;
}
Example #10
0
// Receive some doubles, returns num of doubles received.
int Server::RecvDoubles(double* pVals, int iLen)
{
	int			i				= 0;
	int			j				= 0;
	char*		pTemp			= NULL;
	char*		pResult			= NULL;
	int			iTotalBytes		= 0;
	int			iNumBytes		= 0;
	bool		bEnd			= false;

	pTemp	= (char *) m_pBuffer;
	pResult	= (char *) m_pBuffer2;

	// We receiving the incoming ints one byte at a time.
	while (!bEnd)
	{
		if ((iNumBytes = recv(m_iSock, pTemp, SERVER_BUFF_SIZE, 0)) == -1)
		{
			perror("Server::RecvDoubles, recv");
			return -1;
		}
		for (i = 0; i < iNumBytes; i++)
		{
			pResult[j] = pTemp[i];	
			j++;
		}

		iTotalBytes += iNumBytes;
		if (iTotalBytes == (iLen * sizeof(double)))
			bEnd = true;
	}

	// Now we need to put the array of bytes into the array of ints
	char*	ptr		= (char *) pVals;
	int		iNum	= j / sizeof(double);

	// The significance order depends on the platform
	if (m_bReverse)
	{
		// we need to reverse the order of each set of bytes
		for (i = 0; i < iNum; i++)
		{
			for (j = 0; j < sizeof(double); j++)
				ptr[i * sizeof(double) + j] = (char) pResult[(i + 1) * sizeof(double) - j - 1];
		}
	}
	else
	{
		// Leave the byte order as is
		for (i = 0; i < j; i++)
		{
			ptr[i] = pResult[i];
		}
	}

	if (VERBOSE) 
	{
		printf("Server: received %d doubles - ", iNum);             	
		for (i = 0; i < iNum; i++)
			printf("%0.3f ", pVals[i]);
		printf("\n");
	}

#ifdef DEBUG_ACK
	if (!SendAck())
		return -1;
	if (!RecvAck())
		return -1;
#endif

	return iNum;
}