Refactored various message-related operations into the Message class.

- Replaces the XPCMessage struct.
 - Messages are now read from a UDPSocket by the Message class.
 - Printing messages to the log is now handled by the Message class.
 - In the future, may extend this class with command-specific subclasses that encapsulate additional message parsing functionality.
This commit is contained in:
Jason Watkins
2015-04-11 09:12:47 -07:00
parent 2736cd86de
commit 547eb4de18
9 changed files with 363 additions and 303 deletions

View File

@@ -36,22 +36,6 @@ XPLMDataRef XPLMDataRefs[134][8];
XPLMDataRef multiplayer[20][17];
XPLMDataRef AIswitch;
void readMessage(XPC::UDPSocket* socket, struct XPCMessage * pMessage)
{
pMessage->msglen = socket->Read((std::uint8_t*)pMessage->msg, 5000, &pMessage->recvaddr);
if ( pMessage->msglen <= 0 ) // No Message
{
pMessage->msg[0] = 0;
}
else
{
strncpy( pMessage->head, pMessage->msg, 4 );
}
return;
}
void buildXPLMDataRefs()
{
int i, j;
@@ -300,146 +284,6 @@ unsigned short getIP(struct sockaddr recvaddr, char *IP)
// DEBUGGING TOOLS
// --------------------------------
int printBufferToLog(struct XPCMessage & msg)
{
// Prints the entire buffer to the log (for debugging)
char logmsg[350] = "[DEBUG]";
int i;
for (i=0;i<msg.msglen && strlen(logmsg)<(sizeof(logmsg)-4);i++)
{
sprintf(logmsg,"%s %hhu",logmsg,msg.msg[i]);
}
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
sprintf(logmsg,"[%s-DEBUG](%i)",msg.head,msg.msg[4]);
//Switch for header
if (strncmp(msg.head,"CONN",4)==0)
{// Header = CONN (Connection)
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"SIMU",4)==0)
{// Header = SIMU
sprintf(logmsg,"%s %i",logmsg,msg.msg[5]);
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"POSI",4)==0)
{// Header = POSI (Position)
float position[8] = {0.0};
float pos[3],orient[3];
short aircraft = 0;
float gear = -1.0;
// UPDATE LOG
aircraft = parsePOSI(msg.msg,position,6, &gear);
//ADD AIRCRAFT HANDLING
// Position
memcpy(pos,position,3*sizeof(float));
// Orientation
memcpy(orient,&position[3],3*sizeof(float));
sprintf(logmsg,"%s %i (%f %f %f) (%f %f %f) %f",logmsg,aircraft,pos[0],pos[1],pos[2],orient[0],orient[1],orient[2],gear);
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"CTRL",4)==0)
{// Header = CTRL (Control)
xpcCtrl ctrl = parseCTRL(msg.msg);
sprintf(logmsg,"%s (%f %f %f) %f %hhi %f",logmsg, ctrl.pitch, ctrl.roll, ctrl.yaw, ctrl.throttle, ctrl.gear, ctrl.flaps);
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"WYPT",4)==0)
{// Header = WYPT (Waypoint Draw)
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"GETD",4)==0)
{// Header = GETD (Data Request)
char DREF[100];
int counter = 6;
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
for (i=0;i<msg.msg[5];i++)
{
memcpy(DREF,&msg.msg[counter+1],msg.msg[counter]);
sprintf(logmsg,"\t#%i/%i (size:%i) %s",i+1,msg.msg[5],msg.msg[counter],DREF);
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
memset(DREF,0,sizeof(DREF));
counter += msg.msg[counter]+1;
}
}
else if (strncmp(msg.head,"DREF",4)==0)
{// Header = DREF (By Data Ref) (this is slower than DATA)
char DREF[100]={0};
float tmp;
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
memcpy(DREF,&msg.msg[6],msg.msg[5]);
sprintf(logmsg,"-\tDREF (size %i)= %s",msg.msg[5],DREF);
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
sprintf(logmsg,"-\tValues(Size %i)=",msg.msg[6+msg.msg[5]]);
for (i=0;i<msg.msg[6+msg.msg[5]];i++)
{
memcpy(&tmp,&msg.msg[7+msg.msg[5]+sizeof(float)*i],sizeof(float));
sprintf(logmsg,"%s %f",logmsg,tmp);
}
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"VIEW",4)==0)
{// Header = VIEW (Change View)
XPC::Log::WriteLine(logmsg);
}
else if (strncmp(msg.head,"DATA",4)==0)
{// Header = DATA (UDP Data)
int j;
short totalColumns = ((msg.msglen-5)/36);
float dataRef[30][9];
sprintf(logmsg,"%s (%i lines)",logmsg,totalColumns);
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
totalColumns = parseDATA(msg.msg, msg.msg[4], dataRef);
for (i=0; i<totalColumns; i++)
{
sprintf(logmsg,"\t#%i: ",(int) dataRef[i][0]);
for (j=1; j<9; j++)
{
sprintf(logmsg,"%s %f",logmsg,dataRef[i][j]);
if (dataRef[i][j]!=dataRef[i][j]) sprintf(logmsg,"%s (not a number)",logmsg);
}
XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg));
}
}
else
{
return -1; //unrecognized header
}
return 0;
}
int test(const char *buffer)
{