Mercurial > hg > plosc
comparison c/plosc.c @ 4:8a71c55816be
Put server loop timeout in server structure and set to 50ms default.
author | samer |
---|---|
date | Mon, 20 Feb 2012 14:31:35 +0000 |
parents | 44e4b80bdf97 |
children |
comparison
equal
deleted
inserted
replaced
3:44e4b80bdf97 | 4:8a71c55816be |
---|---|
27 // Prolog from the server thread. | 27 // Prolog from the server thread. |
28 | 28 |
29 typedef struct _my_server_thread { | 29 typedef struct _my_server_thread { |
30 lo_server s; | 30 lo_server s; |
31 pthread_t thread; | 31 pthread_t thread; |
32 int timeout; | |
32 volatile int active; | 33 volatile int active; |
33 volatile int done; | 34 volatile int done; |
34 } *my_server_thread; | 35 } *my_server_thread; |
35 | 36 |
36 int my_server_thread_start(my_server_thread st); | 37 int my_server_thread_start(my_server_thread st); |
37 int my_server_thread_stop(my_server_thread st); | 38 int my_server_thread_stop(my_server_thread st); |
38 int my_server_thread_run(my_server_thread st, int timeout); | 39 int my_server_thread_run(my_server_thread st); |
39 void my_server_thread_free(my_server_thread st); | 40 void my_server_thread_free(my_server_thread st); |
40 my_server_thread my_server_thread_new(const char *port, lo_err_handler err_h); | 41 my_server_thread my_server_thread_new(const char *port, lo_err_handler err_h); |
41 | 42 |
42 // --------------------------------------------------------------------------- | 43 // --------------------------------------------------------------------------- |
43 | 44 |
692 } | 693 } |
693 */ | 694 */ |
694 | 695 |
695 // run OSC server in this thread but with an extra message handler | 696 // run OSC server in this thread but with an extra message handler |
696 // to allow the /plosc/stop message to terminate the loop. | 697 // to allow the /plosc/stop message to terminate the loop. |
697 static int run_stoppable_server(my_server_thread s, int timeout) | 698 static int run_stoppable_server(my_server_thread s) |
698 { | 699 { |
699 lo_server_add_method(s->s, "/plosc/stop", NULL, stop_handler, (void *)s); | 700 lo_server_add_method(s->s, "/plosc/stop", NULL, stop_handler, (void *)s); |
700 my_server_thread_run(s,timeout); | 701 my_server_thread_run(s); |
701 lo_server_del_method(s->s,"/plosc/stop",NULL); | 702 lo_server_del_method(s->s,"/plosc/stop",NULL); |
702 return TRUE; | 703 return TRUE; |
703 } | 704 } |
704 | 705 |
705 foreign_t mk_server(term_t port, term_t server) | 706 foreign_t mk_server(term_t port, term_t server) |
782 | 783 |
783 foreign_t run_server( term_t server) | 784 foreign_t run_server( term_t server) |
784 { | 785 { |
785 my_server_thread s; | 786 my_server_thread s; |
786 printf("running OSC server synchronously...\n"); | 787 printf("running OSC server synchronously...\n"); |
787 return get_server(server,&s) && run_stoppable_server(s,10); | 788 return get_server(server,&s) && run_stoppable_server(s); |
788 } | 789 } |
789 | 790 |
790 | 791 |
791 // ------------------------------------------------------------------------- | 792 // ------------------------------------------------------------------------- |
792 // my_server_thread implementation | 793 // my_server_thread implementation |
793 | 794 |
794 my_server_thread my_server_thread_new(const char *port, lo_err_handler err_h) | 795 my_server_thread my_server_thread_new(const char *port, lo_err_handler err_h) |
795 { | 796 { |
796 my_server_thread st = malloc(sizeof(struct _my_server_thread)); | 797 my_server_thread st = malloc(sizeof(struct _my_server_thread)); |
797 st->s = lo_server_new(port, err_h); | 798 st->s = lo_server_new(port, err_h); |
799 st->timeout= 100; // will check st->active at 10Hz. | |
798 st->active = 0; | 800 st->active = 0; |
799 st->done = 0; | 801 st->done = 0; |
800 | 802 |
801 if (!st->s) { | 803 if (!st->s) { |
802 free(st); | 804 free(st); |
852 | 854 |
853 } | 855 } |
854 return 0; | 856 return 0; |
855 } | 857 } |
856 | 858 |
857 int my_server_thread_run(my_server_thread st, int timeout) | 859 int my_server_thread_run(my_server_thread st) |
858 { | 860 { |
861 int timeout=st->timeout; | |
862 | |
859 st->active = 1; | 863 st->active = 1; |
860 st->done = 0; | 864 st->done = 0; |
861 while (st->active) { | 865 while (st->active) { |
862 lo_server_recv_noblock(st->s, timeout); | 866 lo_server_recv_noblock(st->s, timeout); |
863 } | 867 } |
872 { | 876 { |
873 my_server_thread st = (my_server_thread)data; | 877 my_server_thread st = (my_server_thread)data; |
874 | 878 |
875 printf("OSC server started.\n"); | 879 printf("OSC server started.\n"); |
876 PL_thread_attach_engine(NULL); | 880 PL_thread_attach_engine(NULL); |
877 my_server_thread_run(st,50); | 881 my_server_thread_run(st); |
878 PL_thread_destroy_engine(); | 882 PL_thread_destroy_engine(); |
879 printf("OSC server stopped.\n"); | 883 printf("OSC server stopped.\n"); |
880 pthread_exit(NULL); | 884 pthread_exit(NULL); |
881 } | 885 } |
882 | 886 |