From 840a6cb657b7f2d2661538c98cb4534dff734964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Thu, 3 Sep 2015 13:36:49 +0000 Subject: syncing with git svn path=/trunk/externals/iem/iemnet/; revision=17548 --- Makefile | 26 ++++++----- build/autotests/Makefile.am | 6 +-- iemnet.c | 26 +++-------- iemnet.h | 2 +- iemnet_receiver.c | 1 - iemnet_sender.c | 1 - tcpclient.c | 4 +- tcpreceive.c | 12 ++--- tcpsend.c | 4 +- tcpserver.c | 14 +++--- tests/delete/01_send-and-delete.pd | 39 +++++++++++++--- tests/delete/test-sender.pd | 90 +++++++++++++++++++++++++++++++++---- udpclient.c | 4 +- udpreceive.c | 34 +++++++------- udpsend.c | 7 +-- udpserver.c | 92 +++++++++++++++++++++----------------- 16 files changed, 235 insertions(+), 127 deletions(-) diff --git a/Makefile b/Makefile index 5815377..64b19ff 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ SHARED_HEADERS = iemnet_data.h iemnet.h # list all pd objects (i.e. myobject.pd) files here, and their helpfiles will # be included automatically -#PDOBJECTS = mypdobject.pd +PDOBJECTS = udpsndrcv.pd # example patches and related files, in the 'examples' subfolder #EXAMPLES = bothtogether.pd @@ -107,18 +107,24 @@ ifeq ($(UNAME),Darwin) SHARED_EXTENSION = dylib OS = macosx PD_PATH = /Applications/Pd-extended.app/Contents/Resources - OPT_CFLAGS = -ftree-vectorize -ftree-vectorizer-verbose=2 -fast + OPT_CFLAGS = -ftree-vectorize # build universal 32-bit on 10.4 and 32/64 on newer ifeq ($(shell uname -r | sed 's|\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*|\1|'), 8) FAT_FLAGS = -arch ppc -arch i386 -mmacosx-version-min=10.4 else - FAT_FLAGS = -arch ppc -arch i386 -arch x86_64 -mmacosx-version-min=10.4 + FAT_FLAGS = -arch i386 -arch x86_64 -mmacosx-version-min=10.4 SOURCES += $(SOURCES_iphoneos) endif - ALL_CFLAGS += $(FAT_FLAGS) -fPIC -I/sw/include + ALL_CFLAGS += $(FAT_FLAGS) -fPIC +ifneq ($(strip $(realpath /sw/include)),) + ALL_CFLAGS += -I/sw/include +endif # if the 'pd' binary exists, check the linking against it to aid with stripping BUNDLE_LOADER = $(shell test ! -e $(PD_PATH)/bin/pd || echo -bundle_loader $(PD_PATH)/bin/pd) - ALL_LDFLAGS += $(FAT_FLAGS) -bundle $(BUNDLE_LOADER) -undefined dynamic_lookup -L/sw/lib + ALL_LDFLAGS += $(FAT_FLAGS) -bundle $(BUNDLE_LOADER) -undefined dynamic_lookup +ifneq ($(strip $(realpath /sw/lib)),) + ALL_LDFLAGS += -L/sw/lib +endif SHARED_LDFLAGS += $(FAT_FLAGS) -dynamiclib -undefined dynamic_lookup \ -install_name @loader_path/$(SHARED_LIB) -compatibility_version 1 -current_version 1.0 ALL_LIBS += -lc $(LIBS_macosx) @@ -143,7 +149,7 @@ ifeq ($(UNAME),ANDROID) NDK_UNAME := $(shell uname -s | tr '[A-Z]' '[a-z]') NDK_TOOLCHAIN_BASE=$(NDK_BASE)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(NDK_UNAME)-x86 CC := $(NDK_TOOLCHAIN_BASE)/bin/arm-linux-androideabi-gcc --sysroot=$(NDK_SYSROOT) - OPT_CFLAGS = -O6 -funroll-loops -fomit-frame-pointer + OPT_CFLAGS = -O2 -funroll-loops -fomit-frame-pointer CFLAGS += LDFLAGS += -rdynamic -shared SHARED_LDFLAGS += -Wl,-soname,$(SHARED_LIB) -shared @@ -159,7 +165,7 @@ ifeq ($(UNAME),Linux) SHARED_EXTENSION = so OS = linux PD_PATH = /usr - OPT_CFLAGS = -O6 -funroll-loops -fomit-frame-pointer + OPT_CFLAGS = -O2 -funroll-loops -fomit-frame-pointer ALL_CFLAGS += -fPIC ALL_LDFLAGS += -rdynamic -shared -fPIC -Wl,-rpath,"\$$ORIGIN",--enable-new-dtags SHARED_LDFLAGS += -Wl,-soname,$(SHARED_LIB) -shared @@ -175,7 +181,7 @@ ifeq ($(UNAME),GNU) SHARED_EXTENSION = so OS = linux PD_PATH = /usr - OPT_CFLAGS = -O6 -funroll-loops -fomit-frame-pointer + OPT_CFLAGS = -O2 -funroll-loops -fomit-frame-pointer ALL_CFLAGS += -fPIC ALL_LDFLAGS += -rdynamic -shared -fPIC -Wl,-rpath,"\$$ORIGIN",--enable-new-dtags SHARED_LDFLAGS += -shared -Wl,-soname,$(SHARED_LIB) @@ -191,7 +197,7 @@ ifeq ($(UNAME),GNU/kFreeBSD) SHARED_EXTENSION = so OS = linux PD_PATH = /usr - OPT_CFLAGS = -O6 -funroll-loops -fomit-frame-pointer + OPT_CFLAGS = -O2 -funroll-loops -fomit-frame-pointer ALL_CFLAGS += -fPIC ALL_LDFLAGS += -rdynamic -shared -fPIC -Wl,-rpath,"\$$ORIGIN",--enable-new-dtags SHARED_LDFLAGS += -shared -Wl,-soname,$(SHARED_LIB) @@ -206,7 +212,7 @@ ifeq (CYGWIN,$(findstring CYGWIN,$(UNAME))) SHARED_EXTENSION = dll OS = cygwin PD_PATH = $(shell cygpath $$PROGRAMFILES)/pd - OPT_CFLAGS = -O6 -funroll-loops -fomit-frame-pointer + OPT_CFLAGS = -O2 -funroll-loops -fomit-frame-pointer ALL_CFLAGS += ALL_LDFLAGS += -rdynamic -shared -L"$(PD_PATH)/src" -L"$(PD_PATH)/bin" SHARED_LDFLAGS += -shared -Wl,-soname,$(SHARED_LIB) -L"$(PD_PATH)/src" -L"$(PD_PATH)/bin" diff --git a/build/autotests/Makefile.am b/build/autotests/Makefile.am index d5ca046..ed50959 100644 --- a/build/autotests/Makefile.am +++ b/build/autotests/Makefile.am @@ -13,11 +13,9 @@ libiemnet_la_CFLAGS = libiemnet_la_LIBADD = $(LIBM) libiemnet_la_SOURCES = \ - $(top_srcdir)/../../iemnet.c \ $(top_srcdir)/../../iemnet_data.c \ + $(top_srcdir)/../../iemnet_data.h \ $(top_srcdir)/../../iemnet_receiver.c \ $(top_srcdir)/../../iemnet_sender.c \ - $(top_srcdir)/../../iemnet_notify.c \ - $(top_srcdir)/../../iemnet_data.h \ + $(top_srcdir)/../../iemnet.c \ $(top_srcdir)/../../iemnet.h - diff --git a/iemnet.c b/iemnet.c index ce8cf4b..90e39d0 100644 --- a/iemnet.c +++ b/iemnet.c @@ -27,7 +27,7 @@ #include /* close a socket properly */ -void iemnet__closesocket(int sockfd) +void iemnet__closesocket(int sockfd, int verbose) { if(sockfd >=0) { #ifndef SHUT_RDWR @@ -36,7 +36,7 @@ void iemnet__closesocket(int sockfd) int how=SHUT_RDWR; int err = shutdown(sockfd, how); /* needed on linux, since the recv won't shutdown on sys_closesocket() alone */ - if(err) { + if(verbose && err) { perror("iemnet:socket-shutdown"); } sys_closesocket(sockfd); @@ -245,7 +245,7 @@ IEMNET_EXTERN void iemnet_setup(void) void iemnet_log(const void *object, const t_iemnet_loglevel level, const char *fmt, ...) { t_pd*x=(t_pd*)object; - const char*name=x?((*x)->c_name->s_name):0; + const char*name=(x && (*x) && ((*x)->c_name))?((*x)->c_name->s_name):"iemnet"; char buf[MAXPDSTRING]; va_list ap; t_int arg[8]; @@ -254,24 +254,12 @@ void iemnet_log(const void *object, const t_iemnet_loglevel level, const char *f va_end(ap); strcat(buf, "\0"); #if (defined PD_MINOR_VERSION) && (PD_MINOR_VERSION >= 43) - if (name) { - logpost(x, level, "[%s]: %s", name, buf); - } else { - logpost(x, level, "%s", buf); - } + logpost(x, level, "[%s]: %s", name, buf); #else - if (name) { - if(level>1) { - post("[%s]: %s", name, buf); - } else { - pd_error(x, "[%s]: %s", name, buf); - } + if(level>1) { + post("[%s]: %s", name, buf); } else { - if(level>1) { - post("%s", name, buf); - } else { - pd_error(x, "%s", name, buf); - } + pd_error(x, "[%s]: %s", name, buf); } #endif } diff --git a/iemnet.h b/iemnet.h index 4a54720..7a379cb 100644 --- a/iemnet.h +++ b/iemnet.h @@ -175,7 +175,7 @@ int iemnet__receiver_getsize(t_iemnet_receiver*); * * \param sock socket to close */ -void iemnet__closesocket(int fd); +void iemnet__closesocket(int fd, int verbose); /** * output the address (IP, port) diff --git a/iemnet_receiver.c b/iemnet_receiver.c index 442b055..5fb41bd 100644 --- a/iemnet_receiver.c +++ b/iemnet_receiver.c @@ -118,7 +118,6 @@ void iemnet__receiver_destroy(t_iemnet_receiver*rec, int subthread) } DEBUG("[%p] really destroying receiver %d", sockfd); - //iemnet__closesocket(sockfd); DEBUG("[%p] closed socket %d", rec, sockfd); rec->sockfd=-1; diff --git a/iemnet_sender.c b/iemnet_sender.c index 7c3c5d4..287cbb5 100644 --- a/iemnet_sender.c +++ b/iemnet_sender.c @@ -209,7 +209,6 @@ void iemnet__sender_destroy(t_iemnet_sender*s, int subthread) queue_finish(s->queue); DEBUG("queue finished"); - //iemnet__closesocket(sockfd); pthread_join(s->thread, NULL); DEBUG("thread joined"); diff --git a/tcpclient.c b/tcpclient.c index 40a32ea..cf0bc18 100644 --- a/tcpclient.c +++ b/tcpclient.c @@ -96,7 +96,7 @@ static int tcpclient_do_disconnect(int fd, t_iemnet_sender*sender, receiver=NULL; } if (fd >= 0) { - iemnet__closesocket(fd); + iemnet__closesocket(fd, 1); return 1; } return 0; @@ -135,7 +135,7 @@ static int tcpclient_do_connect(const char*host, unsigned short port, if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to connect to stream socket"); sys_sockerror("connect"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); return (-1); } diff --git a/tcpreceive.c b/tcpreceive.c index fb7c54d..b79c0d6 100644 --- a/tcpreceive.c +++ b/tcpreceive.c @@ -151,7 +151,7 @@ static void tcpreceive_connectpoll(t_tcpreceive *x) iemnet__addrout(x->x_statout, x->x_addrout, addr, port); } else { iemnet_log(x, IEMNET_ERROR, "too many connections"); - iemnet__closesocket(fd); + iemnet__closesocket(fd, 1); } } } @@ -162,7 +162,7 @@ static int tcpreceive_disconnect(t_tcpreceive *x, int id) iemnet__receiver_destroy(x->x_connection[id].receiver, 0); x->x_connection[id].receiver=NULL; - iemnet__closesocket(x->x_connection[id].socket); + iemnet__closesocket(x->x_connection[id].socket, 1); x->x_connection[id].socket = -1; x->x_connection[id].addr = 0L; @@ -217,7 +217,7 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno) /* cleanup any open ports */ if(sockfd>=0) { sys_rmpollfn(sockfd); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); x->x_connectsocket=-1; x->x_port=-1; } @@ -265,7 +265,7 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno) if (bind(sockfd, (struct sockaddr *)&server, serversize) < 0) { iemnet_log(x, IEMNET_ERROR, "couldn't bind socket"); sys_sockerror("bind"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); sockfd = -1; outlet_anything(x->x_statout, gensym("port"), 1, ap); return; @@ -275,7 +275,7 @@ static void tcpreceive_port(t_tcpreceive*x, t_floatarg fportno) if (listen(sockfd, 5) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to listen on socket"); sys_sockerror("listen"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); sockfd = -1; outlet_anything(x->x_statout, gensym("port"), 1, ap); return; @@ -307,7 +307,7 @@ static void tcpreceive_free(t_tcpreceive *x) /* is this ever called? */ if (x->x_connectsocket >= 0) { sys_rmpollfn(x->x_connectsocket); - iemnet__closesocket(x->x_connectsocket); + iemnet__closesocket(x->x_connectsocket, 1); } tcpreceive_disconnect_all(x); if(x->x_floatlist) { diff --git a/tcpsend.c b/tcpsend.c index f35c4e1..4597a32 100644 --- a/tcpsend.c +++ b/tcpsend.c @@ -48,7 +48,7 @@ static void tcpsend_disconnect(t_tcpsend *x) } x->x_sender=NULL; if (x->x_fd >= 0) { - iemnet__closesocket(x->x_fd); + iemnet__closesocket(x->x_fd, 1); x->x_fd = -1; outlet_float(x->x_obj.ob_outlet, 0); } @@ -102,7 +102,7 @@ static void tcpsend_connect(t_tcpsend *x, t_symbol *hostname, if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to initiate connection on socket %d", sockfd); sys_sockerror("connect"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); return; } x->x_fd = sockfd; diff --git a/tcpserver.c b/tcpserver.c index 218b6ba..ded8abe 100644 --- a/tcpserver.c +++ b/tcpserver.c @@ -117,7 +117,7 @@ static void tcpserver_socketreceiver_free(t_tcpserver_socketreceiver *x) iemnet__receiver_destroy(receiver, 0); } - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); freebytes(x, sizeof(*x)); } @@ -557,12 +557,12 @@ static void tcpserver_connectpoll(t_tcpserver *x) iemnet_log(x, IEMNET_ERROR, "cannot handle more than %d connections, dropping!", x->x_nconnections); - iemnet__closesocket(fd); + iemnet__closesocket(fd, 1); } y = tcpserver_socketreceiver_new((void *)x, fd, &incomer_address); if (!y) { - iemnet__closesocket(fd); + iemnet__closesocket(fd, 1); return; } x->x_nconnections++; @@ -592,7 +592,7 @@ static void tcpserver_port(t_tcpserver*x, t_floatarg fportno) /* cleanup any open ports */ if(sockfd>=0) { sys_rmpollfn(sockfd); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); x->x_connectsocket=-1; x->x_port=-1; } @@ -615,7 +615,7 @@ static void tcpserver_port(t_tcpserver*x, t_floatarg fportno) if (bind(sockfd, (struct sockaddr *)&server, serversize) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to bind to TCP/IP socket"); sys_sockerror("bind"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); outlet_anything(x->x_statout, gensym("port"), 1, ap); return; } @@ -624,7 +624,7 @@ static void tcpserver_port(t_tcpserver*x, t_floatarg fportno) if (listen(sockfd, 5) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to listen on TCP/IP socket"); sys_sockerror("listen"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); sockfd = -1; outlet_anything(x->x_statout, gensym("port"), 1, ap); return; @@ -697,7 +697,7 @@ static void tcpserver_free(t_tcpserver *x) } if (x->x_connectsocket >= 0) { sys_rmpollfn(x->x_connectsocket); - iemnet__closesocket(x->x_connectsocket); + iemnet__closesocket(x->x_connectsocket, 1); } if(x->x_floatlist) { iemnet__floatlist_destroy(x->x_floatlist); diff --git a/tests/delete/01_send-and-delete.pd b/tests/delete/01_send-and-delete.pd index 630b585..e2111ea 100644 --- a/tests/delete/01_send-and-delete.pd +++ b/tests/delete/01_send-and-delete.pd @@ -1,11 +1,40 @@ -#N canvas 529 261 450 300 10; -#X obj 302 40 udpreceive 9990; -#X floatatom 302 62 5 0 0 0 - - -, f 5; -#X obj 304 97 tcpreceive 9990; -#X floatatom 304 119 5 0 0 0 - - -, f 5; +#N canvas 534 241 634 300 10; +#X obj 332 40 udpreceive 9990; +#X floatatom 332 62 5 0 0 0 - - -, f 5; +#X obj 334 97 tcpreceive 9990; +#X floatatom 334 119 5 0 0 0 - - -, f 5; #X obj 128 38 test-sender udpsend; #X obj 128 58 test-sender udpclient; #X obj 128 98 test-sender tcpsend; #X obj 128 118 test-sender tcpclient; +#X obj 286 119 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 128 16 bang; +#X obj 286 40 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X obj 286 59 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X obj 286 98 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 375 118 serialize 0; +#X obj 450 98 loadbang; +#X msg 43 45 debug \$1; +#X floatatom 51 16 5 0 0 0 - - -, f 5; +#X msg 286 172 \; pd quit; +#X obj 286 148 select 1; #X connect 0 0 1 0; #X connect 2 0 3 0; +#X connect 4 0 5 0; +#X connect 4 0 10 0; +#X connect 5 0 6 0; +#X connect 5 0 11 0; +#X connect 6 0 7 0; +#X connect 6 0 12 0; +#X connect 7 0 8 0; +#X connect 8 0 18 0; +#X connect 9 0 4 0; +#X connect 13 0 2 0; +#X connect 14 0 13 0; +#X connect 15 0 4 0; +#X connect 16 0 15 0; +#X connect 18 0 17 0; diff --git a/tests/delete/test-sender.pd b/tests/delete/test-sender.pd index a3cad44..02929fb 100644 --- a/tests/delete/test-sender.pd +++ b/tests/delete/test-sender.pd @@ -1,4 +1,4 @@ -#N canvas 488 98 645 576 10; +#N canvas 493 78 758 639 10; #X text 28 37 1 delete object:; #X text 32 99 2a connect; #X text 32 114 2b disconnect; @@ -18,7 +18,7 @@ #X msg 343 123 auto; #X msg 231 37 delete; #X msg 343 23 auto; -#N canvas 11 49 679 407 test1 0; +#N canvas 12 50 679 407 test1 0; #X msg 101 166 connect localhost 9990; #X msg 265 166 disconnect; #X obj 25 7 inlet; @@ -34,7 +34,7 @@ #X obj 430 115 del 500; #X obj 430 187 t b b; #X msg 430 274 1; -#N canvas 5 49 450 300 \$0-test1 0; +#N canvas 5 50 450 300 \$0-test1 0; #X obj 86 168 \$1; #X obj 86 54 inlet; #X connect 1 0 0 0; @@ -106,7 +106,7 @@ #X obj 49 150 until; #X msg 49 125 1024; #X obj 49 100 until; -#X msg 49 75 100; +#X msg 49 75 1; #X connect 0 0 6 0; #X connect 2 0 1 0; #X connect 3 0 2 0; @@ -117,6 +117,7 @@ #X obj 430 140 t b b; #X obj 430 162 del 500; #X obj 397 240 s pd-\$0-test1; +#X obj 249 328 r \$0-ctl; #X connect 0 0 15 0; #X connect 1 0 15 0; #X connect 2 0 3 0; @@ -141,12 +142,13 @@ #X connect 17 0 15 0; #X connect 18 0 19 0; #X connect 19 0 13 0; +#X connect 21 0 15 0; #X restore 231 59 pd test1; #X obj 297 60 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1; #X obj 301 160 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1; -#N canvas 12 50 679 407 test2 0; +#N canvas 13 50 679 407 test2 0; #X msg 101 166 connect localhost 9990; #X msg 265 166 disconnect; #X obj 25 7 inlet; @@ -245,6 +247,7 @@ #X connect 1 0 0 0; #X restore 118 346 pd \$0-test2; #X obj 397 240 s pd-\$0-test2; +#X obj 249 329 r \$0-ctl; #X connect 0 0 19 0; #X connect 1 0 19 0; #X connect 2 0 3 0; @@ -271,13 +274,14 @@ #X connect 17 0 18 0; #X connect 17 1 1 0; #X connect 18 0 13 0; +#X connect 21 0 19 0; #X restore 231 159 pd test2; #X msg 231 193 connect; #X msg 231 217 delete; #X msg 343 203 auto; #X obj 301 240 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1; -#N canvas 13 50 679 407 test3 0; +#N canvas 14 50 679 407 test3 0; #X msg 101 166 connect localhost 9990; #X msg 265 166 disconnect; #X obj 25 7 inlet; @@ -376,6 +380,7 @@ #X connect 1 0 0 0; #X restore 118 346 pd \$0-test3; #X obj 397 240 s pd-\$0-test3; +#X obj 249 329 r \$0-ctl; #X connect 0 0 19 0; #X connect 1 0 19 0; #X connect 2 0 3 0; @@ -401,6 +406,7 @@ #X connect 16 0 19 0; #X connect 17 0 18 0; #X connect 18 0 13 0; +#X connect 21 0 19 0; #X restore 231 239 pd test3; #X msg 231 273 connect; #X msg 231 315 disconnect; @@ -409,7 +415,7 @@ #X obj 301 360 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1; #X msg 231 294 send; -#N canvas 11 50 679 407 test4 0; +#N canvas 12 50 679 407 test4 0; #X msg 101 166 connect localhost 9990; #X msg 265 166 disconnect; #X obj 25 7 inlet; @@ -508,6 +514,7 @@ #X connect 1 0 0 0; #X restore 118 346 pd \$0-test4; #X obj 397 240 s pd-\$0-test4; +#X obj 249 329 r \$0-ctl; #X connect 0 0 19 0; #X connect 1 0 19 0; #X connect 2 0 3 0; @@ -535,6 +542,7 @@ #X connect 17 0 18 0; #X connect 17 1 1 0; #X connect 18 0 13 0; +#X connect 21 0 19 0; #X restore 231 359 pd test4; #X msg 231 403 connect; #X msg 231 467 delete; @@ -542,7 +550,7 @@ #X obj 301 490 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1; #X msg 231 424 send; -#N canvas 14 49 679 407 test5 0; +#N canvas 15 50 679 407 test5 0; #X msg 101 166 connect localhost 9990; #X msg 265 166 disconnect; #X obj 25 7 inlet; @@ -635,12 +643,13 @@ #X restore 275 209 pd data; #X obj 430 140 t b b; #X obj 430 162 del 500; -#N canvas 5 49 450 300 \$0-test5 0; +#N canvas 5 50 450 300 \$0-test5 0; #X obj 86 168 \$1; #X obj 86 54 inlet; #X connect 1 0 0 0; #X restore 118 346 pd \$0-test5; #X obj 397 240 s pd-\$0-test5; +#X obj 249 329 r \$0-ctl; #X connect 0 0 19 0; #X connect 1 0 19 0; #X connect 2 0 3 0; @@ -667,7 +676,38 @@ #X connect 16 0 19 0; #X connect 17 0 18 0; #X connect 18 0 13 0; +#X connect 21 0 19 0; #X restore 231 490 pd test5; +#X obj 522 15 inlet; +#X obj 343 58 spigot 0; +#X obj 406 58 r \$0-auto; +#X obj 343 80 select 1; +#X obj 343 158 spigot 0; +#X obj 406 158 r \$0-auto; +#X obj 343 180 select 1; +#X obj 343 238 spigot 0; +#X obj 406 238 r \$0-auto; +#X obj 343 260 select 1; +#X obj 343 358 spigot 0; +#X obj 406 358 r \$0-auto; +#X obj 343 380 select 1; +#X obj 343 508 spigot 0; +#X obj 406 508 r \$0-auto; +#X obj 343 530 select 1; +#X obj 596 82 t b b; +#X msg 628 105 1; +#X obj 628 127 s \$0-auto; +#X obj 343 552 t b b; +#X msg 413 549 0; +#X obj 413 571 s \$0-auto; +#X obj 522 524 r \$0-auto; +#X obj 522 546 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 343 574 1; +#X obj 343 596 outlet; +#X obj 522 37 route debug; +#X obj 522 81 s \$0-ctl; +#X msg 522 59 debug \$1; #X connect 13 0 22 0; #X connect 14 0 22 0; #X connect 15 0 22 0; @@ -675,19 +715,51 @@ #X connect 17 0 19 0; #X connect 18 0 19 0; #X connect 19 0 20 0; +#X connect 20 0 42 0; +#X connect 21 0 45 0; #X connect 22 0 21 0; #X connect 23 0 27 0; #X connect 24 0 27 0; #X connect 25 0 27 0; +#X connect 26 0 48 0; #X connect 27 0 26 0; #X connect 28 0 34 0; #X connect 29 0 34 0; #X connect 30 0 34 0; #X connect 31 0 34 0; +#X connect 32 0 51 0; #X connect 33 0 34 0; #X connect 34 0 32 0; #X connect 35 0 40 0; #X connect 36 0 40 0; #X connect 37 0 40 0; +#X connect 38 0 54 0; #X connect 39 0 40 0; #X connect 40 0 38 0; +#X connect 41 0 67 0; +#X connect 42 0 44 0; +#X connect 43 0 42 1; +#X connect 44 0 16 0; +#X connect 45 0 47 0; +#X connect 46 0 45 1; +#X connect 47 0 25 0; +#X connect 48 0 50 0; +#X connect 49 0 48 1; +#X connect 50 0 31 0; +#X connect 51 0 53 0; +#X connect 52 0 51 1; +#X connect 53 0 37 0; +#X connect 54 0 56 0; +#X connect 55 0 54 1; +#X connect 56 0 60 0; +#X connect 57 0 18 0; +#X connect 57 1 58 0; +#X connect 58 0 59 0; +#X connect 60 0 65 0; +#X connect 60 1 61 0; +#X connect 61 0 62 0; +#X connect 63 0 64 0; +#X connect 65 0 66 0; +#X connect 67 0 69 0; +#X connect 67 1 57 0; +#X connect 69 0 68 0; diff --git a/udpclient.c b/udpclient.c index d35dd3c..ca6d0c5 100644 --- a/udpclient.c +++ b/udpclient.c @@ -117,7 +117,7 @@ static void *udpclient_doconnect(t_udpclient*x, int subthread) if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to connect to stream socket"); sys_sockerror("connect"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); return (x); } x->x_fd = sockfd; @@ -148,7 +148,7 @@ static int udpclient_do_disconnect(t_udpclient *x) if (x->x_fd < 0) { return 0; } - iemnet__closesocket(x->x_fd); + iemnet__closesocket(x->x_fd, 1); x->x_fd = -1; return 1; } diff --git a/udpreceive.c b/udpreceive.c index 7aaca12..7956860 100644 --- a/udpreceive.c +++ b/udpreceive.c @@ -38,10 +38,10 @@ typedef struct _udpreceive { t_outlet *x_addrout; t_outlet *x_statout; - int x_connectsocket; + int x_fd; int x_port; - t_iemnet_receiver*x_receiver; - t_iemnet_floatlist *x_floatlist; + t_iemnet_receiver *x_receiver; + t_iemnet_floatlist*x_floatlist; int x_reuseport, x_reuseaddr; } t_udpreceive; @@ -65,19 +65,23 @@ static int udpreceive_setport(t_udpreceive*x, unsigned short portno) { struct sockaddr_in server; socklen_t serversize=sizeof(server); - int sockfd = x->x_connectsocket; + int sockfd = x->x_fd; int intarg; memset(&server, 0, sizeof(server)); if(x->x_port == portno) { + iemnet_log(x, IEMNET_VERBOSE, "skipping re-binding to port:%d", portno); return 1; } /* cleanup any open ports */ - if(sockfd>=0) { + if(x->x_receiver) { iemnet__receiver_destroy(x->x_receiver, 0); - iemnet__closesocket(sockfd); - x->x_connectsocket=-1; + x->x_receiver=NULL; + } + if(sockfd>=0) { + iemnet__closesocket(sockfd, 1); + x->x_fd=-1; x->x_port=-1; } @@ -120,12 +124,12 @@ static int udpreceive_setport(t_udpreceive*x, unsigned short portno) if (bind(sockfd, (struct sockaddr *)&server, serversize) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to bind to socket"); sys_sockerror("bind"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); sockfd = -1; return 0; } - x->x_connectsocket = sockfd; + x->x_fd = sockfd; x->x_port = portno; // find out which port is actually used (useful when assigning "0") @@ -197,15 +201,15 @@ static void *udpreceive_new(t_floatarg fportno) x->x_addrout = outlet_new(&x->x_obj, gensym("list")); x->x_statout = outlet_new(&x->x_obj, 0); - x->x_connectsocket = -1; + x->x_fd = -1; x->x_port = -1; x->x_receiver = NULL; + x->x_floatlist=iemnet__floatlist_create(1024); + x->x_reuseaddr = 1; x->x_reuseport = 0; - x->x_floatlist=iemnet__floatlist_create(1024); - udpreceive_setport(x, fportno); return (x); @@ -217,10 +221,10 @@ static void udpreceive_free(t_udpreceive *x) iemnet__receiver_destroy(x->x_receiver, 0); } x->x_receiver=NULL; - if(x->x_connectsocket >= 0) { - iemnet__closesocket(x->x_connectsocket); + if(x->x_fd >= 0) { + iemnet__closesocket(x->x_fd, 0); } - x->x_connectsocket=-1; + x->x_fd=-1; outlet_free(x->x_msgout); outlet_free(x->x_addrout); diff --git a/udpsend.c b/udpsend.c index 3451a41..dccd9c5 100644 --- a/udpsend.c +++ b/udpsend.c @@ -90,7 +90,7 @@ static void udpsend_connect(t_udpsend *x, t_symbol *hostname, if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to connect to socket:%d", sockfd); sys_sockerror("connect"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); return; } x->x_sender=iemnet__sender_create(sockfd, NULL, NULL, 0); @@ -105,7 +105,7 @@ static void udpsend_disconnect(t_udpsend *x) } x->x_sender=NULL; if(x->x_fd >= 0) { - iemnet__closesocket(x->x_fd); + iemnet__closesocket(x->x_fd, 1); x->x_fd=-1; outlet_float(x->x_obj.ob_outlet, 0); } @@ -132,7 +132,8 @@ static void *udpsend_new(void) t_udpsend *x = (t_udpsend *)pd_new(udpsend_class); outlet_new(&x->x_obj, gensym("float")); x->x_sender=NULL; - return (x); + x->x_fd=-1; + return (x); } IEMNET_EXTERN void udpsend_setup(void) diff --git a/udpserver.c b/udpserver.c index 8ecfe90..900e0d7 100644 --- a/udpserver.c +++ b/udpserver.c @@ -45,32 +45,32 @@ typedef struct _udpserver_sender { long sr_host; unsigned short sr_port; - t_int sr_fd; + int sr_fd; t_iemnet_sender*sr_sender; } t_udpserver_sender; typedef struct _udpserver { - t_object x_obj; - t_outlet *x_msgout; - t_outlet *x_connectout; - t_outlet *x_sockout; // legacy - t_outlet *x_addrout; // legacy - t_outlet *x_statout; - - t_udpserver_sender *x_sr[MAX_CONNECT]; /* socket per connection */ - t_int x_nconnections; - - t_int - x_connectsocket; /* socket waiting for new connections */ - t_int x_port; - unsigned char - x_accept; /* whether we accept new connections or not */ - - /* the default connection to send to; 0=broadcast; >0 use this client; <0 exclude this client */ - int x_defaulttarget; - - t_iemnet_receiver *x_receiver; - t_iemnet_floatlist *x_floatlist; + t_object x_obj; + t_outlet*x_msgout; + t_outlet*x_connectout; + t_outlet*x_sockout; // legacy + t_outlet*x_addrout; // legacy + t_outlet*x_statout; + + t_udpserver_sender*x_sr[MAX_CONNECT]; /* socket per connection */ + unsigned int x_nconnections; + + int x_connectsocket; /* socket waiting for new connections */ + unsigned short x_port; + unsigned char x_accept; /* whether we accept new connections or not */ + + /* the default connection to send to; + 0=broadcast; >0 use this client; <0 exclude this client + */ + int x_defaulttarget; + + t_iemnet_receiver*x_receiver; + t_iemnet_floatlist*x_floatlist; } t_udpserver; static t_udpserver_sender *udpserver_sender_new(t_udpserver *owner, @@ -112,7 +112,7 @@ static void udpserver_sender_free(t_udpserver_sender *x) iemnet__sender_destroy(sender, 0); } if(sockfd>=0) { - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); } } /* coverity[pass_freed_arg]: this is merely for debugging printout */ @@ -126,7 +126,7 @@ static t_udpserver_sender* udpserver_sender_copy(t_udpserver_sender*x) static int udpserver_socket2index(t_udpserver*x, int sockfd) { - int i=0; + unsigned int i=0; for(i = 0; i < x->x_nconnections; i++) { /* check if connection exists */ if(x->x_sr[i]->sr_fd == sockfd) { return i; @@ -139,14 +139,22 @@ static int udpserver_socket2index(t_udpserver*x, int sockfd) * if the id is invalid, returns -1 * if the id is valid, return the 0-based index (client-1) */ -static int udpserver_fixindex(t_udpserver*x, int client) +static int udpserver_fixindex(t_udpserver*x, int client_) { + unsigned int client; + if(client_<1) { + iemnet_log(x, IEMNET_ERROR, + "client:%d out of range [1..%d]", + client_, (int)(x->x_nconnections)); + return -1; + } + client = (unsigned int)client_; if(x->x_nconnections <= 0) { iemnet_log(x, IEMNET_ERROR, "no clients connected"); return -1; } - if (!((client > 0) && (client <= x->x_nconnections))) { + if (client > x->x_nconnections) { iemnet_log(x, IEMNET_ERROR, "client:%d out of range [1..%d]", client, (int)(x->x_nconnections)); @@ -170,7 +178,7 @@ static int equal_addr(unsigned long host1, unsigned short port1, static int udpserver__find_sender(t_udpserver*x, unsigned long host, unsigned short port) { - int i=0; + unsigned int i=0; for(i=0; ix_nconnections; i++) { if(NULL==x->x_sr[i]) { return -1; @@ -393,13 +401,14 @@ static void udpserver_send_toclient(t_udpserver *x, unsigned int client, static void udpserver_send_client(t_udpserver *x, t_symbol *s, int argc, t_atom *argv) { - int client=0; + unsigned int client=0; if (argc > 0) { - client=udpserver_fixindex(x, atom_getint(argv)); - if(client<0) { + int c=udpserver_fixindex(x, atom_getint(argv)); + if(c<0) { return; } + client=(unsigned int)c; if(argc==1) { udpserver_info_client(x, client); } else { @@ -482,7 +491,7 @@ static void udpserver_defaulttarget(t_udpserver *x, t_floatarg f) { int sockfd=0; int rawclient=f; - int client=(rawclient<0)?(-rawclient):rawclient; + unsigned int client=(rawclient<0)?(-rawclient):rawclient; if(client > x->x_nconnections) { iemnet_log(x, IEMNET_ERROR, @@ -613,7 +622,7 @@ static void udpserver_receive_callback(void *y, t_iemnet_chunk*c) } if(c) { - int conns = x->x_nconnections; + unsigned int conns = x->x_nconnections; t_udpserver_sender*sdr=NULL; DEBUG("add new sender from %d", c->port); sdr=udpserver_sender_add(x, c->addr, c->port); @@ -632,7 +641,7 @@ static void udpserver_receive_callback(void *y, t_iemnet_chunk*c) } } else { // disconnection never happens with a connectionless protocol like UDP - iemnet_log(x, IEMNET_ERROR, "received disonnection event"); + iemnet_log(x, IEMNET_ERROR, "received disconnection event"); } } @@ -660,7 +669,7 @@ static void udpserver_connectpoll(t_udpserver *x) t_udpserver_sender *y = udpserver_sender_new(x, host, port); if (!y) { - iemnet__closesocket(fd); + iemnet__closesocket(fd, 1); return; } x->x_nconnections++; @@ -690,7 +699,7 @@ static void udpserver_port(t_udpserver*x, t_floatarg fportno) /* cleanup any open ports */ if(sockfd>=0) { //sys_rmpollfn(sockfd); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 0); x->x_connectsocket=-1; x->x_port=-1; } @@ -714,7 +723,7 @@ static void udpserver_port(t_udpserver*x, t_floatarg fportno) if (bind(sockfd, (struct sockaddr *)&server, serversize) < 0) { iemnet_log(x, IEMNET_ERROR, "unable to bind to socket"); sys_sockerror("bind"); - iemnet__closesocket(sockfd); + iemnet__closesocket(sockfd, 1); outlet_anything(x->x_statout, gensym("port"), 1, ap); return; } @@ -771,7 +780,6 @@ static void *udpserver_new(t_floatarg fportno) static void udpserver_free(t_udpserver *x) { int i; - for(i = 0; i < MAX_CONNECT; i++) { if (NULL!=x->x_sr[i]) { DEBUG("[%s] free %x", objName, x); @@ -779,14 +787,18 @@ static void udpserver_free(t_udpserver *x) x->x_sr[i]=NULL; } } + if(x->x_receiver) { + iemnet__receiver_destroy(x->x_receiver, 0); + x->x_receiver=NULL; + } if (x->x_connectsocket >= 0) { - //sys_rmpollfn(x->x_connectsocket); - iemnet__closesocket(x->x_connectsocket); + iemnet__closesocket(x->x_connectsocket, 0); + x->x_connectsocket = -1; } if(x->x_floatlist) { iemnet__floatlist_destroy(x->x_floatlist); + x->x_floatlist=NULL; } - x->x_floatlist=NULL; } IEMNET_EXTERN void udpserver_setup(void) -- cgit v1.2.1