Advertisement

help - serious lag problem

Started by July 23, 2003 08:30 AM
-1 comments, last by Yanroy 21 years, 6 months ago
I've written a client/server pair where the client takes user input and pretty much sends it as-is to the server. The server makes a few modifications to it, and every once in a while outputs commands based on the user input through a COM port to some custom electronics. There's a lag of between .5 and 2 seconds across this system. I believe the lag to be in the server app's recv() function, but I don't have a profiler... Do you guys see anything horrible with this function? (I know it's ugly). Optimizations are appreciated :D
bool CRemoteComm::RecvData(char *Category, char *Identifier, char *Value)
{
	// format: [Category].[Identifier]=[Value]

	// returns false if no data has arrived


	char RecentChar = '\0';
	
	if (!Connected)
		return false;

	int RecvReturn = 1;

	while (RecvReturn != 0 && RecvReturn != SOCKET_ERROR)
	{
		FD_ZERO(&conn);
		FD_SET(Socket, &conn);
		RecvReturn = (select(Socket + 1, &conn, NULL, NULL, &Timeout) != 0);
		if (!RecvReturn)
			break;

		RecvReturn = recv(Socket, &RecentChar, 1, NULL);
		if (RecvReturn == SOCKET_ERROR || RecvReturn == 0)
		{
			CloseConnection();
			return false;
		}

		if (RecentChar == '\n' || RecentChar == '\r')
		{
			Buffer[BufferCount] = '\0';
			if (RecentChar == '\n')
				break;
		}
		else
		{
			Buffer[BufferCount] = RecentChar;
			++BufferCount;
		}
	}

	if (BufferCount == 0 || RecentChar == '\0')
		return false;

	InactivityTime = GetTickCount();

	if (RecentChar != '\n') // only say there is data if an entire piece has been received

		return false;

	// parse the incoming data

	strcpy(Category, strtok(Buffer, "."));
	if (strlen(Category) < BufferCount - 1)
	{
		strcpy(Identifier, strtok(NULL, ":"));
		Value[0] = '\0';
		if (strlen(Category) + strlen(Identifier) < BufferCount - 2) // there's parameters

		{
			strcpy(Value, strtok(NULL, ":")); // get the second half of the string

		}
	}
	else
	{
		WriteLog(TEXT("Bad Data Recv'ed from Remote"), true);
		return false;
	}

	Buffer[0] = '\0';
	BufferCount = 0;

	return true;
}
EDIT - Fixed random comments and such --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com [edited by - Yanroy on July 23, 2003 9:31:26 AM] [edited by - Yanroy on July 23, 2003 9:33:11 AM]

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

This topic is closed to new replies.

Advertisement