void port_checker::server::on_recv(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg) { (void)argv; // std::cout << "on_recv: " << path << ", " << waiting << std::endl; // for(const char** exp_path = exp_paths; *exp_path; // ++exp_path, ++_replied_path) // std::cout << " - exp: " << *exp_path << std::endl; if(waiting && exp_paths && exp_paths[0]) { _replied_path = 0; for(const char** exp_path = exp_paths; *exp_path; ++exp_path, ++_replied_path) if(!strcmp(*exp_path, path)) { size_t len = lo_message_length(msg, path); *last_buffer = std::vector<char>(len); size_t written; lo_message_serialise(msg, path, last_buffer->data(), &written); if(written > last_buffer->size()) // ouch... throw std::runtime_error("can not happen, " "lo_message_length has been used"); last_args->resize(argc); for(int i = 0; i < argc; ++i) { (*last_args)[i].val = rtosc_argument(last_buffer->data(), i); (*last_args)[i].type = types[i]; } waiting = false; break; } } }
/* catch any osc incoming messages. */ int OscToShmdata::osc_handler(const char *path, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, lo_message m, void *user_data) { OscToShmdata *context = static_cast<OscToShmdata *>(user_data); lo_timetag timetag = lo_message_get_timestamp(m); // g_print ("timestamp %u %u", path, timetag.sec, timetag.frac); if (0 != timetag.sec) { // FIXME handle internal timetag // note: this is not implemented in osc-send } size_t size; void *buftmp = lo_message_serialise(m, path, nullptr, &size); if (context->shm_->writer(&shmdata::Writer::alloc_size) < size) { context->shm_.reset(nullptr); context->shm_.reset(new ShmdataWriter(context, context->make_file_name("osc"), size, "application/x-libloserialized-osc")); } context->shm_->writer(&shmdata::Writer::copy_to_shm, buftmp, size); context->shm_->bytes_written(size); g_free(buftmp); return 0; }
void *lo_bundle_serialise(lo_bundle b, void *to, size_t *size) { size_t s, skip; int32_t *bes; int i; char *pos; lo_pcast32 be; if (!b) { return NULL; } s = lo_bundle_length(b); if (size) { *size = s; } if (!to) { to = calloc(1, s); } pos = to; strcpy(pos, "#bundle"); pos += 8; be.nl = lo_htoo32(b->ts.sec); memcpy(pos, &be, 4); pos += 4; be.nl = lo_htoo32(b->ts.frac); memcpy(pos, &be, 4); pos += 4; for (i = 0; i < b->len; i++) { lo_message_serialise(b->msgs[i], b->paths[i], pos + 4, &skip); bes = (int32_t *)pos; *bes = lo_htoo32(skip); pos += skip + 4; if (pos > (char *)to + s) { fprintf(stderr, "liblo: data integrity error at message %d\n", i); return NULL; } } if (pos != to + s) { fprintf(stderr, "liblo: data integrity error\n"); return NULL; } return to; }
int lo_send_message_from(lo_address a, lo_server from, const char *path, lo_message msg) { const size_t data_len = lo_message_length(msg, path); char *data = lo_message_serialise(msg, path, NULL, NULL); // Send the message int ret = send_data( a, from, data, data_len ); // Free the memory allocated by lo_message_serialise if (data) free( data ); return ret; }
int lo_send_message_from(lo_address a, lo_server from, const char *path, lo_message msg) { const size_t data_len = lo_message_length(msg, path); char *data = lo_message_serialise(msg, path, NULL, NULL); // Send the message int ret = send_data( a, from, data, data_len ); // For TCP, retry once if it failed. The first try will return // error if the connection was closed, so the second try will // attempt to re-open the connection. if (ret == -1 && a->protocol == LO_TCP) ret = send_data( a, from, data, data_len ); // Free the memory allocated by lo_message_serialise if (data) free( data ); return ret; }
void ModuleManager::deleteModuleList() { int sock, n, d_len; struct sockaddr_in addr; void *data; char path[] = "/ModuleList/deleteMList"; char p[64]; int mColor; strcpy(p, OSCAddr); strcat(p, mPath); //create lo_message lo_message m = lo_message_new(); lo_message_add_string(m, IPAddr); lo_message_add_string(m, p); lo_message_add_int32(m, mColor); data = lo_message_serialise(m, path, NULL, NULL); d_len = lo_message_length(m, path); //create socket int opt = 1; sock = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int)); addr.sin_family = AF_INET; addr.sin_port = htons(6340); inet_pton(AF_INET, "255.255.255.255", &addr.sin_addr.s_addr); //send(念のため3回) for (int j=0; j<3; j++) { n = sendto(sock, data, d_len, 0, (struct sockaddr *)&addr, sizeof(addr)); if (n < 1) { perror("sendto"); } usleep(1000); } lo_message_free(m); close(sock); }
int main() { //clean buffer memset(buffer1, 0xc3, sizeof(buffer1)); //generate liblo message 1 size_t len = 128; lo_message message = lo_message_new(); assert_non_null(message, "Generating A Liblo Message 1 (int/float)", __LINE__); lo_message_add_float(message, 24.0); lo_message_add_int32(message, 42); lo_message_serialise(message, "/path", buffer1, &len); assert_str_eq("/path", buffer1, "Verifying Path From Message 1", __LINE__); assert_f32_eq(24.0f, rtosc_argument(buffer1, 0).f, "Verifying Float From Message 1", __LINE__); assert_int_eq(42, rtosc_argument(buffer1, 1).i, "Verifying Int From Message 1", __LINE__); assert_int_eq(20, rtosc_message_length(buffer1, 128), "Verifying Length From Message 1", __LINE__); //Message 2 size_t len2 = rtosc_message(buffer2, 1024, "/li", "bb", 4, buffer1, 4, buffer1); assert_int_eq(24, len2, "Generate A Rtosc Message 2 (bb)", __LINE__); lo_message msg2 = lo_message_deserialise((void*)buffer2, len2, &result_var); if(assert_non_null(msg2, "Deserialize Message 2 To Liblo", __LINE__)) printf("# Liblo Did Not Accept the Rtosc Message [error '%d']\n", result_var); //Message 3 size_t len3 = rtosc_message(buffer3+4, 2048, "/close-ui", ""); assert_int_eq(16, len3, "Generate A Rtosc Message 3 ()", __LINE__); lo_message msg3 = lo_message_deserialise((void*)(buffer3+4), len3, &result_var); if(assert_non_null(msg2, "Deserialize Message 3 To Liblo", __LINE__)) printf("#Liblo Did Not Accept the Rtosc Message [error '%d']\n", result_var); //Bundle 4 size_t len4 = rtosc_bundle(buffer4, 2048, 0xdeadbeefcafebaad, 3, buffer1, buffer2, buffer3+4); assert_int_eq(88, len4, "Generate A Bundle 4", __LINE__); //Bundle 5 lo_timetag time; time.sec = 0xdeadbeef; time.frac = 0xcafebaad; lo_bundle ms4 = lo_bundle_new(time); lo_bundle_add_message(ms4, "/path", message); lo_bundle_add_message(ms4, "/li", msg2); lo_bundle_add_message(ms4, "/close-ui", msg3); size_t len5 = 2048; lo_bundle_serialise(ms4,(void*)buffer5, &len5); //Verify 4 == 5 assert_non_null(ms4, "Generate A Liblo Bundle 5", __LINE__); assert_hex_eq(buffer5, buffer4, len5, len4, "Verify Liblo Style Bundles", __LINE__); //Cleanup lo_message_free(message); lo_message_free(msg2); lo_message_free(msg3); lo_bundle_free(ms4); return test_summary(); }
void ModuleController::sendModuleList() { int sock, n, d_len; struct sockaddr_in addr; void *data; char path[] = "/ModuleList/setMList"; char p[64]; int mColor; strcpy(p, OSCAddr); for (int i=0; i<7; i++) { switch (i) { case 0: strcpy(p, OSCAddr); strcat(p, "/SP/DAC"); mColor = 1; break; case 1: strcpy(p, OSCAddr); strcat(p, "/GN/ADC"); mColor = 2; break; case 2: strcpy(p, OSCAddr); strcat(p, "/GN/Sine"); mColor = 3; break; case 3: strcpy(p, OSCAddr); strcat(p, "/EF/Envelope"); mColor = 4; break; case 4: strcpy(p, OSCAddr); strcat(p, "/GN/AudioSource"); mColor = 5; break; case 5: strcpy(p, OSCAddr); strcat(p, "/EF/Delay"); mColor = 6; break; case 6: strcpy(p, OSCAddr); strcat(p, "/GN/AudioClock"); mColor = 7; break; default: break; } //create lo_message lo_message m = lo_message_new(); lo_message_add_string(m, IPAddr); lo_message_add_string(m, p); lo_message_add_int32(m, mColor); data = lo_message_serialise(m, path, NULL, NULL); d_len = lo_message_length(m, path); //create socket int opt = 1; sock = socket(AF_INET, SOCK_DGRAM, 0); setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int)); addr.sin_family = AF_INET; addr.sin_port = htons(6340); inet_pton(AF_INET, "255.255.255.255", &addr.sin_addr.s_addr); //send(念のため3回) for (int j=0; j<3; j++) { n = sendto(sock, data, d_len, 0, (struct sockaddr *)&addr, sizeof(addr)); if (n < 1) { perror("sendto"); } usleep(1000); } lo_message_free(m); } close(sock); }
void test_deserialise() { char *buf, *buf2, *tmp; const char *types = NULL, *path; lo_arg **argv = NULL; size_t len, size; char data[256]; int result = 0; lo_blob btest = lo_blob_new(sizeof(testdata), testdata); uint8_t midi_data[4] = {0xff, 0xf7, 0xAA, 0x00}; lo_timetag tt = {0x1, 0x80000000}; lo_blob b = NULL; // build a message lo_message msg = lo_message_new(); TEST(0 == lo_message_get_argc(msg)); lo_message_add_float(msg, 0.12345678f); // 0 f lo_message_add_int32(msg, 123); // 1 i lo_message_add_string(msg, "123"); // 2 s lo_message_add_blob(msg, btest); // 3 b lo_message_add_midi(msg, midi_data); // 4 m lo_message_add_int64(msg, 0x0123456789abcdefULL); // 5 h lo_message_add_timetag(msg, tt); // 6 t lo_message_add_double(msg, 0.9999); // 7 d lo_message_add_symbol(msg, "sym"); // 8 S lo_message_add_char(msg, 'X'); // 9 c lo_message_add_char(msg, 'Y'); // 10 c lo_message_add_true(msg); // 11 T lo_message_add_false(msg); // 12 F lo_message_add_nil(msg); // 13 N lo_message_add_infinitum(msg); // 14 I // test types, args TEST(15 == lo_message_get_argc(msg)); types = lo_message_get_types(msg); TEST(NULL != types); argv = lo_message_get_argv(msg); TEST(NULL != argv); TEST('f' == types[0] && fabs(argv[0]->f - 0.12345678f) < FLT_EPSILON); TEST('i' == types[1] && 123 == argv[1]->i); TEST('s' == types[2] && !strcmp(&argv[2]->s, "123")); TEST('b' == types[3]); b = (lo_blob)argv[3]; TEST(lo_blob_datasize(b) == sizeof(testdata)); TEST(12 == lo_blobsize(b)); TEST(!memcmp(lo_blob_dataptr(b), &testdata, sizeof(testdata))); TEST('m' == types[4] && !memcmp(&argv[4]->m, midi_data, 4)); TEST('h' == types[5] && 0x0123456789abcdefULL == argv[5]->h); TEST('t' == types[6] && 1 == argv[6]->t.sec && 0x80000000 == argv[6]->t.frac); TEST('d' == types[7] && fabs(argv[7]->d - 0.9999) < FLT_EPSILON); TEST('S' == types[8] && !strcmp(&argv[8]->s, "sym")); TEST('c' == types[9] && 'X' == argv[9]->c); TEST('c' == types[10] && 'Y' == argv[10]->c); TEST('T' == types[11] && NULL == argv[11]); TEST('F' == types[12] && NULL == argv[12]); TEST('N' == types[13] && NULL == argv[13]); TEST('I' == types[14] && NULL == argv[14]); // serialise it len = lo_message_length(msg, "/foo"); printf("serialise message_length=%d\n", (int)len); buf = calloc(len, sizeof(char)); size = 0; tmp = lo_message_serialise(msg, "/foo", buf, &size); TEST(tmp == buf && size == len && 92 == len); lo_message_free(msg); // deserialise it printf("deserialise\n"); path = lo_get_path(buf, len); TEST(NULL != path && !strcmp(path, "/foo")); msg = lo_message_deserialise(buf, size, NULL); TEST(NULL != msg); // repeat same test as above TEST(15 == lo_message_get_argc(msg)); types = lo_message_get_types(msg); TEST(NULL != types); argv = lo_message_get_argv(msg); TEST(NULL != argv); TEST('f' == types[0] && fabs(argv[0]->f - 0.12345678f) < FLT_EPSILON); TEST('i' == types[1] && 123 == argv[1]->i); TEST('s' == types[2] && !strcmp(&argv[2]->s, "123")); TEST('b' == types[3]); b = (lo_blob)argv[3]; TEST(lo_blob_datasize(b) == sizeof(testdata)); TEST(12 == lo_blobsize(b)); TEST(!memcmp(lo_blob_dataptr(b), &testdata, sizeof(testdata))); TEST('m' == types[4] && !memcmp(&argv[4]->m, midi_data, 4)); TEST('h' == types[5] && 0x0123456789abcdefULL == argv[5]->h); TEST('t' == types[6] && 1 == argv[6]->t.sec && 0x80000000 == argv[6]->t.frac); TEST('d' == types[7] && fabs(argv[7]->d - 0.9999) < FLT_EPSILON); TEST('S' == types[8] && !strcmp(&argv[8]->s, "sym")); TEST('c' == types[9] && 'X' == argv[9]->c); TEST('c' == types[10] && 'Y' == argv[10]->c); TEST('T' == types[11] && NULL == argv[11]); TEST('F' == types[12] && NULL == argv[12]); TEST('N' == types[13] && NULL == argv[13]); TEST('I' == types[14] && NULL == argv[14]); // serialise it again, compare len = lo_message_length(msg, "/foo"); printf("serialise message_length=%d\n", (int)len); buf2 = calloc(len, sizeof(char)); size = 0; tmp = lo_message_serialise(msg, "/foo", buf2, &size); TEST(tmp == buf2 && size == len && 92 == len); TEST(!memcmp(buf, buf2, len)); lo_message_free(msg); lo_blob_free(btest); free(buf); free(buf2); // deserialise failure tests with invalid message data msg = lo_message_deserialise(data, 0, &result); // 0 size TEST(NULL == msg && LO_ESIZE == result); snprintf(data, 256, "%s", "/foo"); // unterminated path string msg = lo_message_deserialise(data, 4, &result); TEST(NULL == msg && LO_EINVALIDPATH == result); snprintf(data, 256, "%s", "/f_o"); // non-0 in pad area msg = lo_message_deserialise(data, 4, &result); TEST(NULL == msg && LO_EINVALIDPATH == result); snprintf(data, 256, "%s", "/t__"); // types missing replace_char(data, 4, '_', '\0'); msg = lo_message_deserialise(data, 4, &result); TEST(NULL == msg && LO_ENOTYPE == result); snprintf(data, 256, "%s%s", "/t__", "____"); // types empty replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 8, &result); TEST(NULL == msg && LO_EBADTYPE == result); snprintf(data, 256, "%s%s", "/t__", ",f_"); // short message replace_char(data, 7, '_', '\0'); msg = lo_message_deserialise(data, 7, &result); TEST(NULL == msg && LO_EINVALIDTYPE == result); snprintf(data, 256, "%s%s", "/t__", "ifi_"); // types missing comma replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 8, &result); TEST(NULL == msg && LO_EBADTYPE == result); snprintf(data, 256, "%s%s", "/t__", ",ifi"); // types unterminated replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 8, &result); TEST(NULL == msg && LO_EINVALIDTYPE == result); snprintf(data, 256, "%s%s", "/t__", ",ii_"); // not enough arg data replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 12, &result); TEST(NULL == msg && LO_EINVALIDARG == result); snprintf(data, 256, "%s%s", "/t__", ",ii_"); // not enough arg data again replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 15, &result); TEST(NULL == msg && LO_EINVALIDARG == result); snprintf(data, 256, "%s%s", "/t__", ",f__"); // too much arg data replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 16, &result); TEST(NULL == msg && LO_ESIZE == result); snprintf(data, 256, "%s%s", "/t__", ",bs_"); // blob longer than msg length replace_char(data, 8, '_', '\0'); *(uint32_t *)(data + 8) = lo_htoo32((uint32_t)99999); msg = lo_message_deserialise(data, 256, &result); TEST(NULL == msg && LO_EINVALIDARG == result); }