Skip to content

Commit 1d5dfc8

Browse files
committed
Found/fixed 2 bugs: One was null pointer after o2_service_free, and one was not finding/freeing the port in o2_osc_port_free. Extended oscsendtest and oscrecvtest to test the o2_osc_port_free fix.
1 parent 97931e5 commit 1d5dfc8

7 files changed

Lines changed: 64 additions & 7 deletions

File tree

src/o2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ int o2_service_provider_new(o2string service_name, o2_info_ptr service,
644644
O2_DBd(printf("%s o2_service_provider_new service exists %s\n",
645645
o2_debug_prefix, service_name));
646646
// however, the tappee might be new or different
647-
if (*tappee) {
647+
if (tappee) {
648648
return o2_set_tap(tappee, service_name);
649649
}
650650
return O2_SERVICE_EXISTS;

src/o2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ int o2_tap(const char *tappee, const char *tapper);
819819
*
820820
* @return #O2_SUCCSS if success, #O2_FAIL if not.
821821
*/
822-
int o2_service_free(char *service_name);
822+
int o2_service_free(const char *service_name);
823823

824824

825825
/**

src/o2_interoperation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int o2_osc_port_free(int port_num)
105105
info->osc.service_name = NULL;
106106
}
107107
o2_socket_mark_to_free(info);
108-
result = O2_SUCCESS; // actual found and removed a port
108+
result = O2_SUCCESS; // actually found and removed a port
109109
}
110110
}
111111
if (service_name_copy) O2_FREE((void *)service_name_copy);

src/o2_search.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,11 @@ int o2_set_tap(const char *tappee, const char *tapper_name)
839839

840840
// remove a service from o2_context->path_tree
841841
//
842-
int o2_service_free(char *service_name)
842+
int o2_service_free(const char *service_name)
843843
{
844+
if (!o2_application_name) {
845+
return O2_NOT_INITIALIZED;
846+
}
844847
if (!service_name || strchr(service_name, '/'))
845848
return O2_BAD_SERVICE_NAME;
846849
return o2_service_provider_replace(o2_context->process, service_name, NULL);

src/o2_socket.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ int o2_make_udp_recv_socket(int tag, int *port, process_info_ptr *info)
439439
O2_DBo(printf("%s created socket %ld and bind called to receive UDP\n",
440440
o2_debug_prefix, (long) sock));
441441
*info = o2_add_new_socket(sock, tag, &udp_recv_handler);
442+
(*info)->port = *port;
442443
// printf("%s: o2_make_udp_recv_socket: listening on port %d\n", o2_debug_prefix, o2_context->process.port);
443444
return O2_SUCCESS;
444445
}

test/oscrecvtest.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// oscrecvtest.c - test o2_osc_port_new()
22
//
33
// this test is designed to run with oscsendtest.c
4+
// see oscsendtest.c for details
45

56

67
#include "stdio.h"
@@ -43,6 +44,12 @@ void osc_i_handler(o2_msg_data_ptr data, const char *types,
4344
#endif
4445
assert(approx(timed_start + i * 0.1 - now));
4546
timed_count++;
47+
} else if (i == 5678) {
48+
printf("osc_i_handler received 5678 but port should be closed.\n");
49+
assert(FALSE);
50+
} else if (i == 6789) {
51+
printf("osc_i_handler received 6789 but port should be closed.\n");
52+
assert(FALSE);
4653
} else {
4754
assert(FALSE); // unexpected message
4855
}
@@ -76,6 +83,13 @@ int main(int argc, const char * argv[])
7683
usleep(2000); // 2ms
7784
}
7885
o2_osc_port_free(8100);
86+
87+
// now wait for 2 seconds and check for more messages
88+
for (int i = 0; i < 1000; i++) {
89+
o2_poll();
90+
usleep(2000); // 2ms
91+
}
92+
7993
o2_finish();
8094
printf("OSCRECV DONE\n");
8195
sleep(1); // allow TCP to finish up

test/oscsendtest.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
// oscsendtest.c - test o2_osc_delegate()
1+
// oscsendtest.c - test o2_osc_delegate()
22
//
3-
// this test is designed to run with oscrecvtest.c
3+
// this test is designed to run with oscrecvtest.c
4+
//
5+
// Usage: oscsendtest [flags] (see o2.h for flags,
6+
// use a for all, also u for UDP, M for master)
7+
//
8+
// The test:
9+
// initialize as a clock slave or master depending on M flag
10+
// if we are master, assume we are talking to a liblo server,
11+
// so sleep 2 seconds allowing liblo server to launch
12+
// (you should launch it first if running manually)
13+
// send 12 messages, 1 every 0.5s, and stop,
14+
// message are /oscsend/i 1234
15+
// send 10 messages with timestamps,
16+
// messages are /oscsend/i <2000+i>
17+
// reciever can now call o2_osc_port_free to test closing the port
18+
// wait 1 second
19+
// send 1 message: /oscsend/i 5678
20+
// wait 0.1 seconds
21+
// send 1 message with timestamp: /oscsend/i 6789
22+
// wait 1 second
23+
// shut everything down
424

525
#include "o2.h"
626
#include "stdio.h"
@@ -33,7 +53,7 @@ int main(int argc, const char * argv[])
3353

3454
o2_initialize("test");
3555

36-
// you can make this run without an O2 server by passing "master"
56+
// you can make this run without an O2 server by passing "M" flag
3757
if (master)
3858
o2_clock_set(NULL, NULL);
3959
else if (argc > 2)
@@ -70,6 +90,25 @@ int main(int argc, const char * argv[])
7090
o2_poll();
7191
usleep(2000); // 2ms
7292
}
93+
// receiver may want to close port now and check that these
94+
printf("Time to close receiver's port if you want to test that.\n");
95+
// messages are NOT received:
96+
err = o2_send("/oscsend/i", 0, "i", 5678);
97+
assert(err == O2_SUCCESS);
98+
printf("sent 5678 to /oscsend/i\n");
99+
// pause for 0.1s, but keep running O2 by polling
100+
for (int i = 0; i < 50; i++) {
101+
o2_poll();
102+
usleep(2000); // 2ms
103+
}
104+
double ts = o2_time_get() + 0.1;
105+
o2_send("/oscsend/i", ts, "i", 6789);
106+
printf("sent 6789 to /oscsend/i with timestamp %g\n", ts);
107+
// pause for 1s, but keep running O2 by polling
108+
for (int i = 0; i < 500; i++) {
109+
o2_poll();
110+
usleep(2000); // 2ms
111+
}
73112
o2_service_free("oscsend");
74113
o2_finish();
75114
sleep(1); // finish closing sockets

0 commit comments

Comments
 (0)