Sample code
"seanstewarthunt" <[email protected]>
| Newsgroups | gmane.comp.programming.cppug |
|---|---|
| Message-ID | <[email protected]> |
Folks,
I would really appreciate comments on th efollowing code fragment;
all notes, comments, and issues are welcome...
#include "stdafx.h"
/** Returns the number of bytes contained in the message */
/** Returns the byte array that represents this message */
BYTE* MessageF::getBytes()
{
BYTE* message = new BYTE[getMessageLength()];
BYTE* payload = getPayload();
int payloadSize = getPayloadSize();
int x, checksum;
// START bytes
message[0] = STATE_START1;
message[1] = STATE_START1;
// Payload Size
// high byte
message[2] = (BYTE) ((0xFF00 & (payloadSize + 2)) >> 8) ; //
Size is PayloadSize + 2 bytes (channel and ID)
// low byte
message[3] = (BYTE) (0xFF & (payloadSize + 2));
// F channel ID
message[4] = channel;
// Payload - Message ID
message[5] = ID;
// Payload Data
for (x = 0; x < payloadSize; x++)
{
message[x + 6] = payload[x];
}
// Checksum
// Calculate the checksum for the segment (Channel ID and
Payload). Set bit 0x8000 to indicate direction CP->SLC.
checksum = 0x8000 | getChecksum(message, 4, payloadSize + 2);
message[payloadSize + 6] = (BYTE) ((0xFF00 & checksum) >> 8) ;
message[payloadSize + 7] = (BYTE) (0xFF & checksum) ;
// END Bytes
message[payloadSize + 8] = STATE_END1;
message[payloadSize + 9] = STATE_END2;
return message;
}
/** Assembles a F message from a byte array */
MessageF* MessageF::assembleF(BYTE* message, int length)// throws
DecodingException
{
MessageF* m = NULL;
// check if the channel is valid
if (message[0] != F_CHANNEL) throw new DecodingException
("Invalid F Message received: Incorrect channel number!");
//create the appropriate message based on the message ID
switch (message[1])
{
case MESSAGE_F_SESSION_OPEN_NOTIFICATION:
m = new MessageF_SessionOpenNotification(message,
length);
break;
case MESSAGE_F_SESSION_CLOSE_NOTIFICATION:
m = new MessageF_SessionCloseNotification(message,
length);
break;
case MESSAGE_F_ERROR_NOTIFICATION:
m = new MessageF_ErrorNotification(message, length);
break;
case MESSAGE_F_HARDWARE_CONFIG_REQUEST:
m = new MessageF_HardwareConfigRequest(message, length);
break;
case MESSAGE_F_TIME_TRANSFER_REQUEST:
m = new MessageF_TimeTransferRequest(message, length);
break;
case MESSAGE_F_FREQUENCY_TRANSFER_REQUEST:
m = new MessageF_FrequencyTransferRequest(message,
length);
break;
case MESSAGE_F_APPROX_MS_POSITION_REQUEST:
m = new MessageF_ApproxMSPositionRequest(message,
length);
break;
case MESSAGE_F_REJECT:
m = new MessageF_Reject(message, length);
break;
case MESSAGE_F_SOFTWARE_VERSION_RESPONSE:
m = new MessageF_SoftwareVersionResponse(message,
length);
break;
case MESSAGE_F_CHANNEL_OPEN_RESPONSE:
m = new MessageF_ChannelOpenResponse(message, length);
break;
case MESSAGE_F_CHANNEL_CLOSE_RESPONSE:
m = new MessageF_ChannelCloseResponse(message, length);
break;
default:
throw new DecodingException("Invalid F Message received:
invalid Message ID: " + message[1]);
}
return m;
}
------------------------ Yahoo! Groups Sponsor --------------------~-->
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/cppug/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/