00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "Aria.h"
00027 #include "ArExport.h"
00028 #include "ArNetPacket.h"
00029
00030 AREXPORT ArNetPacket::ArNetPacket(ArTypes::UByte2 bufferSize) :
00031 ArBasePacket(bufferSize, ArNetPacket::HEADER_LENGTH, NULL, ArNetPacket::FOOTER_LENGTH)
00032 {
00033 myCommand = 0;
00034 myAddedFooter = false;
00035 }
00036
00037 AREXPORT ArNetPacket::~ArNetPacket()
00038 {
00039
00040 }
00041
00042 AREXPORT void ArNetPacket::doubleToBuf(double val)
00043 {
00044 char buf[256];
00045 if (val == -HUGE_VAL)
00046 sprintf(buf, "-INF");
00047 else if (val == HUGE_VAL)
00048 sprintf(buf, "INF");
00049 else
00050 sprintf(buf, "%g", val);
00051 strToBuf(buf);
00052 }
00053
00054 AREXPORT double ArNetPacket::bufToDouble(void)
00055 {
00056 char buf[256];
00057 char *endPtr;
00058 double ret;
00059
00060 bufToStr(buf, sizeof(buf));
00061 if (strncmp(buf, "-INF", sizeof(buf)) == 0)
00062 {
00063 ret = -HUGE_VAL;
00064 return ret;
00065 }
00066 else if (strncmp(buf, "INF", sizeof(buf)) == 0)
00067 {
00068 ret = HUGE_VAL;
00069 return ret;
00070 }
00071 else
00072 {
00073 ret = strtod(buf, &endPtr);
00074 if (endPtr[0] == '\0' && endPtr != buf)
00075 return ret;
00076 else
00077 return 0;
00078 }
00079 }
00080
00081 AREXPORT void ArNetPacket::empty(void)
00082 {
00083 myCommand = 0;
00084 myLength = myHeaderLength;
00085 myAddedFooter = false;
00086 resetValid();
00087 }
00088
00089 AREXPORT void ArNetPacket::finalizePacket(void)
00090 {
00091 int length;
00092 length = myLength;
00093 int chkSum;
00094
00095 myLength = 0;
00096 uByteToBuf(0xF);
00097 uByteToBuf(0xC);
00098 if (myAddedFooter)
00099 uByte2ToBuf(length);
00100 else
00101 uByte2ToBuf(length+2);
00102 uByte2ToBuf(myCommand);
00103 if (myAddedFooter)
00104 myLength = length - 2;
00105 else
00106 myLength = length;
00107 chkSum = calcCheckSum();
00108 byteToBuf((chkSum >> 8) & 0xff );
00109 byteToBuf(chkSum & 0xff );
00110 myAddedFooter = true;
00111
00112
00113 }
00114
00115 AREXPORT void ArNetPacket::resetRead(void)
00116 {
00117 myReadLength = 4;
00118 myCommand = bufToUByte2();
00119 myReadLength = myHeaderLength;
00120 resetValid();
00121
00122 }
00123
00124 AREXPORT void ArNetPacket::setCommand(ArTypes::UByte2 command)
00125 {
00126 myCommand = command;
00127 }
00128
00129 AREXPORT ArTypes::UByte2 ArNetPacket::getCommand(void)
00130 {
00131 return myCommand;
00132 }
00133
00134 AREXPORT void ArNetPacket::duplicatePacket(ArNetPacket *packet)
00135 {
00136 myLength = packet->myLength;
00137 if (myMaxLength < myLength)
00138 setMaxLength(packet->myLength);
00139 myReadLength = packet->myReadLength;
00140 myHeaderLength = packet->myHeaderLength;
00141 myFooterLength = packet->myFooterLength;
00142 myCommand = packet->myCommand;
00143 myAddedFooter = packet->myAddedFooter;
00144 memcpy(myBuf, packet->getBuf(), packet->myLength + packet->myFooterLength);
00145 }
00146
00147 AREXPORT ArTypes::Byte2 ArNetPacket::calcCheckSum(void)
00148 {
00149 int i;
00150 int n;
00151 int c = 0;
00152
00153
00154
00155 i = 3;
00156 n = myLength - 2;
00157 while (n > 3) {
00158
00159 c += ((unsigned char)myBuf[i]<<8) | (unsigned char)myBuf[i+1];
00160 c = c & 0xffff;
00161 n -= 2;
00162 i += 2;
00163 }
00164
00165 if (n > 0)
00166 c = c ^ (int)((unsigned char) myBuf[i]);
00167
00168 return c;
00169 }
00170
00171 AREXPORT bool ArNetPacket::verifyCheckSum(void)
00172 {
00173 ArTypes::Byte2 chksum;
00174 ArTypes::Byte2 calcedChksum;
00175 unsigned char c1, c2;
00176 int length;
00177
00178 if (myLength - 2 < myHeaderLength)
00179 return false;
00180
00181 c2 = myBuf[myLength-2];
00182 c1 = myBuf[myLength-1];
00183 chksum = (c1 & 0xff) | (c2 << 8);
00184 length = myLength;
00185 myLength = myLength - 2;
00186 calcedChksum = calcCheckSum();
00187 myLength = length;
00188
00189
00190 if (chksum == calcedChksum) {
00191 return true;
00192 } else {
00193 return false;
00194 }
00195
00196 }