modded version CNMAT/OSC https://github.com/CNMAT/OSC
Fork of osc-cnmat by
Diff: OSCMessage.cpp
- Revision:
- 4:107c23eb31b6
- Parent:
- 2:61caa2495215
--- a/OSCMessage.cpp Sat May 17 12:42:29 2014 +0000 +++ b/OSCMessage.cpp Sat Feb 13 11:29:14 2016 +0000 @@ -26,6 +26,22 @@ #include "OSCMessage.h" #include "OSCMatch.h" +// Fast strlen function http://www.strchr.com/optimized_strlen_function +inline size_t strlengthgth(const char *s) +{ + size_t len = 0; + + for (;;) + { + unsigned x = *(unsigned*)s; + if ((x & 0xFF) == 0) return len; + if ((x & 0xFF00) == 0) return len + 1; + if ((x & 0xFF0000) == 0) return len + 2; + if ((x & 0xFF000000) == 0) return len + 3; + s += 4, len += 4; + } +} + /*============================================================================= CONSTRUCTORS / DESTRUCTOR =============================================================================*/ @@ -215,7 +231,6 @@ return testType(position, 't'); } - bool OSCMessage::isFloat(int position){ return testType(position, 'f'); } @@ -347,11 +362,11 @@ if (typePad == 0){ typePad = 4; // to make sure the type string is null terminated } - messageSize+=typePad; + messageSize += typePad; //then the data for (int i = 0; i < dataCount; i++){ OSCData * datum = getOSCData(i); - messageSize+=datum->bytes; + messageSize += datum->bytes; messageSize += padSize(datum->bytes); } return messageSize; @@ -458,6 +473,115 @@ } }*/ + +int OSCMessage::send(uint8_t * messageBuffer){ + int i, j; + //don't send a message with errors + if (hasError()){ + return -1; + } + uint8_t nullChar = '\0'; + //send the address + int addrLen = strlen(address) + 1; + //padding amount + int addrPad = padSize(addrLen); + //write it to the stream + //p.write((uint8_t *) address, addrLen); + int addrIdx = 0; + int idx = 0 + bundleOffset; + + while(addrLen--) { + messageBuffer[idx++] = address[addrIdx++]; + } + //add the padding + while(addrPad--){ + //p.write(nullChar); + messageBuffer[idx++] = nullChar; + } + //add the comma seperator + //p.write((uint8_t) ','); + messageBuffer[idx++] = ','; + //add the types +#ifdef PAULSSUGGESTION + // Paul suggested buffering on the stack + // to improve performance. The problem is this could exhaust the stack + // for long complex messages + { + //uint8_t typstr[dataCount]; + + for (i = 0; i < dataCount; i++){ + //typstr[i] = getType(i); + messageBuffer[idx++] = getType(i); + } + //p.write(typstr,dataCount); + } +#else + for (i = 0; i < dataCount; i++){ + //p.write((uint8_t) getType(i)); + messageBuffer[idx++] = getType(i); + } +#endif + //pad the types + int typePad = padSize(dataCount + 1); // 1 is for the comma + + if (typePad == 0) { + typePad = 4; // This is because the type string has to be null terminated + } + + while(typePad--){ + //p.write(nullChar); + messageBuffer[idx++] = nullChar; + } + //write the data + for (i = 0; i < dataCount; i++) { + + OSCData * datum = getOSCData(i); + + if ((datum->type == 's') || (datum->type == 'b')){ + //p.write(datum->data.b, datum->bytes); + for (j = 0; j < datum->bytes; j++) { + messageBuffer[idx++] = datum->data.b[j]; + } + + int dataPad = padSize(datum->bytes); + while(dataPad--){ + //p.write(nullChar); + messageBuffer[idx++] = nullChar; + } + } else if (datum->type == 'd'){ + double d = BigEndian(datum->data.d); + //uint8_t * ptr = (uint8_t *) &d; + uint8_t * ptr = (uint8_t *) &d; + //p.write(ptr, 8); + for (j = 0; j < 8; j++) { + messageBuffer[idx++] = ptr[j]; + } + } else if (datum->type == 't'){ + uint64_t d = BigEndian(datum->data.l); + //uint8_t * ptr = (uint8_t *) &d; + uint8_t * ptr = (uint8_t *) &d; + //p.write(ptr, 8); + for (j = 0; j < 8; j++) { + messageBuffer[idx++] = ptr[j]; + } + } else if (datum->type == 'T' || datum->type == 'F') { + + } else { // float or int + //uint32_t i = BigEndian(datum->data.i); + uint32_t ui = BigEndian(datum->data.i); + //uint8_t * ptr = (uint8_t *) &i; + uint8_t * ptr = (uint8_t *) &ui; + //p.write(ptr, datum->bytes); + for (j = 0; j < datum->bytes; j++) { + messageBuffer[idx++] = ptr[j]; + } + } + } + + bundleOffset = 0; + return idx; +} + /*============================================================================= FILLING =============================================================================*/ @@ -465,13 +589,13 @@ void OSCMessage::fill(uint8_t incomingByte){ decode(incomingByte); } - +/* void OSCMessage::fill(uint8_t * incomingBytes, int length){ while (length--){ decode(*incomingBytes++); } } - +*/ /*============================================================================= DECODING =============================================================================*/ @@ -578,6 +702,7 @@ break; } } + } //does not validate the incoming OSC for correctness @@ -685,4 +810,4 @@ } incomingBufferSize = 0; -} \ No newline at end of file +}