uint32_t Node::encode(std::vector<uint8_t>& buffer) const { uint32_t size = 0; uint32_t ret = 0; buffer.push_back(static_cast<uint8_t>(marker)); size += 1; switch (marker) { case Marker::Number: ret = writeNumber(buffer, doubleValue); break; case Marker::Boolean: ret = writeBoolean(buffer, boolValue); break; case Marker::String: ret = writeString(buffer, stringValue); break; case Marker::Object: ret = writeObject(buffer, mapValue); break; case Marker::Null: /* Null */; break; case Marker::Undefined: /* Undefined */; break; case Marker::ECMAArray: ret = writeECMAArray(buffer, mapValue); break; case Marker::ObjectEnd: break; // should not happen case Marker::StrictArray: ret = writeStrictArray(buffer, vectorValue); break; case Marker::Date: ret = writeDate(buffer, dateValue); break; case Marker::LongString: ret = writeLongString(buffer, stringValue); break; case Marker::XMLDocument: ret = writeXMLDocument(buffer, stringValue); break; case Marker::TypedObject: ret = writeTypedObject(buffer); break; case Marker::SwitchToAMF3: ret = writeSwitchToAMF3(buffer); break; default: return 0; } size += ret; return size; }
/* This is like dprintf on post 2008 POSIX * You can use FDPRINTF which might use dprintf if it is available * Parameters: the client, format * Returns: the number of characters printed */ long nprintf (int client, const char *format, ...) { /* initial buffer large enough for most cases, will resize if required */ char *buf = malloc(MAX_BUFFER_SIZE); int len; va_list arg; long done; va_start (arg, format); len = vsnprintf (buf, MAX_BUFFER_SIZE, format, arg); va_end (arg); if (len > MAX_BUFFER_SIZE) { /* buffer size was not enough */ free(buf); buf = malloc(len+1); if (buf == NULL) { printf("Could not allocate memory."); exit(EXIT_FAILURE); } va_start (arg, format); vsnprintf (buf, len+1, format, arg); va_end (arg); } /* printf("Buffer length %d",strlen(buf)); */ if (len<MAX_BUFFER_SIZE) { done = (int) send(client, buf, len, 0); } else { done = writeLongString(client,buf,len); } free(buf); return done; }
/* Just like fprintf, but writing to the socket instead of a * file. * Parameters: the client, format * Returns: the number of characters printed */ long nprintf (int client, const char *format, ...) { /* Need to figure out a better method for memory management */ char *buf = malloc(MAX_BUFFER_SIZE*MAX_DPRINTF_SIZE*sizeof(char)); va_list arg; long done; va_start (arg, format); vsprintf (buf, format, arg); va_end (arg); /* printf("Buffer length %d",strlen(buf)); */ if (strlen(buf)<MAX_BUFFER_SIZE) { done = (int) send(client, buf, strlen(buf), 0); } else { done = writeLongString(client,buf); } free(buf); return done; }