
API in C
220
* Encode message length and write it out to the socket
********************************************************************/
void writeLen(int fdSock, int iLen)
{
char *cEncodedLength; // encoded length to send to the api socket
char *cLength; // exactly what is in memory at &iLen integer
cLength = calloc(sizeof(int), 1);
cEncodedLength = calloc(sizeof(int), 1);
// set cLength address to be same as iLen
cLength = (char *)&iLen;
DEBUG ? printf("length of word is %d\n", iLen) : 0;
// write 1 byte
if (iLen < 0x80)
{
cEncodedLength[0] = (char)iLen;
write (fdSock, cEncodedLength, 1);
}
// write 2 bytes
else if (iLen < 0x4000)
{
DEBUG ? printf("iLen < 0x4000.\n") : 0;
if (iLittleEndian)
{
cEncodedLength[0] = cLength[1] | 0x80;
cEncodedLength[1] = cLength[0];
}
else
{
cEncodedLength[0] = cLength[2] | 0x80;
cEncodedLength[1] = cLength[3];
}
write (fdSock, cEncodedLength, 2);
}
// write 3 bytes
else if (iLen < 0x200000)
{
DEBUG ? printf("iLen < 0x200000.\n") : 0;
if (iLittleEndian)
Commenti su questo manuale