void simplemax_anything(t_simplemax *x,t_symbol* s,int argc,t_atom* argv){ // msg is a dispatch name (/foo), argc is # of elements, argv is element vector if (!s){object_error((t_object*)x, "NOOOOOO!!!!");} //object_post((t_object*)x, "%s",msg->s_name); t_osc_bundle_u *bundle = osc_bundle_u_alloc();//alloc creates memory for and initializes the bundle t_osc_message_u *msg = osc_message_u_alloc(); osc_message_u_setAddress(msg, s->s_name); for(int i=0;i < argc;i++){ //going through all the arguments (arg vector) whos length is argc t_osc_atom_u* a = osc_atom_u_alloc(); switch(atom_gettype(argv+i)){ case A_LONG: osc_atom_u_setInt32(a, atom_getlong(argv+i)); break; case A_FLOAT: osc_atom_u_setFloat(a, atom_getfloat(argv+i)); break; case A_SYM: osc_atom_u_setString(a, atom_getsym(argv+i)->s_name); break; } osc_message_u_appendAtom(msg, a); //osc_message_u_appendFloat(msg, 15.0); } osc_bundle_u_addMsg(bundle, msg); long bytes = 0;//length of byte array char* pointer = NULL; osc_bundle_u_serialize(bundle, &bytes, &pointer);//& is adress of the variable //post("%ld %p", bytes,pointer); t_atom out[2]; atom_setlong(out, bytes); atom_setlong(out+1, (long)pointer); outlet_anything(x->outlet, gensym("FullPacket"), 2, out); osc_bundle_u_free(bundle);//get rid of stuff in osc message osc_mem_free(pointer);//marks pointer address as being free (clear if you want to keep using same pointer) }
t_osc_msg_u *osc_message_s_deserialize(t_osc_msg_s *msg) { t_osc_msg_u *m = osc_message_u_alloc(); osc_message_u_setAddress(m, osc_message_s_getAddress(msg)); t_osc_msg_it_s *it = osc_msg_it_s_get(msg); while(osc_msg_it_s_hasNext(it)){ t_osc_atom_s *a = osc_msg_it_s_next(it); t_osc_atom_u *ua = osc_atom_s_deserialize(a); osc_message_u_appendAtom(m, ua); } osc_msg_it_s_destroy(it); return m; }
void *oedge_new(t_symbol *msg, short argc, t_atom *argv) { t_oedge *x = NULL; if((x = (t_oedge *)object_alloc(oedge_class))){ dsp_setup((t_pxobject *)x, 1); x->outlet = outlet_new((t_object *)x, "FullPacket"); critical_new(&(x->lock)); x->ac = 0; x->av = NULL; x->lastx = 0; x->gettime = 0; x->time_onset = osc_message_u_alloc(); osc_message_u_setAddress(x->time_onset, "/zerotononzero/time"); x->block_sample_onset = osc_message_u_alloc(); osc_message_u_setAddress(x->block_sample_onset, "/zerotononzero/sample/withinblock"); x->global_sample_onset = osc_message_u_alloc(); osc_message_u_setAddress(x->global_sample_onset, "/zerotononzero/sample/sincedspstart"); x->value_onset = osc_message_u_alloc(); osc_message_u_setAddress(x->value_onset, "/zerotononzero/value"); x->time_zero = osc_message_u_alloc(); osc_message_u_setAddress(x->time_zero, "/nonzerotozero/time"); x->block_sample_zero = osc_message_u_alloc(); osc_message_u_setAddress(x->block_sample_zero, "/nonzerotozero/sample/withinblock"); x->global_sample_zero = osc_message_u_alloc(); osc_message_u_setAddress(x->global_sample_zero, "/nonzerotozero/sample/sincedspstart"); x->bundle = osc_bundle_u_alloc(); osc_bundle_u_addMsg(x->bundle, x->time_onset); osc_bundle_u_addMsg(x->bundle, x->block_sample_onset); osc_bundle_u_addMsg(x->bundle, x->global_sample_onset); osc_bundle_u_addMsg(x->bundle, x->value_onset); osc_bundle_u_addMsg(x->bundle, x->time_zero); osc_bundle_u_addMsg(x->bundle, x->block_sample_zero); osc_bundle_u_addMsg(x->bundle, x->global_sample_zero); } return x; }
void ovalidate_fullPacket(t_ovalidate *x, t_symbol *msg, int argc, t_atom *argv) { OMAX_UTIL_GET_LEN_AND_PTR; /* t_osc_err e = osc_error_bundleSanityCheck(len, ptr); if(e){ t_osc_bndl_u *b = osc_bundle_u_alloc(); t_osc_msg_u *merr = osc_message_u_alloc(); osc_message_u_setAddress(merr, "/error/str"); osc_message_u_appendString(merr, osc_error_string(e)); osc_bundle_u_addMsg(b, merr); long l = 0; char *buf = NULL; osc_bundle_u_serialize(b, &l, &buf); if(buf){ omax_util_outletOSC(x->outletErr, l, buf); omax_util_outletOSC(x->outletInval, len, ptr); osc_mem_free(buf); } return; } */ if(*ptr != '#' && *ptr != '/'){ char errstr[128]; snprintf(errstr, 128, "invalid packet: packet does not begin with a # or a /"); t_osc_bndl_u *b = osc_bundle_u_alloc(); t_osc_msg_u *merr = osc_message_u_alloc(); osc_message_u_setAddress(merr, "/error/str"); osc_message_u_appendString(merr, errstr); osc_bundle_u_addMsg(b, merr); t_osc_bndl_s *bs = osc_bundle_u_serialize(b); if(bs){ omax_util_outletOSC(x->outletErr, osc_bundle_s_getLen(bs), osc_bundle_s_getPtr(bs)); omax_util_outletOSC(x->outletInval, len, ptr); osc_bundle_s_deepFree(bs); } return; } if(len % 4){ char errstr[128]; snprintf(errstr, 128, "%ld is not a multiple of 4 bytes", len); t_osc_bndl_u *b = osc_bundle_u_alloc(); t_osc_msg_u *merr = osc_message_u_alloc(); osc_message_u_setAddress(merr, "/error/str"); osc_message_u_appendString(merr, errstr); osc_bundle_u_addMsg(b, merr); t_osc_bndl_s *bs = osc_bundle_u_serialize(b); if(bs){ omax_util_outletOSC(x->outletErr, osc_bundle_s_getLen(bs), osc_bundle_s_getPtr(bs)); omax_util_outletOSC(x->outletInval, len, ptr); osc_bundle_s_deepFree(bs); } return; } if(*ptr == '#'){ char *p = ptr; p += OSC_HEADER_SIZE; while((p - ptr) < (len - 4)){ int i = ntoh32(*((int32_t *)p)); if(i < 0){ break; } p += i + 4; } if((p - ptr) != len){ char errstr[128]; snprintf(errstr, 128, "expected %ld bytes, but found %d", len, p - ptr); t_osc_bndl_u *b = osc_bundle_u_alloc(); t_osc_msg_u *merr = osc_message_u_alloc(); osc_message_u_setAddress(merr, "/error/str"); osc_message_u_appendString(merr, errstr); osc_bundle_u_addMsg(b, merr); t_osc_bndl_s *bs = osc_bundle_u_serialize(b); if(bs){ omax_util_outletOSC(x->outletErr, osc_bundle_s_getLen(bs), osc_bundle_s_getPtr(bs)); omax_util_outletOSC(x->outletInval, len, ptr); osc_bundle_s_deepFree(bs); } return; } } uint64_t state = OSC_SERIAL_INIT; for(int i = 0; i < len; i++){ state = osc_serial_processByte(ptr[i], state); if(osc_serial_errorp(state)){ char *errstr = osc_serial_errstr(state); t_osc_bndl_u *b = osc_bundle_u_alloc(); t_osc_msg_u *merr = osc_message_u_alloc(); osc_message_u_setAddress(merr, "/error/str"); osc_message_u_appendString(merr, errstr); osc_bundle_u_addMsg(b, merr); t_osc_msg_u *mbytenum = osc_message_u_alloc(); osc_message_u_setAddress(mbytenum, "/error/byte/num"); osc_message_u_appendInt32(mbytenum, i); osc_bundle_u_addMsg(b, mbytenum); if(mbytenum < len){ t_osc_msg_u *mbyteval = osc_message_u_alloc(); osc_message_u_setAddress(mbyteval, "/error/byte/val"); osc_message_u_appendInt32(mbyteval, ptr[i]); osc_bundle_u_addMsg(b, mbyteval); } t_osc_bndl_s *bs = osc_bundle_u_serialize(b); if(bs){ omax_util_outletOSC(x->outletErr, osc_bundle_s_getLen(bs), osc_bundle_s_getPtr(bs)); omax_util_outletOSC(x->outletInval, len, ptr); osc_bundle_s_deepFree(bs); } return; } } omax_util_outletOSC(x->outletVal, len, ptr); }
static t_osc_err osc_bundle_u_flatten_impl(t_osc_bndl_u **dest, t_osc_bndl_u *src, int maxlevel, int level, char *prefix, char *sep, int remove_enclosing_address_if_empty) { if(!sep){ sep = ""; } if(!(*dest)){ *dest = osc_bundle_u_alloc(); } t_osc_bndl_it_u *bit = osc_bndl_it_u_get(src); while(osc_bndl_it_u_hasNext(bit)){ t_osc_msg_u *m = osc_bndl_it_u_next(bit); t_osc_msg_u *mcopy = NULL; osc_message_u_deepCopy(&mcopy, m); if((level < maxlevel) || (maxlevel <= 0)){ t_osc_msg_it_u *mit = osc_msg_it_u_get(mcopy); while(osc_msg_it_u_hasNext(mit)){ t_osc_atom_u *a = osc_msg_it_u_next(mit); if(osc_atom_u_getTypetag(a) == OSC_BUNDLE_TYPETAG){ osc_message_u_removeAtom(mcopy, a); t_osc_bndl_u *bu = osc_atom_u_getBndl(a); /* t_osc_bndl_u *bu = NULL; osc_bundle_s_deserialize(osc_bundle_s_getLen(b), osc_bundle_s_getPtr(b), &bu); */ t_osc_err e; if(prefix){ int prefixlen = strlen(prefix) + strlen(osc_message_u_getAddress(mcopy)) + strlen(sep); char pfx[prefixlen + 1]; sprintf(pfx, "%s%s%s", prefix, sep, osc_message_u_getAddress(mcopy)); e = osc_bundle_u_flatten_impl(dest, bu, maxlevel, level + 1, pfx, sep, remove_enclosing_address_if_empty); }else{ e = osc_bundle_u_flatten_impl(dest, bu, maxlevel, level + 1, osc_message_u_getAddress(mcopy), sep, remove_enclosing_address_if_empty); } if(e){ return e; } osc_atom_u_free(a); } } osc_msg_it_u_destroy(mit); } if(!remove_enclosing_address_if_empty || osc_message_u_getArgCount(mcopy) > 0){ if(prefix){ int newaddresslen = strlen(prefix) + strlen(osc_message_u_getAddress(mcopy)) + strlen(sep); char newaddress[newaddresslen + 1]; sprintf(newaddress, "%s%s%s", prefix, sep, osc_message_u_getAddress(mcopy)); osc_message_u_setAddress(mcopy, newaddress); } osc_bundle_u_addMsgWithoutDups(*dest, mcopy); }else{ osc_message_u_free(mcopy); } } osc_bndl_it_u_destroy(bit); return OSC_ERR_NONE; }
int main(int argc, char **argv) { // create a bundle and add messages to it t_osc_bndl_u *bndl_u = osc_bundle_u_alloc(); t_osc_msg_u *m1 = osc_message_u_alloc(); osc_message_u_setAddress(m1, "/foo"); osc_message_u_appendFloat(m1, 3.14); osc_bundle_u_addMsg(bndl_u, m1); t_osc_msg_u *m2 = osc_message_u_allocWithString("/bar", "whatevs"); osc_bundle_u_addMsg(bndl_u, m2); t_osc_msg_u *m3 = osc_message_u_allocWithAddress("/bloo"); t_osc_atom_u *a = osc_atom_u_allocWithInt32(12); osc_message_u_appendAtom(m3, a); osc_bundle_u_addMsg(bndl_u, m3); // serialize the bundle long len = osc_bundle_u_nserialize(NULL, 0, bndl_u); char bndl_s[len]; osc_bundle_u_nserialize(bndl_s, len, bndl_u); // free the original unserialized bundle osc_bundle_u_free(bndl_u); bndl_u = NULL; // deserialize the serialized bundle osc_bundle_s_deserialize(len, bndl_s, &bndl_u); // iterate over messages in a serialized bundle t_osc_bndl_it_s *b_it_s = osc_bndl_it_s_get(len, bndl_s); while(osc_bndl_it_s_hasNext(b_it_s)){ t_osc_msg_s *m = osc_bndl_it_s_next(b_it_s); printf("%s\n", osc_message_s_getAddress(m)); } osc_bndl_it_s_destroy(b_it_s); // turn a serialized bundle into printable text long tlen = osc_bundle_s_nformat(NULL, 0, len, bndl_s, 0); char text[tlen + 1]; osc_bundle_s_nformat(text, tlen, len, bndl_s, 0); printf("\nBUNDLE:\n"); printf("%s\n", text); printf("\n"); // turn text into an unserialized bundle t_osc_bndl_u *bndl_u_2 = NULL; char *text2 = "/jean : [1, 2, 3], /john : 6.66, /jeremy : \"is cool\""; osc_parser_parseString(strlen(text2), text2, &bndl_u_2); // iterate over messages in an unserialized bundle t_osc_bndl_it_u *b_it_u = osc_bndl_it_u_get(bndl_u_2); while(osc_bndl_it_u_hasNext(b_it_u)){ t_osc_msg_u *m = osc_bndl_it_u_next(b_it_u); printf("%s has typetags ", osc_message_u_getAddress(m)); // iterate over atoms in list t_osc_msg_it_u *m_it_u = osc_msg_it_u_get(m); while(osc_msg_it_u_hasNext(m_it_u)){ t_osc_atom_u *a = osc_msg_it_u_next(m_it_u); printf("%c", osc_atom_u_getTypetag(a)); } osc_msg_it_u_destroy(m_it_u); printf("\n"); } osc_bndl_it_u_destroy(b_it_u); }
void _omax_doc_outletDoc(void *outlet, char *name, char *short_desc, char *long_desc, int ninlets, char **inlets_desc, int noutlets, char **outlets_desc, int num_see_also_refs, char **see_also) { t_osc_bndl_u *bndl = osc_bundle_u_alloc(); t_osc_msg_u *msg_name = osc_message_u_alloc(); osc_message_u_setAddress(msg_name, "/doc/name"); osc_message_u_appendString(msg_name, name); osc_bundle_u_addMsg(bndl, msg_name); t_osc_msg_u *msg_short_desc = osc_message_u_alloc(); osc_message_u_setAddress(msg_short_desc, "/doc/desc/short"); osc_message_u_appendString(msg_short_desc, short_desc); osc_bundle_u_addMsg(bndl, msg_short_desc); t_osc_msg_u *msg_long_desc = osc_message_u_alloc(); osc_message_u_setAddress(msg_long_desc, "/doc/desc/long"); osc_message_u_appendString(msg_long_desc, long_desc); osc_bundle_u_addMsg(bndl, msg_long_desc); t_osc_msg_u *msg_ninlets = osc_message_u_alloc(); osc_message_u_setAddress(msg_ninlets, "/doc/ninlets"); osc_message_u_appendInt32(msg_ninlets, ninlets); osc_bundle_u_addMsg(bndl, msg_ninlets); t_osc_msg_u *msg_noutlets = osc_message_u_alloc(); osc_message_u_setAddress(msg_noutlets, "/doc/noutlets"); osc_message_u_appendInt32(msg_noutlets, noutlets); osc_bundle_u_addMsg(bndl, msg_noutlets); int i; for(i = 0; i < ninlets; i++){ if(inlets_desc[i]){ t_osc_msg_u *m = osc_message_u_alloc(); char buf[64]; sprintf(buf, "/doc/desc/inlet/%d", i + 1); osc_message_u_setAddress(m, buf); osc_message_u_appendString(m, inlets_desc[i]); osc_bundle_u_addMsg(bndl, m); } } for(i = 0; i < noutlets; i++){ if(outlets_desc[i]){ t_osc_msg_u *m = osc_message_u_alloc(); char buf[64]; sprintf(buf, "/doc/desc/outlet/%d", i + 1); osc_message_u_setAddress(m, buf); osc_message_u_appendString(m, outlets_desc[i]); osc_bundle_u_addMsg(bndl, m); } } t_osc_msg_u *msg_seealso = osc_message_u_alloc(); osc_message_u_setAddress(msg_seealso, "/doc/seealso"); for(i = 0; i < num_see_also_refs; i++){ osc_message_u_appendString(msg_seealso, see_also[i]); } osc_bundle_u_addMsg(bndl, msg_seealso); //long len = 0; //char *bndl_s = NULL; t_osc_bndl_s *bs = osc_bundle_u_serialize(bndl); if(bs){ omax_util_outletOSC(outlet, osc_bundle_s_getLen(bs), osc_bundle_s_getPtr(bs)); osc_bundle_s_deepFree(bs); } if(bndl){ osc_bundle_u_free(bndl); } }