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]